Delete chrome.mediaGalleriesPrivate because the functionality unique to it has since...
[chromium-blink-merge.git] / chrome / common / extensions / manifest_handlers / ui_overrides_handler.cc
blob84120a2cf43630351e03142c9dc4c5f4abdcd69d
1 // Copyright 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.
5 #include "chrome/common/extensions/manifest_handlers/ui_overrides_handler.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "extensions/common/error_utils.h"
13 #include "extensions/common/feature_switch.h"
14 #include "extensions/common/manifest_constants.h"
15 #include "extensions/common/permissions/manifest_permission.h"
16 #include "extensions/common/permissions/permissions_data.h"
17 #include "extensions/common/permissions/permissions_info.h"
18 #include "ipc/ipc_message.h"
19 #include "ui/base/l10n/l10n_util.h"
21 using extensions::api::manifest_types::ChromeUIOverrides;
23 namespace extensions {
25 // The manifest permission implementation supports a permission for overriding
26 // the bookmark UI.
27 class UIOverridesHandler::ManifestPermissionImpl : public ManifestPermission {
28 public:
29 explicit ManifestPermissionImpl(bool override_bookmarks_ui_permission)
30 : override_bookmarks_ui_permission_(override_bookmarks_ui_permission) {}
32 // extensions::ManifestPermission overrides.
33 std::string name() const override { return manifest_keys::kUIOverride; }
35 std::string id() const override { return name(); }
37 PermissionIDSet GetPermissions() const override {
38 PermissionIDSet permissions;
39 if (override_bookmarks_ui_permission_) {
40 // TODO(sashab): Add rule to ChromePermissionMessageProvider:
41 // kOverrideBookmarksUI ->
42 // IDS_EXTENSION_PROMPT_WARNING_OVERRIDE_BOOKMARKS_UI
43 permissions.insert(APIPermission::kOverrideBookmarksUI);
45 return permissions;
48 bool HasMessages() const override {
49 return override_bookmarks_ui_permission_;
52 PermissionMessages GetMessages() const override {
53 // When making changes to this function, be careful to modify
54 // GetPermissions() above to have the same behaviour.
55 PermissionMessages result;
56 if (override_bookmarks_ui_permission_) {
57 result.push_back(PermissionMessage(
58 PermissionMessage::kOverrideBookmarksUI,
59 l10n_util::GetStringUTF16(
60 IDS_EXTENSION_PROMPT_WARNING_OVERRIDE_BOOKMARKS_UI)));
62 return result;
65 bool FromValue(const base::Value* value) override {
66 return value && value->GetAsBoolean(&override_bookmarks_ui_permission_);
69 scoped_ptr<base::Value> ToValue() const override {
70 return scoped_ptr<base::Value>(
71 new base::FundamentalValue(override_bookmarks_ui_permission_)).Pass();
74 ManifestPermission* Diff(const ManifestPermission* rhs) const override {
75 const ManifestPermissionImpl* other =
76 static_cast<const ManifestPermissionImpl*>(rhs);
78 return scoped_ptr<ManifestPermissionImpl>(new ManifestPermissionImpl(
79 override_bookmarks_ui_permission_ &&
80 !other->override_bookmarks_ui_permission_)).release();
83 ManifestPermission* Union(const ManifestPermission* rhs) const override {
84 const ManifestPermissionImpl* other =
85 static_cast<const ManifestPermissionImpl*>(rhs);
87 return scoped_ptr<ManifestPermissionImpl>(new ManifestPermissionImpl(
88 override_bookmarks_ui_permission_ ||
89 other->override_bookmarks_ui_permission_)).release();
92 ManifestPermission* Intersect(const ManifestPermission* rhs) const override {
93 const ManifestPermissionImpl* other =
94 static_cast<const ManifestPermissionImpl*>(rhs);
96 return scoped_ptr<ManifestPermissionImpl>(new ManifestPermissionImpl(
97 override_bookmarks_ui_permission_ &&
98 other->override_bookmarks_ui_permission_)).release();
101 private:
102 bool override_bookmarks_ui_permission_;
105 UIOverrides::UIOverrides() {}
107 UIOverrides::~UIOverrides() {}
109 const UIOverrides* UIOverrides::Get(const Extension* extension) {
110 return static_cast<UIOverrides*>(
111 extension->GetManifestData(manifest_keys::kUIOverride));
114 bool UIOverrides::RemovesBookmarkButton(const Extension* extension) {
115 const UIOverrides* ui_overrides = Get(extension);
116 return ui_overrides && ui_overrides->bookmarks_ui &&
117 ui_overrides->bookmarks_ui->remove_button &&
118 *ui_overrides->bookmarks_ui->remove_button;
121 bool UIOverrides::RemovesBookmarkShortcut(const Extension* extension) {
122 const UIOverrides* ui_overrides = Get(extension);
123 return ui_overrides && ui_overrides->bookmarks_ui &&
124 ui_overrides->bookmarks_ui->remove_bookmark_shortcut &&
125 *ui_overrides->bookmarks_ui->remove_bookmark_shortcut;
128 bool UIOverrides::RemovesBookmarkOpenPagesShortcut(const Extension* extension) {
129 const UIOverrides* ui_overrides = Get(extension);
130 return ui_overrides && ui_overrides->bookmarks_ui &&
131 ui_overrides->bookmarks_ui->remove_bookmark_open_pages_shortcut &&
132 *ui_overrides->bookmarks_ui->remove_bookmark_open_pages_shortcut;
135 UIOverridesHandler::UIOverridesHandler() {}
137 UIOverridesHandler::~UIOverridesHandler() {}
139 bool UIOverridesHandler::Parse(Extension* extension, base::string16* error) {
140 const base::Value* dict = NULL;
141 CHECK(extension->manifest()->Get(manifest_keys::kUIOverride, &dict));
142 scoped_ptr<ChromeUIOverrides> overrides(
143 ChromeUIOverrides::FromValue(*dict, error));
144 if (!overrides)
145 return false;
147 scoped_ptr<UIOverrides> info(new UIOverrides);
148 info->bookmarks_ui.swap(overrides->bookmarks_ui);
149 if (!info->bookmarks_ui) {
150 *error = ErrorUtils::FormatErrorMessageUTF16(
151 manifest_errors::kInvalidEmptyDictionary,
152 manifest_keys::kUIOverride);
153 return false;
155 info->manifest_permission.reset(new ManifestPermissionImpl(
156 info->bookmarks_ui.get() != NULL));
157 extension->SetManifestData(manifest_keys::kUIOverride, info.release());
158 return true;
161 bool UIOverridesHandler::Validate(const Extension* extension,
162 std::string* error,
163 std::vector<InstallWarning>* warnings) const {
164 const UIOverrides* ui_overrides = UIOverrides::Get(extension);
166 if (ui_overrides && ui_overrides->bookmarks_ui) {
167 if (!FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled()) {
168 warnings->push_back(InstallWarning(
169 ErrorUtils::FormatErrorMessage(
170 manifest_errors::kUnrecognizedManifestProperty,
171 manifest_keys::kBookmarkUI,
172 manifest_keys::kUIOverride)));
176 return true;
179 ManifestPermission* UIOverridesHandler::CreatePermission() {
180 return new ManifestPermissionImpl(false);
183 ManifestPermission* UIOverridesHandler::CreateInitialRequiredPermission(
184 const Extension* extension) {
185 const UIOverrides* data = UIOverrides::Get(extension);
186 if (data)
187 return data->manifest_permission->Clone();
188 return NULL;
190 const std::vector<std::string> UIOverridesHandler::Keys() const {
191 return SingleKey(manifest_keys::kUIOverride);
194 } // namespace extensions