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_Loaded_Params::ExtensionMsg_Loaded_Params()
26 : location(Manifest::INVALID_LOCATION
),
27 creation_flags(Extension::NO_FLAGS
) {}
29 ExtensionMsg_Loaded_Params::~ExtensionMsg_Loaded_Params() {}
31 ExtensionMsg_Loaded_Params::ExtensionMsg_Loaded_Params(
32 const Extension
* extension
)
33 : manifest(extension
->manifest()->value()->DeepCopy()),
34 location(extension
->location()),
35 path(extension
->path()),
36 apis(extension
->permissions_data()->active_permissions()->apis()),
37 manifest_permissions(extension
->permissions_data()
38 ->active_permissions()
39 ->manifest_permissions()),
40 explicit_hosts(extension
->permissions_data()
41 ->active_permissions()
43 scriptable_hosts(extension
->permissions_data()
44 ->active_permissions()
45 ->scriptable_hosts()),
47 creation_flags(extension
->creation_flags()) {
50 scoped_refptr
<Extension
> ExtensionMsg_Loaded_Params::ConvertToExtension(
51 std::string
* error
) const {
52 scoped_refptr
<Extension
> extension
=
53 Extension::Create(path
, location
, *manifest
, creation_flags
, error
);
54 if (extension
.get()) {
55 extension
->permissions_data()->SetActivePermissions(new PermissionSet(
56 apis
, manifest_permissions
, explicit_hosts
, scriptable_hosts
));
64 struct ParamTraits
<Manifest::Location
> {
65 typedef Manifest::Location param_type
;
66 static void Write(Message
* m
, const param_type
& p
) {
67 int val
= static_cast<int>(p
);
70 static bool Read(const Message
* m
, PickleIterator
* iter
, param_type
* p
) {
72 if (!ReadParam(m
, iter
, &val
) ||
73 val
< Manifest::INVALID_LOCATION
||
74 val
>= Manifest::NUM_LOCATIONS
)
76 *p
= static_cast<param_type
>(val
);
79 static void Log(const param_type
& p
, std::string
* l
) {
80 ParamTraits
<int>::Log(static_cast<int>(p
), l
);
84 void ParamTraits
<URLPattern
>::Write(Message
* m
, const param_type
& p
) {
85 WriteParam(m
, p
.valid_schemes());
86 WriteParam(m
, p
.GetAsString());
89 bool ParamTraits
<URLPattern
>::Read(const Message
* m
, PickleIterator
* iter
,
93 if (!ReadParam(m
, iter
, &valid_schemes
) ||
94 !ReadParam(m
, iter
, &spec
))
97 // TODO(jstritar): We don't want the URLPattern to fail parsing when the
98 // scheme is invalid. Instead, the pattern should parse but it should not
99 // match the invalid patterns. We get around this by setting the valid
100 // schemes after parsing the pattern. Update these method calls once we can
101 // ignore scheme validation with URLPattern parse options. crbug.com/90544
102 p
->SetValidSchemes(URLPattern::SCHEME_ALL
);
103 URLPattern::ParseResult result
= p
->Parse(spec
);
104 p
->SetValidSchemes(valid_schemes
);
105 return URLPattern::PARSE_SUCCESS
== result
;
108 void ParamTraits
<URLPattern
>::Log(const param_type
& p
, std::string
* l
) {
109 LogParam(p
.GetAsString(), l
);
112 void ParamTraits
<URLPatternSet
>::Write(Message
* m
, const param_type
& p
) {
113 WriteParam(m
, p
.patterns());
116 bool ParamTraits
<URLPatternSet
>::Read(const Message
* m
, PickleIterator
* iter
,
118 std::set
<URLPattern
> patterns
;
119 if (!ReadParam(m
, iter
, &patterns
))
122 for (std::set
<URLPattern
>::iterator i
= patterns
.begin();
123 i
!= patterns
.end(); ++i
)
128 void ParamTraits
<URLPatternSet
>::Log(const param_type
& p
, std::string
* l
) {
129 LogParam(p
.patterns(), l
);
132 void ParamTraits
<APIPermission::ID
>::Write(
133 Message
* m
, const param_type
& p
) {
134 WriteParam(m
, static_cast<int>(p
));
137 bool ParamTraits
<APIPermission::ID
>::Read(
138 const Message
* m
, PickleIterator
* iter
, param_type
* p
) {
140 if (!ReadParam(m
, iter
, &api_id
))
143 *p
= static_cast<APIPermission::ID
>(api_id
);
147 void ParamTraits
<APIPermission::ID
>::Log(
148 const param_type
& p
, std::string
* l
) {
149 LogParam(static_cast<int>(p
), l
);
152 void ParamTraits
<APIPermissionSet
>::Write(
153 Message
* m
, const param_type
& p
) {
154 APIPermissionSet::const_iterator it
= p
.begin();
155 const APIPermissionSet::const_iterator end
= p
.end();
156 WriteParam(m
, p
.size());
157 for (; it
!= end
; ++it
) {
158 WriteParam(m
, it
->id());
163 bool ParamTraits
<APIPermissionSet
>::Read(
164 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
166 if (!ReadParam(m
, iter
, &size
))
168 for (size_t i
= 0; i
< size
; ++i
) {
169 APIPermission::ID id
;
170 if (!ReadParam(m
, iter
, &id
))
172 const APIPermissionInfo
* permission_info
=
173 extensions::PermissionsInfo::GetInstance()->GetByID(id
);
174 if (!permission_info
)
176 scoped_ptr
<APIPermission
> p(permission_info
->CreateAPIPermission());
177 if (!p
->Read(m
, iter
))
179 r
->insert(p
.release());
184 void ParamTraits
<APIPermissionSet
>::Log(
185 const param_type
& p
, std::string
* l
) {
186 LogParam(p
.map(), l
);
189 void ParamTraits
<ManifestPermissionSet
>::Write(
190 Message
* m
, const param_type
& p
) {
191 ManifestPermissionSet::const_iterator it
= p
.begin();
192 const ManifestPermissionSet::const_iterator end
= p
.end();
193 WriteParam(m
, p
.size());
194 for (; it
!= end
; ++it
) {
195 WriteParam(m
, it
->name());
200 bool ParamTraits
<ManifestPermissionSet
>::Read(
201 const Message
* m
, PickleIterator
* iter
, param_type
* r
) {
203 if (!ReadParam(m
, iter
, &size
))
205 for (size_t i
= 0; i
< size
; ++i
) {
207 if (!ReadParam(m
, iter
, &name
))
209 scoped_ptr
<ManifestPermission
> p(ManifestHandler::CreatePermission(name
));
212 if (!p
->Read(m
, iter
))
214 r
->insert(p
.release());
219 void ParamTraits
<ManifestPermissionSet
>::Log(
220 const param_type
& p
, std::string
* l
) {
221 LogParam(p
.map(), l
);
224 void ParamTraits
<ExtensionMsg_Loaded_Params
>::Write(Message
* m
,
225 const param_type
& p
) {
226 WriteParam(m
, p
.location
);
227 WriteParam(m
, p
.path
);
228 WriteParam(m
, *(p
.manifest
));
229 WriteParam(m
, p
.creation_flags
);
230 WriteParam(m
, p
.apis
);
231 WriteParam(m
, p
.explicit_hosts
);
232 WriteParam(m
, p
.scriptable_hosts
);
235 bool ParamTraits
<ExtensionMsg_Loaded_Params
>::Read(const Message
* m
,
236 PickleIterator
* iter
,
238 p
->manifest
.reset(new base::DictionaryValue());
239 return ReadParam(m
, iter
, &p
->location
) &&
240 ReadParam(m
, iter
, &p
->path
) &&
241 ReadParam(m
, iter
, p
->manifest
.get()) &&
242 ReadParam(m
, iter
, &p
->creation_flags
) &&
243 ReadParam(m
, iter
, &p
->apis
) &&
244 ReadParam(m
, iter
, &p
->explicit_hosts
) &&
245 ReadParam(m
, iter
, &p
->scriptable_hosts
);
248 void ParamTraits
<ExtensionMsg_Loaded_Params
>::Log(const param_type
& p
,