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

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: Put ExtensionKey into MenuItem::Id. 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>
Fady Samuel 2014/03/05 17:54:10 Why add this space?
lazyboy 2014/03/05 18:27:59 git cl format did this.
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));
Fady Samuel 2014/03/05 17:54:10 As before, this really should take in an extension
lazyboy 2014/03/05 18:27:59 Done.
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->extension_key.webview_instance_id = key.webview_instance_id;
Fady Samuel 2014/03/05 17:54:10 If MenuItem::Id took in an extension_key instead,
lazyboy 2014/03/05 18:27:59 Done.
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.extension_key.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 scoped_ptr<MenuItem::Id> parent_id(GetParentId(
184 profile->IsOffTheRecord(), 193 create_properties, profile->IsOffTheRecord(), item_id.extension_key));
185 extension->id()));
186 if (parent_id.get()) { 194 if (parent_id.get()) {
187 MenuItem* parent = GetParent(*parent_id, menu_manager, error); 195 MenuItem* parent = GetParent(*parent_id, menu_manager, error);
188 if (!parent) 196 if (!parent)
189 return false; 197 return false;
190 success = menu_manager->AddChildItem(parent->id(), item.release()); 198 success = menu_manager->AddChildItem(parent->id(), item.release());
191 } else { 199 } else {
192 success = menu_manager->AddContextItem(extension, item.release()); 200 success = menu_manager->AddContextItem(extension, item.release());
193 } 201 }
194 202
195 if (!success) 203 if (!success)
196 return false; 204 return false;
197 205
198 menu_manager->WriteToStorage(extension); 206 menu_manager->WriteToStorage(extension, item_id.extension_key);
199 return true; 207 return true;
200 } 208 }
201 209
202 // Updates a menu item from |update_properties|. 210 // Updates a menu item from |update_properties|.
203 template<typename PropertyWithEnumT> 211 template<typename PropertyWithEnumT>
204 bool UpdateMenuItem(const PropertyWithEnumT& update_properties, 212 bool UpdateMenuItem(const PropertyWithEnumT& update_properties,
205 Profile* profile, 213 Profile* profile,
206 const Extension* extension, 214 const Extension* extension,
207 const MenuItem::Id& item_id, 215 const MenuItem::Id& item_id,
208 std::string* error) { 216 std::string* error) {
209 bool radio_item_updated = false; 217 bool radio_item_updated = false;
218 bool is_webview = item_id.extension_key.webview_instance_id != 0;
210 MenuManager* menu_manager = MenuManager::Get(profile); 219 MenuManager* menu_manager = MenuManager::Get(profile);
211 220
212 MenuItem* item = menu_manager->GetItemById(item_id); 221 MenuItem* item = menu_manager->GetItemById(item_id);
213 if (!item || item->extension_id() != extension->id()){ 222 if (!item || item->extension_id() != extension->id()){
214 *error = ErrorUtils::FormatErrorMessage( 223 *error = ErrorUtils::FormatErrorMessage(
215 kCannotFindItemError, GetIDString(item_id)); 224 kCannotFindItemError, GetIDString(item_id));
216 return false; 225 return false;
217 } 226 }
218 227
219 // Type. 228 // Type.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
255 264
256 // Enabled. 265 // Enabled.
257 if (update_properties.enabled.get()) 266 if (update_properties.enabled.get())
258 item->set_enabled(*update_properties.enabled); 267 item->set_enabled(*update_properties.enabled);
259 268
260 // Contexts. 269 // Contexts.
261 MenuItem::ContextList contexts; 270 MenuItem::ContextList contexts;
262 if (update_properties.contexts.get()) { 271 if (update_properties.contexts.get()) {
263 contexts = GetContexts(update_properties); 272 contexts = GetContexts(update_properties);
264 273
265 if (contexts.Contains(MenuItem::LAUNCHER) && 274 if (contexts.Contains(MenuItem::LAUNCHER)) {
266 !extension->is_platform_app()) { 275 // Launcher item is not allowed for <webview>.
267 *error = kLauncherNotAllowedError; 276 if (!extension->is_platform_app() || is_webview) {
268 return false; 277 *error = kLauncherNotAllowedError;
278 return false;
279 }
269 } 280 }
270 281
271 if (contexts != item->contexts()) 282 if (contexts != item->contexts())
272 item->set_contexts(contexts); 283 item->set_contexts(contexts);
273 } 284 }
274 285
275 // Parent id. 286 // Parent id.
276 MenuItem* parent = NULL; 287 MenuItem* parent = NULL;
277 scoped_ptr<MenuItem::Id> parent_id(GetParentId(update_properties, 288 scoped_ptr<MenuItem::Id> parent_id(GetParentId(
278 profile->IsOffTheRecord(), 289 update_properties, profile->IsOffTheRecord(), item_id.extension_key));
279 extension->id()));
280 if (parent_id.get()) { 290 if (parent_id.get()) {
281 MenuItem* parent = GetParent(*parent_id, menu_manager, error); 291 MenuItem* parent = GetParent(*parent_id, menu_manager, error);
282 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id())) 292 if (!parent || !menu_manager->ChangeParent(item->id(), &parent->id()))
283 return false; 293 return false;
284 } 294 }
285 295
286 // URL Patterns. 296 // URL Patterns.
287 if (!item->PopulateURLPatterns( 297 if (!item->PopulateURLPatterns(
288 update_properties.document_url_patterns.get(), 298 update_properties.document_url_patterns.get(),
289 update_properties.target_url_patterns.get(), error)) { 299 update_properties.target_url_patterns.get(), error)) {
290 return false; 300 return false;
291 } 301 }
292 302
293 // There is no need to call ItemUpdated if ChangeParent is called because 303 // There is no need to call ItemUpdated if ChangeParent is called because
294 // all sanitation is taken care of in ChangeParent. 304 // all sanitation is taken care of in ChangeParent.
295 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id())) 305 if (!parent && radio_item_updated && !menu_manager->ItemUpdated(item->id()))
296 return false; 306 return false;
297 307
298 menu_manager->WriteToStorage(extension); 308 menu_manager->WriteToStorage(extension, item_id.extension_key);
299 return true; 309 return true;
300 } 310 }
301 311
302 } // namespace context_menus_api_helpers 312 } // namespace context_menus_api_helpers
303 } // namespace extensions 313 } // namespace extensions
304 314
305 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS _H_ 315 #endif // CHROME_BROWSER_EXTENSIONS_API_CONTEXT_MENUS_CONTEXT_MENUS_API_HELPERS _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698