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

Side by Side Diff: chrome/browser/extensions/api/context_menus/context_menus_api_helpers.h

Issue 186213003: <webview>: Context menu API implementation CL. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Make {extension_id,webview_instance_id} key for MenuManager items. Created 6 years, 9 months 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 // Definition of helper functions for the ContextMenus API. 5 // Definition of helper functions for the ContextMenus API.
6 6
7 #ifndef CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_ 7 #ifndef CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_
8 #define CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_ 8 #define CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS_H_
9 9
10 #include "chrome/browser/extensions/menu_manager.h" 10 #include "chrome/browser/extensions/menu_manager.h"
11 #include "chrome/browser/profiles/profile.h" 11 #include "chrome/browser/profiles/profile.h"
12 #include "extensions/common/error_utils.h" 12 #include "extensions/common/error_utils.h"
13 #include "extensions/common/manifest_handlers/background_info.h" 13 #include "extensions/common/manifest_handlers/background_info.h"
14 14
15 namespace extensions { 15 namespace extensions {
16 namespace context_menus_api_helpers { 16 namespace context_menus_api_helpers {
17 17
18 namespace { 18 namespace {
19 19
20 template<typename PropertyWithEnumT> 20 template <typename PropertyWithEnumT>
21 scoped_ptr<extensions::MenuItem::Id> GetParentId( 21 scoped_ptr<extensions::MenuItem::Id> GetParentId(
22 const PropertyWithEnumT& property, 22 const PropertyWithEnumT& property,
23 bool is_off_the_record, 23 bool is_off_the_record,
24 std::string extension_id) { 24 const MenuItem::ExtensionKey& key) {
25 if (!property.parent_id) 25 if (!property.parent_id)
26 return scoped_ptr<extensions::MenuItem::Id>(); 26 return scoped_ptr<extensions::MenuItem::Id>();
27 27
28 scoped_ptr<extensions::MenuItem::Id> parent_id( 28 scoped_ptr<extensions::MenuItem::Id> parent_id(
29 new extensions::MenuItem::Id(is_off_the_record, extension_id)); 29 new extensions::MenuItem::Id(is_off_the_record, key.extension_id));
30 if (property.parent_id->as_integer) 30 if (property.parent_id->as_integer)
31 parent_id->uid = *property.parent_id->as_integer; 31 parent_id->uid = *property.parent_id->as_integer;
32 else if (property.parent_id->as_string) 32 else if (property.parent_id->as_string)
33 parent_id->string_uid = *property.parent_id->as_string; 33 parent_id->string_uid = *property.parent_id->as_string;
34 else 34 else
35 NOTREACHED(); 35 NOTREACHED();
36
37 if (key.webview_instance_id)
38 parent_id->webview_instance_id = key.webview_instance_id;
39
36 return parent_id.Pass(); 40 return parent_id.Pass();
37 } 41 }
38 42
39 } // namespace 43 } // namespace
40 44
41 extern const char kCannotFindItemError[]; 45 extern const char kCannotFindItemError[];
42 extern const char kCheckedError[]; 46 extern const char kCheckedError[];
43 extern const char kDuplicateIDError[]; 47 extern const char kDuplicateIDError[];
44 extern const char kGeneratedIdKey[]; 48 extern const char kGeneratedIdKey[];
45 extern const char kLauncherNotAllowedError[]; 49 extern const char kLauncherNotAllowedError[];
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 case PropertyWithEnumT::CONTEXTS_TYPE_VIDEO: 83 case PropertyWithEnumT::CONTEXTS_TYPE_VIDEO:
80 contexts.Add(extensions::MenuItem::VIDEO); 84 contexts.Add(extensions::MenuItem::VIDEO);
81 break; 85 break;
82 case PropertyWithEnumT::CONTEXTS_TYPE_AUDIO: 86 case PropertyWithEnumT::CONTEXTS_TYPE_AUDIO:
83 contexts.Add(extensions::MenuItem::AUDIO); 87 contexts.Add(extensions::MenuItem::AUDIO);
84 break; 88 break;
85 case PropertyWithEnumT::CONTEXTS_TYPE_FRAME: 89 case PropertyWithEnumT::CONTEXTS_TYPE_FRAME:
86 contexts.Add(extensions::MenuItem::FRAME); 90 contexts.Add(extensions::MenuItem::FRAME);
87 break; 91 break;
88 case PropertyWithEnumT::CONTEXTS_TYPE_LAUNCHER: 92 case PropertyWithEnumT::CONTEXTS_TYPE_LAUNCHER:
93 // Not available for <webview>.
89 contexts.Add(extensions::MenuItem::LAUNCHER); 94 contexts.Add(extensions::MenuItem::LAUNCHER);
90 break; 95 break;
91 case PropertyWithEnumT::CONTEXTS_TYPE_NONE: 96 case PropertyWithEnumT::CONTEXTS_TYPE_NONE:
92 NOTREACHED(); 97 NOTREACHED();
93 } 98 }
94 } 99 }
95 return contexts; 100 return contexts;
96 } 101 }
97 102
98 template<typename PropertyWithEnumT> 103 template<typename PropertyWithEnumT>
(...skipping 14 matching lines...) Expand all
113 return extensions::MenuItem::NORMAL; 118 return extensions::MenuItem::NORMAL;
114 } 119 }
115 120
116 // Creates and adds a menu item from |create_properties|. 121 // Creates and adds a menu item from |create_properties|.
117 template<typename PropertyWithEnumT> 122 template<typename PropertyWithEnumT>
118 bool CreateMenuItem(const PropertyWithEnumT& create_properties, 123 bool CreateMenuItem(const PropertyWithEnumT& create_properties,
119 Profile* profile, 124 Profile* profile,
120 const Extension* extension, 125 const Extension* extension,
121 const MenuItem::Id& item_id, 126 const MenuItem::Id& item_id,
122 std::string* error) { 127 std::string* error) {
128 bool is_webview = item_id.webview_instance_id != 0;
123 MenuManager* menu_manager = MenuManager::Get(profile); 129 MenuManager* menu_manager = MenuManager::Get(profile);
124 130
125 if (menu_manager->GetItemById(item_id)) { 131 if (menu_manager->GetItemById(item_id)) {
126 *error = ErrorUtils::FormatErrorMessage(kDuplicateIDError, 132 *error = ErrorUtils::FormatErrorMessage(kDuplicateIDError,
127 GetIDString(item_id)); 133 GetIDString(item_id));
128 return false; 134 return false;
129 } 135 }
130 136
131 if (BackgroundInfo::HasLazyBackgroundPage(extension) && 137 if (!is_webview && BackgroundInfo::HasLazyBackgroundPage(extension) &&
132 create_properties.onclick.get()) { 138 create_properties.onclick.get()) {
133 *error = kOnclickDisallowedError; 139 *error = kOnclickDisallowedError;
134 return false; 140 return false;
135 } 141 }
136 142
137 // Contexts. 143 // Contexts.
138 MenuItem::ContextList contexts; 144 MenuItem::ContextList contexts;
139 if (create_properties.contexts.get()) 145 if (create_properties.contexts.get())
140 contexts = GetContexts(create_properties); 146 contexts = GetContexts(create_properties);
141 else 147 else
142 contexts.Add(MenuItem::PAGE); 148 contexts.Add(MenuItem::PAGE);
143 149
144 if (contexts.Contains(MenuItem::LAUNCHER) && !extension->is_platform_app()) { 150 if (contexts.Contains(MenuItem::LAUNCHER)) {
145 *error = kLauncherNotAllowedError; 151 // Launcher item is not allowed for <webview>.
146 return false; 152 if (!extension->is_platform_app() || is_webview) {
153 *error = kLauncherNotAllowedError;
154 return false;
155 }
147 } 156 }
148 157
149 // Title. 158 // Title.
150 std::string title; 159 std::string title;
151 if (create_properties.title.get()) 160 if (create_properties.title.get())
152 title = *create_properties.title; 161 title = *create_properties.title;
153 162
154 MenuItem::Type type = GetType(create_properties, MenuItem::NORMAL); 163 MenuItem::Type type = GetType(create_properties, MenuItem::NORMAL);
155 if (title.empty() && type != MenuItem::SEPARATOR) { 164 if (title.empty() && type != MenuItem::SEPARATOR) {
156 *error = kTitleNeededError; 165 *error = kTitleNeededError;
(...skipping 16 matching lines...) Expand all
173 // URL Patterns. 182 // URL Patterns.
174 if (!item->PopulateURLPatterns( 183 if (!item->PopulateURLPatterns(
175 create_properties.document_url_patterns.get(), 184 create_properties.document_url_patterns.get(),
176 create_properties.target_url_patterns.get(), 185 create_properties.target_url_patterns.get(),
177 error)) { 186 error)) {
178 return false; 187 return false;
179 } 188 }
180 189
181 // Parent id. 190 // Parent id.
182 bool success = true; 191 bool success = true;
183 scoped_ptr<MenuItem::Id> parent_id(GetParentId(create_properties, 192 const MenuItem::ExtensionKey key(extension->id(),
184 profile->IsOffTheRecord(), 193 item_id.webview_instance_id);
185 extension->id())); 194 scoped_ptr<MenuItem::Id> parent_id(
195 GetParentId(create_properties, profile->IsOffTheRecord(), key));
186 if (parent_id.get()) { 196 if (parent_id.get()) {
187 MenuItem* parent = GetParent(*parent_id, menu_manager, error); 197 MenuItem* parent = GetParent(*parent_id, menu_manager, error);
188 if (!parent) 198 if (!parent)
189 return false; 199 return false;
190 success = menu_manager->AddChildItem(parent->id(), item.release()); 200 success = menu_manager->AddChildItem(parent->id(), item.release());
191 } else { 201 } else {
192 success = menu_manager->AddContextItem(extension, item.release()); 202 success = menu_manager->AddContextItem(extension, item.release());
193 } 203 }
194 204
195 if (!success) 205 if (!success)
196 return false; 206 return false;
197 207
198 menu_manager->WriteToStorage(extension); 208 menu_manager->WriteToStorage(extension);
199 return true; 209 return true;
200 } 210 }
201 211
202 // Updates a menu item from |update_properties|. 212 // Updates a menu item from |update_properties|.
203 template<typename PropertyWithEnumT> 213 template<typename PropertyWithEnumT>
204 bool UpdateMenuItem(const PropertyWithEnumT& update_properties, 214 bool UpdateMenuItem(const PropertyWithEnumT& update_properties,
205 Profile* profile, 215 Profile* profile,
206 const Extension* extension, 216 const Extension* extension,
207 const MenuItem::Id& item_id, 217 const MenuItem::Id& item_id,
208 std::string* error) { 218 std::string* error) {
209 bool radio_item_updated = false; 219 bool radio_item_updated = false;
220 bool is_webview = item_id.webview_instance_id != 0;
210 MenuManager* menu_manager = MenuManager::Get(profile); 221 MenuManager* menu_manager = MenuManager::Get(profile);
211 222
212 MenuItem* item = menu_manager->GetItemById(item_id); 223 MenuItem* item = menu_manager->GetItemById(item_id);
213 if (!item || item->extension_id() != extension->id()){ 224 if (!item || item->extension_id() != extension->id()){
214 *error = ErrorUtils::FormatErrorMessage( 225 *error = ErrorUtils::FormatErrorMessage(
215 kCannotFindItemError, GetIDString(item_id)); 226 kCannotFindItemError, GetIDString(item_id));
216 return false; 227 return false;
217 } 228 }
218 229
219 // Type. 230 // Type.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 266
256 // Enabled. 267 // Enabled.
257 if (update_properties.enabled.get()) 268 if (update_properties.enabled.get())
258 item->set_enabled(*update_properties.enabled); 269 item->set_enabled(*update_properties.enabled);
259 270
260 // Contexts. 271 // Contexts.
261 MenuItem::ContextList contexts; 272 MenuItem::ContextList contexts;
262 if (update_properties.contexts.get()) { 273 if (update_properties.contexts.get()) {
263 contexts = GetContexts(update_properties); 274 contexts = GetContexts(update_properties);
264 275
265 if (contexts.Contains(MenuItem::LAUNCHER) && 276 if (contexts.Contains(MenuItem::LAUNCHER)) {
266 !extension->is_platform_app()) { 277 // Launcher item is not allowed for <webview>.
267 *error = kLauncherNotAllowedError; 278 if (!extension->is_platform_app() || is_webview) {
268 return false; 279 *error = kLauncherNotAllowedError;
280 return false;
281 }
269 } 282 }
270 283
271 if (contexts != item->contexts()) 284 if (contexts != item->contexts())
272 item->set_contexts(contexts); 285 item->set_contexts(contexts);
273 } 286 }
274 287
275 // Parent id. 288 // Parent id.
276 MenuItem* parent = NULL; 289 MenuItem* parent = NULL;
277 scoped_ptr<MenuItem::Id> parent_id(GetParentId(update_properties, 290 const MenuItem::ExtensionKey key(extension->id(),
278 profile->IsOffTheRecord(), 291 item_id.webview_instance_id);
279 extension->id())); 292 scoped_ptr<MenuItem::Id> parent_id(
293 GetParentId(update_properties, profile->IsOffTheRecord(), key));
280 if (parent_id.get()) { 294 if (parent_id.get()) {
281 MenuItem* parent = GetParent(*parent_id, menu_manager, error); 295 MenuItem* parent = GetParent(*parent_id, menu_manager, error);
282 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id())) 296 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id()))
283 return false; 297 return false;
284 } 298 }
285 299
286 // URL Patterns. 300 // URL Patterns.
287 if (!item->PopulateURLPatterns( 301 if (!item->PopulateURLPatterns(
288 update_properties.document_url_patterns.get(), 302 update_properties.document_url_patterns.get(),
289 update_properties.target_url_patterns.get(), error)) { 303 update_properties.target_url_patterns.get(), error)) {
290 return false; 304 return false;
291 } 305 }
292 306
293 // There is no need to call ItemUpdated if ChangeParent is called because 307 // There is no need to call ItemUpdated if ChangeParent is called because
294 // all sanitation is taken care of in ChangeParent. 308 // all sanitation is taken care of in ChangeParent.
295 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id())) 309 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id()))
296 return false; 310 return false;
297 311
298 menu_manager->WriteToStorage(extension); 312 menu_manager->WriteToStorage(extension);
299 return true; 313 return true;
300 } 314 }
301 315
302 } // namespace context_menus_api_helpers 316 } // namespace context_menus_api_helpers
303 } // namespace extensions 317 } // namespace extensions
304 318
305 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS _H_ 319 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698