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 : manifest(extension
->manifest()->value()->DeepCopy()),
54 location(extension
->location()),
55 path(extension
->path()),
56 active_permissions(*extension
->permissions_data()->active_permissions()),
58 *extension
->permissions_data()->withheld_permissions()),
60 creation_flags(extension
->creation_flags()) {
63 scoped_refptr
<Extension
> ExtensionMsg_Loaded_Params::ConvertToExtension(
64 std::string
* error
) const {
65 scoped_refptr
<Extension
> extension
=
66 Extension::Create(path
, location
, *manifest
, creation_flags
, error
);
67 if (extension
.get()) {
68 extension
->permissions_data()->SetPermissions(
69 active_permissions
.ToPermissionSet(),
70 withheld_permissions
.ToPermissionSet());
78 struct ParamTraits
<Manifest::Location
> {
79 typedef Manifest::Location param_type
;
80 static void Write(Message
* m
, const param_type
& p
) {
81 int val
= static_cast<int>(p
);
84 static bool Read(const Message
* m
, PickleIterator
* iter
, param_type
* p
) {
86 if (!ReadParam(m
, iter
, &val
) ||
87 val
< Manifest::INVALID_LOCATION
||
88 val
>= Manifest::NUM_LOCATIONS
)
90 *p
= static_cast<param_type
>(val
);
93 static void Log(const param_type
& p
, std::string
* l
) {
94 ParamTraits
<int>::Log(static_cast<int>(p
), l
);
98 void ParamTraits
<URLPattern
>::Write(Message
* m
, const param_type
& p
) {
99 WriteParam(m
, p
.valid_schemes());
100 WriteParam(m
, p
.GetAsString());
103 bool ParamTraits
<URLPattern
>::Read(const Message
* m
, PickleIterator
* iter
,
107 if (!ReadParam(m
, iter
, &valid_schemes
) ||
108 !ReadParam(m
, iter
, &spec
))
111 // TODO(jstritar): We don't want the URLPattern to fail parsing when the
112 // scheme is invalid. Instead, the pattern should parse but it should not
113 // match the invalid patterns. We get around this by setting the valid
114 // schemes after parsing the pattern. Update these method calls once we can
115 // ignore scheme validation with URLPattern parse options. crbug.com/90544
116 p
->SetValidSchemes(URLPattern::SCHEME_ALL
);
117 URLPattern::ParseResult result
= p
->Parse(spec
);
118 p
->SetValidSchemes(valid_schemes
);
119 return URLPattern::PARSE_SUCCESS
== result
;
122 void ParamTraits
<URLPattern
>::Log(const param_type
& p
, std::string
* l
) {
123 LogParam(p
.GetAsString(), l
);
126 void ParamTraits
<URLPatternSet
>::Write(Message
* m
, const param_type
& p
) {
127 WriteParam(m
, p
.patterns());
130 bool ParamTraits
<URLPatternSet
>::Read(const Message
* m
, PickleIterator
* iter
,
132 std::set
<URLPattern
> patterns
;
133 if (!ReadParam(m
, iter
, &patterns
))
136 for (std::set
<URLPattern
>::iterator i
= patterns
.begin();
137 i
!= patterns
.end(); ++i
)
142 void ParamTraits
<URLPatternSet
>::Log(const param_type
& p
, std::string
* l
) {
143 LogParam(p
.patterns(), l
);
146 void ParamTraits
<APIPermission::ID
>::Write(
147 Message
* m
, const param_type
& p
) {
148 WriteParam(m
, static_cast<int>(p
));
151 bool ParamTraits
<APIPermission::ID
>::Read(
152 const Message
* m
, PickleIterator
* iter
, param_type
* p
) {
154 if (!ReadParam(m
, iter
, &api_id
))
157 *p
= static_cast<APIPermission::ID
>(api_id
);
161 void ParamTraits
<APIPermission::ID
>::Log(
162 const param_type
& p
, std::string
* l
) {
163 LogParam(static_cast<int>(p
), l
);
166 void ParamTraits
<APIPermissionSet
>::Write(
167 Message
* m
, const param_type
& p
) {
168 APIPermissionSet::const_iterator it
= p
.begin();
169 const APIPermissionSet::const_iterator end
= p
.end();
170 WriteParam(m
, p
.size());
171 for (; it
!= end
; ++it
) {
172 WriteParam(m
, it
->id());
177 bool ParamTraits
<APIPermissionSet
>::Read(
178 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
180 if (!ReadParam(m
, iter
, &size
))
182 for (size_t i
= 0; i
< size
; ++i
) {
183 APIPermission::ID id
;
184 if (!ReadParam(m
, iter
, &id
))
186 const APIPermissionInfo
* permission_info
=
187 extensions::PermissionsInfo::GetInstance()->GetByID(id
);
188 if (!permission_info
)
190 scoped_ptr
<APIPermission
> p(permission_info
->CreateAPIPermission());
191 if (!p
->Read(m
, iter
))
193 r
->insert(p
.release());
198 void ParamTraits
<APIPermissionSet
>::Log(
199 const param_type
& p
, std::string
* l
) {
200 LogParam(p
.map(), l
);
203 void ParamTraits
<ManifestPermissionSet
>::Write(
204 Message
* m
, const param_type
& p
) {
205 ManifestPermissionSet::const_iterator it
= p
.begin();
206 const ManifestPermissionSet::const_iterator end
= p
.end();
207 WriteParam(m
, p
.size());
208 for (; it
!= end
; ++it
) {
209 WriteParam(m
, it
->name());
214 bool ParamTraits
<ManifestPermissionSet
>::Read(
215 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
217 if (!ReadParam(m
, iter
, &size
))
219 for (size_t i
= 0; i
< size
; ++i
) {
221 if (!ReadParam(m
, iter
, &name
))
223 scoped_ptr
<ManifestPermission
> p(ManifestHandler::CreatePermission(name
));
226 if (!p
->Read(m
, iter
))
228 r
->insert(p
.release());
233 void ParamTraits
<ManifestPermissionSet
>::Log(
234 const param_type
& p
, std::string
* l
) {
235 LogParam(p
.map(), l
);
238 void ParamTraits
<ExtensionMsg_PermissionSetStruct
>::Write(Message
* m
,
239 const param_type
& p
) {
240 WriteParam(m
, p
.apis
);
241 WriteParam(m
, p
.manifest_permissions
);
242 WriteParam(m
, p
.explicit_hosts
);
243 WriteParam(m
, p
.scriptable_hosts
);
246 bool ParamTraits
<ExtensionMsg_PermissionSetStruct
>::Read(const Message
* m
,
247 PickleIterator
* iter
,
249 return ReadParam(m
, iter
, &p
->apis
) &&
250 ReadParam(m
, iter
, &p
->manifest_permissions
) &&
251 ReadParam(m
, iter
, &p
->explicit_hosts
) &&
252 ReadParam(m
, iter
, &p
->scriptable_hosts
);
255 void ParamTraits
<ExtensionMsg_PermissionSetStruct
>::Log(const param_type
& p
,
258 LogParam(p
.manifest_permissions
, l
);
259 LogParam(p
.explicit_hosts
, l
);
260 LogParam(p
.scriptable_hosts
, l
);
263 void ParamTraits
<ExtensionMsg_Loaded_Params
>::Write(Message
* m
,
264 const param_type
& p
) {
265 WriteParam(m
, p
.location
);
266 WriteParam(m
, p
.path
);
267 WriteParam(m
, *(p
.manifest
));
268 WriteParam(m
, p
.creation_flags
);
269 WriteParam(m
, p
.active_permissions
);
270 WriteParam(m
, p
.withheld_permissions
);
273 bool ParamTraits
<ExtensionMsg_Loaded_Params
>::Read(const Message
* m
,
274 PickleIterator
* iter
,
276 p
->manifest
.reset(new base::DictionaryValue());
277 return ReadParam(m
, iter
, &p
->location
) && ReadParam(m
, iter
, &p
->path
) &&
278 ReadParam(m
, iter
, p
->manifest
.get()) &&
279 ReadParam(m
, iter
, &p
->creation_flags
) &&
280 ReadParam(m
, iter
, &p
->active_permissions
) &&
281 ReadParam(m
, iter
, &p
->withheld_permissions
);
284 void ParamTraits
<ExtensionMsg_Loaded_Params
>::Log(const param_type
& p
,