Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(396)

Side by Side Diff: chrome/browser/extensions/extension_permissions_api_helpers.cc

Issue 8493017: Cleanup extension permissions module. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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.
4
5 #include "chrome/browser/extensions/extension_permissions_api_helpers.h"
6
7 #include "base/values.h"
8 #include "chrome/common/extensions/extension.h"
9 #include "chrome/common/extensions/extension_error_utils.h"
10 #include "chrome/common/extensions/extension_permission_set.h"
11 #include "chrome/common/extensions/url_pattern_set.h"
12
13 namespace extension_permissions_api {
14
15 namespace {
16
17 const char kInvalidOrigin[] =
18 "Invalid value for origin pattern *: *";
19 const char kUnknownPermissionError[] =
20 "'*' is not a recognized permission.";
21
22 const char kApisKey[] = "permissions";
23 const char kOriginsKey[] = "origins";
24
25 } // namespace
26
27 DictionaryValue* PackPermissionsToValue(const ExtensionPermissionSet* set) {
28 DictionaryValue* value = new DictionaryValue();
29
30 // Generate the list of API permissions.
31 ListValue* apis = new ListValue();
32 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
33 for (ExtensionAPIPermissionSet::const_iterator i = set->apis().begin();
34 i != set->apis().end(); ++i)
35 apis->Append(Value::CreateStringValue(info->GetByID(*i)->name()));
36
37 // Generate the list of origin permissions.
38 URLPatternSet hosts = set->explicit_hosts();
39 ListValue* origins = new ListValue();
40 for (URLPatternSet::const_iterator i = hosts.begin(); i != hosts.end(); ++i)
41 origins->Append(Value::CreateStringValue(i->GetAsString()));
42
43 value->Set(kApisKey, apis);
44 value->Set(kOriginsKey, origins);
45 return value;
46 }
47
48 // Creates a new ExtensionPermissionSet from its |value| and passes ownership to
49 // the caller through |ptr|. Sets |bad_message| to true if the message is badly
50 // formed. Returns false if the method fails to unpack a permission set.
51 bool UnpackPermissionsFromValue(DictionaryValue* value,
52 scoped_refptr<ExtensionPermissionSet>* ptr,
53 bool* bad_message,
54 std::string* error) {
55 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
56 ExtensionAPIPermissionSet apis;
57 if (value->HasKey(kApisKey)) {
58 ListValue* api_list = NULL;
59 if (!value->GetList(kApisKey, &api_list)) {
60 *bad_message = true;
61 return false;
62 }
63 for (size_t i = 0; i < api_list->GetSize(); ++i) {
64 std::string api_name;
65 if (!api_list->GetString(i, &api_name)) {
66 *bad_message = true;
67 return false;
68 }
69
70 ExtensionAPIPermission* permission = info->GetByName(api_name);
71 if (!permission) {
72 *error = ExtensionErrorUtils::FormatErrorMessage(
73 kUnknownPermissionError, api_name);
74 return false;
75 }
76 apis.insert(permission->id());
77 }
78 }
79
80 URLPatternSet origins;
81 if (value->HasKey(kOriginsKey)) {
82 ListValue* origin_list = NULL;
83 if (!value->GetList(kOriginsKey, &origin_list)) {
84 *bad_message = true;
85 return false;
86 }
87 for (size_t i = 0; i < origin_list->GetSize(); ++i) {
88 std::string pattern;
89 if (!origin_list->GetString(i, &pattern)) {
90 *bad_message = true;
91 return false;
92 }
93
94 URLPattern origin(URLPattern::IGNORE_PORTS,
95 Extension::kValidHostPermissionSchemes);
96 URLPattern::ParseResult parse_result = origin.Parse(pattern);
97 if (URLPattern::PARSE_SUCCESS != parse_result) {
98 *error = ExtensionErrorUtils::FormatErrorMessage(
99 kInvalidOrigin,
100 pattern,
101 URLPattern::GetParseResultString(parse_result));
102 return false;
103 }
104 origins.AddPattern(origin);
105 }
106 }
107
108 *ptr = new ExtensionPermissionSet(apis, origins, URLPatternSet());
109 return true;
110 }
111
112 } // namespace extension_permissions_api
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698