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

Side by Side Diff: extensions/common/permissions/media_galleries_permission.cc

Issue 795543002: Added PermissionIDSet to APIPermissions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@permissions_patch_1_static_initializer_fix
Patch Set: Rebase Created 6 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "extensions/common/permissions/media_galleries_permission.h" 5 #include "extensions/common/permissions/media_galleries_permission.h"
6 6
7 #include <set> 7 #include <set>
8 #include <string> 8 #include <string>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "extensions/common/permissions/permissions_info.h" 12 #include "extensions/common/permissions/permissions_info.h"
13 #include "grit/extensions_strings.h" 13 #include "grit/extensions_strings.h"
14 #include "ui/base/l10n/l10n_util.h" 14 #include "ui/base/l10n/l10n_util.h"
15 15
16 namespace extensions {
17
16 namespace { 18 namespace {
17 19
18 // copyTo permission requires delete permission as a prerequisite. 20 // copyTo permission requires delete permission as a prerequisite.
19 // delete permission requires read permission as a prerequisite. 21 // delete permission requires read permission as a prerequisite.
20 bool IsValidPermissionSet(bool has_read, bool has_copy_to, bool has_delete, 22 bool IsValidPermissionSet(bool has_read, bool has_copy_to, bool has_delete,
21 std::string* error) { 23 std::string* error) {
22 if (has_copy_to) { 24 if (has_copy_to) {
23 if (has_read && has_delete) 25 if (has_read && has_delete)
24 return true; 26 return true;
25 if (error) 27 if (error)
26 *error = "copyTo permission requires read and delete permissions"; 28 *error = "copyTo permission requires read and delete permissions";
27 return false; 29 return false;
28 } 30 }
29 if (has_delete) { 31 if (has_delete) {
30 if (has_read) 32 if (has_read)
31 return true; 33 return true;
32 if (error) 34 if (error)
33 *error = "delete permission requires read permission"; 35 *error = "delete permission requires read permission";
34 return false; 36 return false;
35 } 37 }
36 return true; 38 return true;
37 } 39 }
38 40
41 // Adds the permissions from the |data_set| to the permission lists that are
42 // not NULL. If NULL, that list is ignored.
43 void AddPermissionsToLists(
44 const std::set<MediaGalleriesPermissionData>& data_set,
45 PermissionIDSet* ids,
46 PermissionMessages* messages) {
47 // TODO(sashab): Once GetMessages() is deprecated, move this logic back into
48 // GetPermissions().
49 bool has_all_auto_detected = false;
50 bool has_read = false;
51 bool has_copy_to = false;
52 bool has_delete = false;
53
54 for (std::set<MediaGalleriesPermissionData>::const_iterator it =
55 data_set.begin();
56 it != data_set.end(); ++it) {
57 if (it->permission() ==
58 MediaGalleriesPermission::kAllAutoDetectedPermission)
59 has_all_auto_detected = true;
60 else if (it->permission() == MediaGalleriesPermission::kReadPermission)
61 has_read = true;
62 else if (it->permission() == MediaGalleriesPermission::kCopyToPermission)
63 has_copy_to = true;
64 else if (it->permission() == MediaGalleriesPermission::kDeletePermission)
65 has_delete = true;
66 }
67
68 if (!IsValidPermissionSet(has_read, has_copy_to, has_delete, NULL)) {
69 NOTREACHED();
70 return;
71 }
72
73 // If |has_all_auto_detected| is false, then Chrome will prompt the user at
74 // runtime when the extension call the getMediaGalleries API.
75 if (!has_all_auto_detected)
76 return;
77 // No access permission case.
78 if (!has_read)
79 return;
80
81 // Separate PermissionMessage IDs for read, copyTo, and delete. Otherwise an
82 // extension can silently gain new access capabilities.
83 if (messages) {
84 messages->push_back(PermissionMessage(
85 PermissionMessage::kMediaGalleriesAllGalleriesRead,
86 l10n_util::GetStringUTF16(
87 IDS_EXTENSION_PROMPT_WARNING_MEDIA_GALLERIES_READ)));
88 }
89 if (ids)
90 ids->insert(APIPermission::kMediaGalleriesAllGalleriesRead);
91
92 // For copyTo and delete, the proper combined permission message will be
93 // derived in ChromePermissionMessageProvider::GetWarningMessages(), such
94 // that the user get 1 entry for all media galleries access permissions,
95 // rather than several separate entries.
96 if (has_copy_to) {
97 if (messages) {
98 messages->push_back(PermissionMessage(
99 PermissionMessage::kMediaGalleriesAllGalleriesCopyTo,
100 base::string16()));
101 }
102 if (ids)
103 ids->insert(APIPermission::kMediaGalleriesAllGalleriesCopyTo);
104 }
105 if (has_delete) {
106 if (messages) {
107 messages->push_back(PermissionMessage(
108 PermissionMessage::kMediaGalleriesAllGalleriesDelete,
109 base::string16()));
110 }
111 if (ids)
112 ids->insert(APIPermission::kMediaGalleriesAllGalleriesDelete);
113 }
114 return;
115 }
116
39 } // namespace 117 } // namespace
40 118
41 namespace extensions {
42
43 const char MediaGalleriesPermission::kAllAutoDetectedPermission[] = 119 const char MediaGalleriesPermission::kAllAutoDetectedPermission[] =
44 "allAutoDetected"; 120 "allAutoDetected";
45 const char MediaGalleriesPermission::kScanPermission[] = "scan"; 121 const char MediaGalleriesPermission::kScanPermission[] = "scan";
46 const char MediaGalleriesPermission::kReadPermission[] = "read"; 122 const char MediaGalleriesPermission::kReadPermission[] = "read";
47 const char MediaGalleriesPermission::kCopyToPermission[] = "copyTo"; 123 const char MediaGalleriesPermission::kCopyToPermission[] = "copyTo";
48 const char MediaGalleriesPermission::kDeletePermission[] = "delete"; 124 const char MediaGalleriesPermission::kDeletePermission[] = "delete";
49 125
50 MediaGalleriesPermission::MediaGalleriesPermission( 126 MediaGalleriesPermission::MediaGalleriesPermission(
51 const APIPermissionInfo* info) 127 const APIPermissionInfo* info)
52 : SetDisjunctionPermission<MediaGalleriesPermissionData, 128 : SetDisjunctionPermission<MediaGalleriesPermissionData,
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // No other permissions, so reaching this means 179 // No other permissions, so reaching this means
104 // MediaGalleriesPermissionData is probably out of sync in some way. 180 // MediaGalleriesPermissionData is probably out of sync in some way.
105 // Fail so developers notice this. 181 // Fail so developers notice this.
106 NOTREACHED(); 182 NOTREACHED();
107 return false; 183 return false;
108 } 184 }
109 185
110 return IsValidPermissionSet(has_read, has_copy_to, has_delete, error); 186 return IsValidPermissionSet(has_read, has_copy_to, has_delete, error);
111 } 187 }
112 188
189 PermissionIDSet MediaGalleriesPermission::GetPermissions() const {
190 DCHECK(HasMessages());
191 PermissionIDSet result;
192 AddPermissionsToLists(data_set_, &result, NULL);
193 return result;
194 }
195
113 PermissionMessages MediaGalleriesPermission::GetMessages() const { 196 PermissionMessages MediaGalleriesPermission::GetMessages() const {
114 DCHECK(HasMessages()); 197 DCHECK(HasMessages());
115 PermissionMessages result; 198 PermissionMessages result;
116 199 AddPermissionsToLists(data_set_, NULL, &result);
117 bool has_all_auto_detected = false;
118 bool has_read = false;
119 bool has_copy_to = false;
120 bool has_delete = false;
121
122 for (std::set<MediaGalleriesPermissionData>::const_iterator it =
123 data_set_.begin(); it != data_set_.end(); ++it) {
124 if (it->permission() == kAllAutoDetectedPermission)
125 has_all_auto_detected = true;
126 else if (it->permission() == kReadPermission)
127 has_read = true;
128 else if (it->permission() == kCopyToPermission)
129 has_copy_to = true;
130 else if (it->permission() == kDeletePermission)
131 has_delete = true;
132 }
133
134 if (!IsValidPermissionSet(has_read, has_copy_to, has_delete, NULL)) {
135 NOTREACHED();
136 return result;
137 }
138
139 // If |has_all_auto_detected| is false, then Chrome will prompt the user at
140 // runtime when the extension call the getMediaGalleries API.
141 if (!has_all_auto_detected)
142 return result;
143 // No access permission case.
144 if (!has_read)
145 return result;
146
147 // Separate PermissionMessage IDs for read, copyTo, and delete. Otherwise an
148 // extension can silently gain new access capabilities.
149 result.push_back(PermissionMessage(
150 PermissionMessage::kMediaGalleriesAllGalleriesRead,
151 l10n_util::GetStringUTF16(
152 IDS_EXTENSION_PROMPT_WARNING_MEDIA_GALLERIES_READ)));
153
154 // For copyTo and delete, the proper combined permission message will be
155 // derived in ChromePermissionMessageProvider::GetWarningMessages(), such
156 // that the user get 1 entry for all media galleries access permissions,
157 // rather than several separate entries.
158 if (has_copy_to) {
159 result.push_back(PermissionMessage(
160 PermissionMessage::kMediaGalleriesAllGalleriesCopyTo,
161 base::string16()));
162 }
163 if (has_delete) {
164 result.push_back(PermissionMessage(
165 PermissionMessage::kMediaGalleriesAllGalleriesDelete,
166 base::string16()));
167 }
168 return result; 200 return result;
169 } 201 }
170 202
171 } // namespace extensions 203 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698