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

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: merge Created 8 years, 11 months 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) 2012 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 extensions {
14 namespace permissions_api {
15
16 namespace {
17
18 const char kInvalidOrigin[] =
19 "Invalid value for origin pattern *: *";
20 const char kUnknownPermissionError[] =
21 "'*' is not a recognized permission.";
22
23 const char kApisKey[] = "permissions";
24 const char kOriginsKey[] = "origins";
25
26 } // namespace
27
28 DictionaryValue* PackPermissionsToValue(const ExtensionPermissionSet* set) {
29 DictionaryValue* value = new DictionaryValue();
30
31 // Generate the list of API permissions.
32 ListValue* apis = new ListValue();
33 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
34 for (ExtensionAPIPermissionSet::const_iterator i = set->apis().begin();
35 i != set->apis().end(); ++i)
36 apis->Append(Value::CreateStringValue(info->GetByID(*i)->name()));
37
38 // Generate the list of origin permissions.
39 URLPatternSet hosts = set->explicit_hosts();
40 ListValue* origins = new ListValue();
41 for (URLPatternSet::const_iterator i = hosts.begin(); i != hosts.end(); ++i)
42 origins->Append(Value::CreateStringValue(i->GetAsString()));
43
44 value->Set(kApisKey, apis);
45 value->Set(kOriginsKey, origins);
46 return value;
47 }
48
49 // Creates a new ExtensionPermissionSet from its |value| and passes ownership to
50 // the caller through |ptr|. Sets |bad_message| to true if the message is badly
51 // formed. Returns false if the method fails to unpack a permission set.
52 bool UnpackPermissionsFromValue(DictionaryValue* value,
53 scoped_refptr<ExtensionPermissionSet>* ptr,
54 bool* bad_message,
55 std::string* error) {
56 ExtensionPermissionsInfo* info = ExtensionPermissionsInfo::GetInstance();
57 ExtensionAPIPermissionSet apis;
58 if (value->HasKey(kApisKey)) {
59 ListValue* api_list = NULL;
60 if (!value->GetList(kApisKey, &api_list)) {
61 *bad_message = true;
62 return false;
63 }
64 for (size_t i = 0; i < api_list->GetSize(); ++i) {
65 std::string api_name;
66 if (!api_list->GetString(i, &api_name)) {
67 *bad_message = true;
68 return false;
69 }
70
71 ExtensionAPIPermission* permission = info->GetByName(api_name);
72 if (!permission) {
73 *error = ExtensionErrorUtils::FormatErrorMessage(
74 kUnknownPermissionError, api_name);
75 return false;
76 }
77 apis.insert(permission->id());
78 }
79 }
80
81 URLPatternSet origins;
82 if (value->HasKey(kOriginsKey)) {
83 ListValue* origin_list = NULL;
84 if (!value->GetList(kOriginsKey, &origin_list)) {
85 *bad_message = true;
86 return false;
87 }
88 for (size_t i = 0; i < origin_list->GetSize(); ++i) {
89 std::string pattern;
90 if (!origin_list->GetString(i, &pattern)) {
91 *bad_message = true;
92 return false;
93 }
94
95 URLPattern origin(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 permissions_api
113 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698