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