1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "extensions/common/extension_messages.h"
7 #include "content/public/common/common_param_traits.h"
8 #include "extensions/common/extension.h"
9 #include "extensions/common/manifest.h"
10 #include "extensions/common/manifest_handler.h"
11 #include "extensions/common/permissions/permissions_data.h"
12 #include "extensions/common/permissions/permissions_info.h"
14 using extensions::APIPermission
;
15 using extensions::APIPermissionInfo
;
16 using extensions::APIPermissionSet
;
17 using extensions::Extension
;
18 using extensions::Manifest
;
19 using extensions::ManifestHandler
;
20 using extensions::ManifestPermission
;
21 using extensions::ManifestPermissionSet
;
22 using extensions::PermissionSet
;
23 using extensions::URLPatternSet
;
25 ExtensionMsg_PermissionSetStruct::ExtensionMsg_PermissionSetStruct() {
28 ExtensionMsg_PermissionSetStruct::ExtensionMsg_PermissionSetStruct(
29 const PermissionSet
& permissions
)
30 : apis(permissions
.apis()),
31 manifest_permissions(permissions
.manifest_permissions()),
32 explicit_hosts(permissions
.explicit_hosts()),
33 scriptable_hosts(permissions
.scriptable_hosts()) {
36 ExtensionMsg_PermissionSetStruct::~ExtensionMsg_PermissionSetStruct() {
39 scoped_refptr
<const PermissionSet
>
40 ExtensionMsg_PermissionSetStruct::ToPermissionSet() const {
41 return new PermissionSet(
42 apis
, manifest_permissions
, explicit_hosts
, scriptable_hosts
);
45 ExtensionMsg_Loaded_Params::ExtensionMsg_Loaded_Params()
46 : location(Manifest::INVALID_LOCATION
),
47 creation_flags(Extension::NO_FLAGS
) {}
49 ExtensionMsg_Loaded_Params::~ExtensionMsg_Loaded_Params() {}
51 ExtensionMsg_Loaded_Params::ExtensionMsg_Loaded_Params(
52 const Extension
* extension
,
53 bool include_tab_permissions
)
54 : manifest(extension
->manifest()->value()->DeepCopy()),
55 location(extension
->location()),
56 path(extension
->path()),
57 active_permissions(*extension
->permissions_data()->active_permissions()),
59 *extension
->permissions_data()->withheld_permissions()),
61 creation_flags(extension
->creation_flags()) {
62 if (include_tab_permissions
) {
63 extensions::PermissionsData::TabPermissionsMap tab_permissions
=
64 extension
->permissions_data()->CopyTabSpecificPermissionsMap();
65 for (const auto& pair
: tab_permissions
) {
66 tab_specific_permissions
[pair
.first
] =
67 ExtensionMsg_PermissionSetStruct(*pair
.second
);
72 scoped_refptr
<Extension
> ExtensionMsg_Loaded_Params::ConvertToExtension(
73 std::string
* error
) const {
74 scoped_refptr
<Extension
> extension
=
75 Extension::Create(path
, location
, *manifest
, creation_flags
, error
);
76 if (extension
.get()) {
77 const extensions::PermissionsData
* permissions_data
=
78 extension
->permissions_data();
79 permissions_data
->SetPermissions(active_permissions
.ToPermissionSet(),
80 withheld_permissions
.ToPermissionSet());
81 for (const auto& pair
: tab_specific_permissions
) {
82 permissions_data
->UpdateTabSpecificPermissions(
83 pair
.first
, pair
.second
.ToPermissionSet());
92 struct ParamTraits
<Manifest::Location
> {
93 typedef Manifest::Location param_type
;
94 static void Write(Message
* m
, const param_type
& p
) {
95 int val
= static_cast<int>(p
);
98 static bool Read(const Message
* m
, PickleIterator
* iter
, param_type
* p
) {
100 if (!ReadParam(m
, iter
, &val
) ||
101 val
< Manifest::INVALID_LOCATION
||
102 val
>= Manifest::NUM_LOCATIONS
)
104 *p
= static_cast<param_type
>(val
);
107 static void Log(const param_type
& p
, std::string
* l
) {
108 ParamTraits
<int>::Log(static_cast<int>(p
), l
);
112 void ParamTraits
<URLPattern
>::Write(Message
* m
, const param_type
& p
) {
113 WriteParam(m
, p
.valid_schemes());
114 WriteParam(m
, p
.GetAsString());
117 bool ParamTraits
<URLPattern
>::Read(const Message
* m
, PickleIterator
* iter
,
121 if (!ReadParam(m
, iter
, &valid_schemes
) ||
122 !ReadParam(m
, iter
, &spec
))
125 // TODO(jstritar): We don't want the URLPattern to fail parsing when the
126 // scheme is invalid. Instead, the pattern should parse but it should not
127 // match the invalid patterns. We get around this by setting the valid
128 // schemes after parsing the pattern. Update these method calls once we can
129 // ignore scheme validation with URLPattern parse options. crbug.com/90544
130 p
->SetValidSchemes(URLPattern::SCHEME_ALL
);
131 URLPattern::ParseResult result
= p
->Parse(spec
);
132 p
->SetValidSchemes(valid_schemes
);
133 return URLPattern::PARSE_SUCCESS
== result
;
136 void ParamTraits
<URLPattern
>::Log(const param_type
& p
, std::string
* l
) {
137 LogParam(p
.GetAsString(), l
);
140 void ParamTraits
<URLPatternSet
>::Write(Message
* m
, const param_type
& p
) {
141 WriteParam(m
, p
.patterns());
144 bool ParamTraits
<URLPatternSet
>::Read(const Message
* m
, PickleIterator
* iter
,
146 std::set
<URLPattern
> patterns
;
147 if (!ReadParam(m
, iter
, &patterns
))
150 for (std::set
<URLPattern
>::iterator i
= patterns
.begin();
151 i
!= patterns
.end(); ++i
)
156 void ParamTraits
<URLPatternSet
>::Log(const param_type
& p
, std::string
* l
) {
157 LogParam(p
.patterns(), l
);
160 void ParamTraits
<APIPermission::ID
>::Write(
161 Message
* m
, const param_type
& p
) {
162 WriteParam(m
, static_cast<int>(p
));
165 bool ParamTraits
<APIPermission::ID
>::Read(
166 const Message
* m
, PickleIterator
* iter
, param_type
* p
) {
168 if (!ReadParam(m
, iter
, &api_id
))
171 *p
= static_cast<APIPermission::ID
>(api_id
);
175 void ParamTraits
<APIPermission::ID
>::Log(
176 const param_type
& p
, std::string
* l
) {
177 LogParam(static_cast<int>(p
), l
);
180 void ParamTraits
<APIPermissionSet
>::Write(
181 Message
* m
, const param_type
& p
) {
182 APIPermissionSet::const_iterator it
= p
.begin();
183 const APIPermissionSet::const_iterator end
= p
.end();
184 WriteParam(m
, p
.size());
185 for (; it
!= end
; ++it
) {
186 WriteParam(m
, it
->id());
191 bool ParamTraits
<APIPermissionSet
>::Read(
192 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
194 if (!ReadParam(m
, iter
, &size
))
196 for (size_t i
= 0; i
< size
; ++i
) {
197 APIPermission::ID id
;
198 if (!ReadParam(m
, iter
, &id
))
200 const APIPermissionInfo
* permission_info
=
201 extensions::PermissionsInfo::GetInstance()->GetByID(id
);
202 if (!permission_info
)
204 scoped_ptr
<APIPermission
> p(permission_info
->CreateAPIPermission());
205 if (!p
->Read(m
, iter
))
207 r
->insert(p
.release());
212 void ParamTraits
<APIPermissionSet
>::Log(
213 const param_type
& p
, std::string
* l
) {
214 LogParam(p
.map(), l
);
217 void ParamTraits
<ManifestPermissionSet
>::Write(
218 Message
* m
, const param_type
& p
) {
219 ManifestPermissionSet::const_iterator it
= p
.begin();
220 const ManifestPermissionSet::const_iterator end
= p
.end();
221 WriteParam(m
, p
.size());
222 for (; it
!= end
; ++it
) {
223 WriteParam(m
, it
->name());
228 bool ParamTraits
<ManifestPermissionSet
>::Read(
229 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
231 if (!ReadParam(m
, iter
, &size
))
233 for (size_t i
= 0; i
< size
; ++i
) {
235 if (!ReadParam(m
, iter
, &name
))
237 scoped_ptr
<ManifestPermission
> p(ManifestHandler::CreatePermission(name
));
240 if (!p
->Read(m
, iter
))
242 r
->insert(p
.release());
247 void ParamTraits
<ManifestPermissionSet
>::Log(
248 const param_type
& p
, std::string
* l
) {
249 LogParam(p
.map(), l
);
252 void ParamTraits
<HostID
>::Write(
253 Message
* m
, const param_type
& p
) {
254 WriteParam(m
, p
.type());
255 WriteParam(m
, p
.id());
258 bool ParamTraits
<HostID
>::Read(
259 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
260 HostID::HostType type
;
262 if (!ReadParam(m
, iter
, &type
))
264 if (!ReadParam(m
, iter
, &id
))
266 *r
= HostID(type
, id
);
270 void ParamTraits
<HostID
>::Log(
271 const param_type
& p
, std::string
* l
) {
272 LogParam(p
.type(), l
);
276 void ParamTraits
<ExtensionMsg_PermissionSetStruct
>::Write(Message
* m
,
277 const param_type
& p
) {
278 WriteParam(m
, p
.apis
);
279 WriteParam(m
, p
.manifest_permissions
);
280 WriteParam(m
, p
.explicit_hosts
);
281 WriteParam(m
, p
.scriptable_hosts
);
284 bool ParamTraits
<ExtensionMsg_PermissionSetStruct
>::Read(const Message
* m
,
285 PickleIterator
* iter
,
287 return ReadParam(m
, iter
, &p
->apis
) &&
288 ReadParam(m
, iter
, &p
->manifest_permissions
) &&
289 ReadParam(m
, iter
, &p
->explicit_hosts
) &&
290 ReadParam(m
, iter
, &p
->scriptable_hosts
);
293 void ParamTraits
<ExtensionMsg_PermissionSetStruct
>::Log(const param_type
& p
,
296 LogParam(p
.manifest_permissions
, l
);
297 LogParam(p
.explicit_hosts
, l
);
298 LogParam(p
.scriptable_hosts
, l
);
301 void ParamTraits
<ExtensionMsg_Loaded_Params
>::Write(Message
* m
,
302 const param_type
& p
) {
303 WriteParam(m
, p
.location
);
304 WriteParam(m
, p
.path
);
305 WriteParam(m
, *(p
.manifest
));
306 WriteParam(m
, p
.creation_flags
);
307 WriteParam(m
, p
.active_permissions
);
308 WriteParam(m
, p
.withheld_permissions
);
311 bool ParamTraits
<ExtensionMsg_Loaded_Params
>::Read(const Message
* m
,
312 PickleIterator
* iter
,
314 p
->manifest
.reset(new base::DictionaryValue());
315 return ReadParam(m
, iter
, &p
->location
) && ReadParam(m
, iter
, &p
->path
) &&
316 ReadParam(m
, iter
, p
->manifest
.get()) &&
317 ReadParam(m
, iter
, &p
->creation_flags
) &&
318 ReadParam(m
, iter
, &p
->active_permissions
) &&
319 ReadParam(m
, iter
, &p
->withheld_permissions
);
322 void ParamTraits
<ExtensionMsg_Loaded_Params
>::Log(const param_type
& p
,