OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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/common/extensions/permissions/media_galleries_permission.h" | |
6 | |
7 #include <set> | |
8 #include <string> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "extensions/common/permissions/permissions_info.h" | |
13 #include "grit/generated_resources.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 | |
16 namespace { | |
17 | |
18 // copyTo permission requires delete permission as a prerequisite. | |
19 // delete permission requires read permission as a prerequisite. | |
20 bool IsValidPermissionSet(bool has_read, bool has_copy_to, bool has_delete, | |
21 std::string* error) { | |
22 if (has_copy_to) { | |
23 if (has_read && has_delete) | |
24 return true; | |
25 if (error) | |
26 *error = "copyTo permission requires read and delete permissions"; | |
27 return false; | |
28 } | |
29 if (has_delete) { | |
30 if (has_read) | |
31 return true; | |
32 if (error) | |
33 *error = "delete permission requires read permission"; | |
34 return false; | |
35 } | |
36 return true; | |
37 } | |
38 | |
39 } // namespace | |
40 | |
41 namespace extensions { | |
42 | |
43 const char MediaGalleriesPermission::kAllAutoDetectedPermission[] = | |
44 "allAutoDetected"; | |
45 const char MediaGalleriesPermission::kScanPermission[] = "scan"; | |
46 const char MediaGalleriesPermission::kReadPermission[] = "read"; | |
47 const char MediaGalleriesPermission::kCopyToPermission[] = "copyTo"; | |
48 const char MediaGalleriesPermission::kDeletePermission[] = "delete"; | |
49 | |
50 MediaGalleriesPermission::MediaGalleriesPermission( | |
51 const APIPermissionInfo* info) | |
52 : SetDisjunctionPermission<MediaGalleriesPermissionData, | |
53 MediaGalleriesPermission>(info) { | |
54 } | |
55 | |
56 MediaGalleriesPermission::~MediaGalleriesPermission() { | |
57 } | |
58 | |
59 bool MediaGalleriesPermission::FromValue(const base::Value* value, | |
60 std::string* error) { | |
61 if (!SetDisjunctionPermission<MediaGalleriesPermissionData, | |
62 MediaGalleriesPermission>::FromValue(value, | |
63 error)) { | |
64 return false; | |
65 } | |
66 | |
67 bool has_read = false; | |
68 bool has_copy_to = false; | |
69 bool has_delete = false; | |
70 for (std::set<MediaGalleriesPermissionData>::const_iterator it = | |
71 data_set_.begin(); it != data_set_.end(); ++it) { | |
72 if (it->permission() == kAllAutoDetectedPermission || | |
73 it->permission() == kScanPermission) { | |
74 continue; | |
75 } | |
76 if (it->permission() == kReadPermission) { | |
77 has_read = true; | |
78 continue; | |
79 } | |
80 if (it->permission() == kCopyToPermission) { | |
81 has_copy_to = true; | |
82 continue; | |
83 } | |
84 if (it->permission() == kDeletePermission) { | |
85 has_delete = true; | |
86 continue; | |
87 } | |
88 | |
89 // No other permissions, so reaching this means | |
90 // MediaGalleriesPermissionData is probably out of sync in some way. | |
91 // Fail so developers notice this. | |
92 NOTREACHED(); | |
93 return false; | |
94 } | |
95 | |
96 return IsValidPermissionSet(has_read, has_copy_to, has_delete, error); | |
97 } | |
98 | |
99 PermissionMessages MediaGalleriesPermission::GetMessages() const { | |
100 DCHECK(HasMessages()); | |
101 PermissionMessages result; | |
102 | |
103 bool has_all_auto_detected = false; | |
104 bool has_read = false; | |
105 bool has_copy_to = false; | |
106 bool has_delete = false; | |
107 | |
108 for (std::set<MediaGalleriesPermissionData>::const_iterator it = | |
109 data_set_.begin(); it != data_set_.end(); ++it) { | |
110 if (it->permission() == kAllAutoDetectedPermission) | |
111 has_all_auto_detected = true; | |
112 else if (it->permission() == kReadPermission) | |
113 has_read = true; | |
114 else if (it->permission() == kCopyToPermission) | |
115 has_copy_to = true; | |
116 else if (it->permission() == kDeletePermission) | |
117 has_delete = true; | |
118 } | |
119 | |
120 if (!IsValidPermissionSet(has_read, has_copy_to, has_delete, NULL)) { | |
121 NOTREACHED(); | |
122 return result; | |
123 } | |
124 | |
125 // If |has_all_auto_detected| is false, then Chrome will prompt the user at | |
126 // runtime when the extension call the getMediaGalleries API. | |
127 if (!has_all_auto_detected) | |
128 return result; | |
129 // No access permission case. | |
130 if (!has_read) | |
131 return result; | |
132 | |
133 // Separate PermissionMessage IDs for read, copyTo, and delete. Otherwise an | |
134 // extension can silently gain new access capabilities. | |
135 result.push_back(PermissionMessage( | |
136 PermissionMessage::kMediaGalleriesAllGalleriesRead, | |
137 l10n_util::GetStringUTF16( | |
138 IDS_EXTENSION_PROMPT_WARNING_MEDIA_GALLERIES_READ))); | |
139 | |
140 // For copyTo and delete, the proper combined permission message will be | |
141 // derived in ChromePermissionMessageProvider::GetWarningMessages(), such | |
142 // that the user get 1 entry for all media galleries access permissions, | |
143 // rather than several separate entries. | |
144 if (has_copy_to) { | |
145 result.push_back(PermissionMessage( | |
146 PermissionMessage::kMediaGalleriesAllGalleriesCopyTo, | |
147 base::string16())); | |
148 } | |
149 if (has_delete) { | |
150 result.push_back(PermissionMessage( | |
151 PermissionMessage::kMediaGalleriesAllGalleriesDelete, | |
152 base::string16())); | |
153 } | |
154 return result; | |
155 } | |
156 | |
157 } // namespace extensions | |
OLD | NEW |