Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / extensions / common / extension_messages.cc
blob42e25f4351d57e90fc187b20f20e68834203b0f2
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()),
58 withheld_permissions(
59 *extension->permissions_data()->withheld_permissions()),
60 id(extension->id()),
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());
86 return extension;
89 namespace IPC {
91 template <>
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);
96 WriteParam(m, val);
98 static bool Read(const Message* m,
99 base::PickleIterator* iter,
100 param_type* p) {
101 int val = 0;
102 if (!ReadParam(m, iter, &val) ||
103 val < Manifest::INVALID_LOCATION ||
104 val >= Manifest::NUM_LOCATIONS)
105 return false;
106 *p = static_cast<param_type>(val);
107 return true;
109 static void Log(const param_type& p, std::string* l) {
110 ParamTraits<int>::Log(static_cast<int>(p), l);
114 void ParamTraits<URLPattern>::Write(Message* m, const param_type& p) {
115 WriteParam(m, p.valid_schemes());
116 WriteParam(m, p.GetAsString());
119 bool ParamTraits<URLPattern>::Read(const Message* m,
120 base::PickleIterator* iter,
121 param_type* p) {
122 int valid_schemes;
123 std::string spec;
124 if (!ReadParam(m, iter, &valid_schemes) ||
125 !ReadParam(m, iter, &spec))
126 return false;
128 // TODO(jstritar): We don't want the URLPattern to fail parsing when the
129 // scheme is invalid. Instead, the pattern should parse but it should not
130 // match the invalid patterns. We get around this by setting the valid
131 // schemes after parsing the pattern. Update these method calls once we can
132 // ignore scheme validation with URLPattern parse options. crbug.com/90544
133 p->SetValidSchemes(URLPattern::SCHEME_ALL);
134 URLPattern::ParseResult result = p->Parse(spec);
135 p->SetValidSchemes(valid_schemes);
136 return URLPattern::PARSE_SUCCESS == result;
139 void ParamTraits<URLPattern>::Log(const param_type& p, std::string* l) {
140 LogParam(p.GetAsString(), l);
143 void ParamTraits<URLPatternSet>::Write(Message* m, const param_type& p) {
144 WriteParam(m, p.patterns());
147 bool ParamTraits<URLPatternSet>::Read(const Message* m,
148 base::PickleIterator* iter,
149 param_type* p) {
150 std::set<URLPattern> patterns;
151 if (!ReadParam(m, iter, &patterns))
152 return false;
154 for (std::set<URLPattern>::iterator i = patterns.begin();
155 i != patterns.end(); ++i)
156 p->AddPattern(*i);
157 return true;
160 void ParamTraits<URLPatternSet>::Log(const param_type& p, std::string* l) {
161 LogParam(p.patterns(), l);
164 void ParamTraits<APIPermission::ID>::Write(
165 Message* m, const param_type& p) {
166 WriteParam(m, static_cast<int>(p));
169 bool ParamTraits<APIPermission::ID>::Read(const Message* m,
170 base::PickleIterator* iter,
171 param_type* p) {
172 int api_id = -2;
173 if (!ReadParam(m, iter, &api_id))
174 return false;
176 *p = static_cast<APIPermission::ID>(api_id);
177 return true;
180 void ParamTraits<APIPermission::ID>::Log(
181 const param_type& p, std::string* l) {
182 LogParam(static_cast<int>(p), l);
185 void ParamTraits<APIPermissionSet>::Write(
186 Message* m, const param_type& p) {
187 APIPermissionSet::const_iterator it = p.begin();
188 const APIPermissionSet::const_iterator end = p.end();
189 WriteParam(m, p.size());
190 for (; it != end; ++it) {
191 WriteParam(m, it->id());
192 it->Write(m);
196 bool ParamTraits<APIPermissionSet>::Read(const Message* m,
197 base::PickleIterator* iter,
198 param_type* r) {
199 size_t size;
200 if (!ReadParam(m, iter, &size))
201 return false;
202 for (size_t i = 0; i < size; ++i) {
203 APIPermission::ID id;
204 if (!ReadParam(m, iter, &id))
205 return false;
206 const APIPermissionInfo* permission_info =
207 extensions::PermissionsInfo::GetInstance()->GetByID(id);
208 if (!permission_info)
209 return false;
210 scoped_ptr<APIPermission> p(permission_info->CreateAPIPermission());
211 if (!p->Read(m, iter))
212 return false;
213 r->insert(p.release());
215 return true;
218 void ParamTraits<APIPermissionSet>::Log(
219 const param_type& p, std::string* l) {
220 LogParam(p.map(), l);
223 void ParamTraits<ManifestPermissionSet>::Write(
224 Message* m, const param_type& p) {
225 ManifestPermissionSet::const_iterator it = p.begin();
226 const ManifestPermissionSet::const_iterator end = p.end();
227 WriteParam(m, p.size());
228 for (; it != end; ++it) {
229 WriteParam(m, it->name());
230 it->Write(m);
234 bool ParamTraits<ManifestPermissionSet>::Read(const Message* m,
235 base::PickleIterator* iter,
236 param_type* r) {
237 size_t size;
238 if (!ReadParam(m, iter, &size))
239 return false;
240 for (size_t i = 0; i < size; ++i) {
241 std::string name;
242 if (!ReadParam(m, iter, &name))
243 return false;
244 scoped_ptr<ManifestPermission> p(ManifestHandler::CreatePermission(name));
245 if (!p)
246 return false;
247 if (!p->Read(m, iter))
248 return false;
249 r->insert(p.release());
251 return true;
254 void ParamTraits<ManifestPermissionSet>::Log(
255 const param_type& p, std::string* l) {
256 LogParam(p.map(), l);
259 void ParamTraits<HostID>::Write(
260 Message* m, const param_type& p) {
261 WriteParam(m, p.type());
262 WriteParam(m, p.id());
265 bool ParamTraits<HostID>::Read(const Message* m,
266 base::PickleIterator* iter,
267 param_type* r) {
268 HostID::HostType type;
269 std::string id;
270 if (!ReadParam(m, iter, &type))
271 return false;
272 if (!ReadParam(m, iter, &id))
273 return false;
274 *r = HostID(type, id);
275 return true;
278 void ParamTraits<HostID>::Log(
279 const param_type& p, std::string* l) {
280 LogParam(p.type(), l);
281 LogParam(p.id(), l);
284 void ParamTraits<ExtensionMsg_PermissionSetStruct>::Write(Message* m,
285 const param_type& p) {
286 WriteParam(m, p.apis);
287 WriteParam(m, p.manifest_permissions);
288 WriteParam(m, p.explicit_hosts);
289 WriteParam(m, p.scriptable_hosts);
292 bool ParamTraits<ExtensionMsg_PermissionSetStruct>::Read(
293 const Message* m,
294 base::PickleIterator* iter,
295 param_type* p) {
296 return ReadParam(m, iter, &p->apis) &&
297 ReadParam(m, iter, &p->manifest_permissions) &&
298 ReadParam(m, iter, &p->explicit_hosts) &&
299 ReadParam(m, iter, &p->scriptable_hosts);
302 void ParamTraits<ExtensionMsg_PermissionSetStruct>::Log(const param_type& p,
303 std::string* l) {
304 LogParam(p.apis, l);
305 LogParam(p.manifest_permissions, l);
306 LogParam(p.explicit_hosts, l);
307 LogParam(p.scriptable_hosts, l);
310 void ParamTraits<ExtensionMsg_Loaded_Params>::Write(Message* m,
311 const param_type& p) {
312 WriteParam(m, p.location);
313 WriteParam(m, p.path);
314 WriteParam(m, *(p.manifest));
315 WriteParam(m, p.creation_flags);
316 WriteParam(m, p.active_permissions);
317 WriteParam(m, p.withheld_permissions);
320 bool ParamTraits<ExtensionMsg_Loaded_Params>::Read(const Message* m,
321 base::PickleIterator* iter,
322 param_type* p) {
323 p->manifest.reset(new base::DictionaryValue());
324 return ReadParam(m, iter, &p->location) && ReadParam(m, iter, &p->path) &&
325 ReadParam(m, iter, p->manifest.get()) &&
326 ReadParam(m, iter, &p->creation_flags) &&
327 ReadParam(m, iter, &p->active_permissions) &&
328 ReadParam(m, iter, &p->withheld_permissions);
331 void ParamTraits<ExtensionMsg_Loaded_Params>::Log(const param_type& p,
332 std::string* l) {
333 l->append(p.id);
336 } // namespace IPC