Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / manifest_handlers / ui_overrides_handler.cc
blobe5da7fa594978d51b071000e1a6ff37fe6e72252
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 FromValue(const base::Value* value) override {
49 return value && value->GetAsBoolean(&override_bookmarks_ui_permission_);
52 scoped_ptr<base::Value> ToValue() const override {
53 return scoped_ptr<base::Value>(
54 new base::FundamentalValue(override_bookmarks_ui_permission_)).Pass();
57 ManifestPermission* Diff(const ManifestPermission* rhs) const override {
58 const ManifestPermissionImpl* other =
59 static_cast<const ManifestPermissionImpl*>(rhs);
61 return scoped_ptr<ManifestPermissionImpl>(new ManifestPermissionImpl(
62 override_bookmarks_ui_permission_ &&
63 !other->override_bookmarks_ui_permission_)).release();
66 ManifestPermission* Union(const ManifestPermission* rhs) const override {
67 const ManifestPermissionImpl* other =
68 static_cast<const ManifestPermissionImpl*>(rhs);
70 return scoped_ptr<ManifestPermissionImpl>(new ManifestPermissionImpl(
71 override_bookmarks_ui_permission_ ||
72 other->override_bookmarks_ui_permission_)).release();
75 ManifestPermission* Intersect(const ManifestPermission* rhs) const override {
76 const ManifestPermissionImpl* other =
77 static_cast<const ManifestPermissionImpl*>(rhs);
79 return scoped_ptr<ManifestPermissionImpl>(new ManifestPermissionImpl(
80 override_bookmarks_ui_permission_ &&
81 other->override_bookmarks_ui_permission_)).release();
84 private:
85 bool override_bookmarks_ui_permission_;
88 UIOverrides::UIOverrides() {}
90 UIOverrides::~UIOverrides() {}
92 const UIOverrides* UIOverrides::Get(const Extension* extension) {
93 return static_cast<UIOverrides*>(
94 extension->GetManifestData(manifest_keys::kUIOverride));
97 bool UIOverrides::RemovesBookmarkButton(const Extension* extension) {
98 const UIOverrides* ui_overrides = Get(extension);
99 return ui_overrides && ui_overrides->bookmarks_ui &&
100 ui_overrides->bookmarks_ui->remove_button &&
101 *ui_overrides->bookmarks_ui->remove_button;
104 bool UIOverrides::RemovesBookmarkShortcut(const Extension* extension) {
105 const UIOverrides* ui_overrides = Get(extension);
106 return ui_overrides && ui_overrides->bookmarks_ui &&
107 ui_overrides->bookmarks_ui->remove_bookmark_shortcut &&
108 *ui_overrides->bookmarks_ui->remove_bookmark_shortcut;
111 bool UIOverrides::RemovesBookmarkOpenPagesShortcut(const Extension* extension) {
112 const UIOverrides* ui_overrides = Get(extension);
113 return ui_overrides && ui_overrides->bookmarks_ui &&
114 ui_overrides->bookmarks_ui->remove_bookmark_open_pages_shortcut &&
115 *ui_overrides->bookmarks_ui->remove_bookmark_open_pages_shortcut;
118 UIOverridesHandler::UIOverridesHandler() {}
120 UIOverridesHandler::~UIOverridesHandler() {}
122 bool UIOverridesHandler::Parse(Extension* extension, base::string16* error) {
123 const base::Value* dict = NULL;
124 CHECK(extension->manifest()->Get(manifest_keys::kUIOverride, &dict));
125 scoped_ptr<ChromeUIOverrides> overrides(
126 ChromeUIOverrides::FromValue(*dict, error));
127 if (!overrides)
128 return false;
130 scoped_ptr<UIOverrides> info(new UIOverrides);
131 info->bookmarks_ui.swap(overrides->bookmarks_ui);
132 if (!info->bookmarks_ui) {
133 *error = ErrorUtils::FormatErrorMessageUTF16(
134 manifest_errors::kInvalidEmptyDictionary,
135 manifest_keys::kUIOverride);
136 return false;
138 info->manifest_permission.reset(new ManifestPermissionImpl(
139 info->bookmarks_ui.get() != NULL));
140 extension->SetManifestData(manifest_keys::kUIOverride, info.release());
141 return true;
144 bool UIOverridesHandler::Validate(const Extension* extension,
145 std::string* error,
146 std::vector<InstallWarning>* warnings) const {
147 const UIOverrides* ui_overrides = UIOverrides::Get(extension);
149 if (ui_overrides && ui_overrides->bookmarks_ui) {
150 if (!FeatureSwitch::enable_override_bookmarks_ui()->IsEnabled()) {
151 warnings->push_back(InstallWarning(
152 ErrorUtils::FormatErrorMessage(
153 manifest_errors::kUnrecognizedManifestProperty,
154 manifest_keys::kBookmarkUI,
155 manifest_keys::kUIOverride)));
159 return true;
162 ManifestPermission* UIOverridesHandler::CreatePermission() {
163 return new ManifestPermissionImpl(false);
166 ManifestPermission* UIOverridesHandler::CreateInitialRequiredPermission(
167 const Extension* extension) {
168 const UIOverrides* data = UIOverrides::Get(extension);
169 if (data)
170 return data->manifest_permission->Clone();
171 return NULL;
173 const std::vector<std::string> UIOverridesHandler::Keys() const {
174 return SingleKey(manifest_keys::kUIOverride);
177 } // namespace extensions