1 // Copyright (c) 2012 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/browser/extensions/menu_manager.h"
9 #include "base/json/json_writer.h"
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/extension_tab_util.h"
18 #include "chrome/browser/extensions/menu_manager_factory.h"
19 #include "chrome/browser/extensions/state_store.h"
20 #include "chrome/browser/extensions/tab_helper.h"
21 #include "chrome/browser/guest_view/web_view/web_view_guest.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/common/extensions/api/context_menus.h"
24 #include "chrome/common/extensions/api/webview.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/notification_source.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/common/context_menu_params.h"
30 #include "extensions/browser/event_router.h"
31 #include "extensions/browser/extension_registry.h"
32 #include "extensions/browser/extension_system.h"
33 #include "extensions/common/extension.h"
34 #include "extensions/common/manifest_handlers/background_info.h"
35 #include "ui/gfx/favicon_size.h"
36 #include "ui/gfx/text_elider.h"
38 using content::WebContents;
39 using extensions::ExtensionSystem;
41 namespace extensions {
43 namespace context_menus = api::context_menus;
44 namespace webview = api::webview;
48 // Keys for serialization to and from Value to store in the preferences.
49 const char kContextMenusKey[] = "context_menus";
51 const char kCheckedKey[] = "checked";
52 const char kContextsKey[] = "contexts";
53 const char kDocumentURLPatternsKey[] = "document_url_patterns";
54 const char kEnabledKey[] = "enabled";
55 const char kIncognitoKey[] = "incognito";
56 const char kParentUIDKey[] = "parent_uid";
57 const char kStringUIDKey[] = "string_uid";
58 const char kTargetURLPatternsKey[] = "target_url_patterns";
59 const char kTitleKey[] = "title";
60 const char kTypeKey[] = "type";
62 void SetIdKeyValue(base::DictionaryValue* properties,
64 const MenuItem::Id& id) {
66 properties->SetString(key, id.string_uid);
68 properties->SetInteger(key, id.uid);
71 MenuItem::List MenuItemsFromValue(const std::string& extension_id,
75 base::ListValue* list = NULL;
76 if (!value || !value->GetAsList(&list))
79 for (size_t i = 0; i < list->GetSize(); ++i) {
80 base::DictionaryValue* dict = NULL;
81 if (!list->GetDictionary(i, &dict))
83 MenuItem* item = MenuItem::Populate(
84 extension_id, *dict, NULL);
87 items.push_back(item);
92 scoped_ptr<base::Value> MenuItemsToValue(const MenuItem::List& items) {
93 scoped_ptr<base::ListValue> list(new base::ListValue());
94 for (size_t i = 0; i < items.size(); ++i)
95 list->Append(items[i]->ToValue().release());
96 return scoped_ptr<base::Value>(list.release());
99 bool GetStringList(const base::DictionaryValue& dict,
100 const std::string& key,
101 std::vector<std::string>* out) {
102 if (!dict.HasKey(key))
105 const base::ListValue* list = NULL;
106 if (!dict.GetListWithoutPathExpansion(key, &list))
109 for (size_t i = 0; i < list->GetSize(); ++i) {
111 if (!list->GetString(i, &pattern))
113 out->push_back(pattern);
121 MenuItem::MenuItem(const Id& id,
122 const std::string& title,
126 const ContextList& contexts)
132 contexts_(contexts) {}
134 MenuItem::~MenuItem() {
135 STLDeleteElements(&children_);
138 MenuItem* MenuItem::ReleaseChild(const Id& child_id,
140 for (List::iterator i = children_.begin(); i != children_.end(); ++i) {
141 MenuItem* child = NULL;
142 if ((*i)->id() == child_id) {
146 } else if (recursive) {
147 child = (*i)->ReleaseChild(child_id, recursive);
155 void MenuItem::GetFlattenedSubtree(MenuItem::List* list) {
156 list->push_back(this);
157 for (List::iterator i = children_.begin(); i != children_.end(); ++i)
158 (*i)->GetFlattenedSubtree(list);
161 std::set<MenuItem::Id> MenuItem::RemoveAllDescendants() {
163 for (List::iterator i = children_.begin(); i != children_.end(); ++i) {
164 MenuItem* child = *i;
165 result.insert(child->id());
166 std::set<Id> removed = child->RemoveAllDescendants();
167 result.insert(removed.begin(), removed.end());
169 STLDeleteElements(&children_);
173 base::string16 MenuItem::TitleWithReplacement(const base::string16& selection,
174 size_t max_length) const {
175 base::string16 result = base::UTF8ToUTF16(title_);
176 // TODO(asargent) - Change this to properly handle %% escaping so you can
177 // put "%s" in titles that won't get substituted.
178 ReplaceSubstringsAfterOffset(&result, 0, base::ASCIIToUTF16("%s"), selection);
180 if (result.length() > max_length)
181 result = gfx::TruncateString(result, max_length);
185 bool MenuItem::SetChecked(bool checked) {
186 if (type_ != CHECKBOX && type_ != RADIO)
192 void MenuItem::AddChild(MenuItem* item) {
193 item->parent_id_.reset(new Id(id_));
194 children_.push_back(item);
197 scoped_ptr<base::DictionaryValue> MenuItem::ToValue() const {
198 scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
199 // Should only be called for extensions with event pages, which only have
200 // string IDs for items.
201 DCHECK_EQ(0, id_.uid);
202 value->SetString(kStringUIDKey, id_.string_uid);
203 value->SetBoolean(kIncognitoKey, id_.incognito);
204 value->SetInteger(kTypeKey, type_);
205 if (type_ != SEPARATOR)
206 value->SetString(kTitleKey, title_);
207 if (type_ == CHECKBOX || type_ == RADIO)
208 value->SetBoolean(kCheckedKey, checked_);
209 value->SetBoolean(kEnabledKey, enabled_);
210 value->Set(kContextsKey, contexts_.ToValue().release());
212 DCHECK_EQ(0, parent_id_->uid);
213 value->SetString(kParentUIDKey, parent_id_->string_uid);
215 value->Set(kDocumentURLPatternsKey,
216 document_url_patterns_.ToValue().release());
217 value->Set(kTargetURLPatternsKey, target_url_patterns_.ToValue().release());
222 MenuItem* MenuItem::Populate(const std::string& extension_id,
223 const base::DictionaryValue& value,
224 std::string* error) {
225 bool incognito = false;
226 if (!value.GetBoolean(kIncognitoKey, &incognito))
228 Id id(incognito, MenuItem::ExtensionKey(extension_id));
229 if (!value.GetString(kStringUIDKey, &id.string_uid))
233 if (!value.GetInteger(kTypeKey, &type_int))
235 type = static_cast<Type>(type_int);
237 if (type != SEPARATOR && !value.GetString(kTitleKey, &title))
239 bool checked = false;
240 if ((type == CHECKBOX || type == RADIO) &&
241 !value.GetBoolean(kCheckedKey, &checked)) {
245 if (!value.GetBoolean(kEnabledKey, &enabled))
247 ContextList contexts;
248 const base::Value* contexts_value = NULL;
249 if (!value.Get(kContextsKey, &contexts_value))
251 if (!contexts.Populate(*contexts_value))
254 scoped_ptr<MenuItem> result(new MenuItem(
255 id, title, checked, enabled, type, contexts));
257 std::vector<std::string> document_url_patterns;
258 if (!GetStringList(value, kDocumentURLPatternsKey, &document_url_patterns))
260 std::vector<std::string> target_url_patterns;
261 if (!GetStringList(value, kTargetURLPatternsKey, &target_url_patterns))
264 if (!result->PopulateURLPatterns(&document_url_patterns,
265 &target_url_patterns,
270 // parent_id is filled in from the value, but it might not be valid. It's left
271 // to be validated upon being added (via AddChildItem) to the menu manager.
272 scoped_ptr<Id> parent_id(
273 new Id(incognito, MenuItem::ExtensionKey(extension_id)));
274 if (value.HasKey(kParentUIDKey)) {
275 if (!value.GetString(kParentUIDKey, &parent_id->string_uid))
277 result->parent_id_.swap(parent_id);
279 return result.release();
282 bool MenuItem::PopulateURLPatterns(
283 std::vector<std::string>* document_url_patterns,
284 std::vector<std::string>* target_url_patterns,
285 std::string* error) {
286 if (document_url_patterns) {
287 if (!document_url_patterns_.Populate(
288 *document_url_patterns, URLPattern::SCHEME_ALL, true, error)) {
292 if (target_url_patterns) {
293 if (!target_url_patterns_.Populate(
294 *target_url_patterns, URLPattern::SCHEME_ALL, true, error)) {
302 const char MenuManager::kOnContextMenus[] = "contextMenus";
303 const char MenuManager::kOnWebviewContextMenus[] = "webview.contextMenus";
305 MenuManager::MenuManager(Profile* profile, StateStore* store)
306 : extension_registry_observer_(this), profile_(profile), store_(store) {
307 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
308 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
309 content::NotificationService::AllSources());
311 store_->RegisterKey(kContextMenusKey);
314 MenuManager::~MenuManager() {
315 MenuItemMap::iterator i;
316 for (i = context_items_.begin(); i != context_items_.end(); ++i) {
317 STLDeleteElements(&(i->second));
322 MenuManager* MenuManager::Get(Profile* profile) {
323 return MenuManagerFactory::GetForProfile(profile);
326 std::set<MenuItem::ExtensionKey> MenuManager::ExtensionIds() {
327 std::set<MenuItem::ExtensionKey> id_set;
328 for (MenuItemMap::const_iterator i = context_items_.begin();
329 i != context_items_.end(); ++i) {
330 id_set.insert(i->first);
335 const MenuItem::List* MenuManager::MenuItems(
336 const MenuItem::ExtensionKey& key) {
337 MenuItemMap::iterator i = context_items_.find(key);
338 if (i != context_items_.end()) {
344 bool MenuManager::AddContextItem(const Extension* extension, MenuItem* item) {
345 const MenuItem::ExtensionKey& key = item->id().extension_key;
346 // The item must have a non-empty extension id, and not have already been
348 if (key.empty() || ContainsKey(items_by_id_, item->id()))
351 DCHECK_EQ(extension->id(), key.extension_id);
353 bool first_item = !ContainsKey(context_items_, key);
354 context_items_[key].push_back(item);
355 items_by_id_[item->id()] = item;
357 if (item->type() == MenuItem::RADIO) {
359 RadioItemSelected(item);
361 SanitizeRadioList(context_items_[key]);
364 // If this is the first item for this extension, start loading its icon.
366 icon_manager_.LoadIcon(profile_, extension);
371 bool MenuManager::AddChildItem(const MenuItem::Id& parent_id,
373 MenuItem* parent = GetItemById(parent_id);
374 if (!parent || parent->type() != MenuItem::NORMAL ||
375 parent->incognito() != child->incognito() ||
376 parent->extension_id() != child->extension_id() ||
377 ContainsKey(items_by_id_, child->id()))
379 parent->AddChild(child);
380 items_by_id_[child->id()] = child;
382 if (child->type() == MenuItem::RADIO)
383 SanitizeRadioList(parent->children());
387 bool MenuManager::DescendantOf(MenuItem* item,
388 const MenuItem::Id& ancestor_id) {
389 // Work our way up the tree until we find the ancestor or NULL.
390 MenuItem::Id* id = item->parent_id();
392 DCHECK(*id != item->id()); // Catch circular graphs.
393 if (*id == ancestor_id)
395 MenuItem* next = GetItemById(*id);
400 id = next->parent_id();
405 bool MenuManager::ChangeParent(const MenuItem::Id& child_id,
406 const MenuItem::Id* parent_id) {
407 MenuItem* child = GetItemById(child_id);
408 MenuItem* new_parent = parent_id ? GetItemById(*parent_id) : NULL;
409 if ((parent_id && (child_id == *parent_id)) || !child ||
410 (!new_parent && parent_id != NULL) ||
411 (new_parent && (DescendantOf(new_parent, child_id) ||
412 child->incognito() != new_parent->incognito() ||
413 child->extension_id() != new_parent->extension_id())))
416 MenuItem::Id* old_parent_id = child->parent_id();
417 if (old_parent_id != NULL) {
418 MenuItem* old_parent = GetItemById(*old_parent_id);
424 old_parent->ReleaseChild(child_id, false /* non-recursive search*/);
425 DCHECK(taken == child);
426 SanitizeRadioList(old_parent->children());
428 // This is a top-level item, so we need to pull it out of our list of
430 const MenuItem::ExtensionKey& child_key = child->id().extension_key;
431 MenuItemMap::iterator i = context_items_.find(child_key);
432 if (i == context_items_.end()) {
436 MenuItem::List& list = i->second;
437 MenuItem::List::iterator j = std::find(list.begin(), list.end(), child);
438 if (j == list.end()) {
443 SanitizeRadioList(list);
447 new_parent->AddChild(child);
448 SanitizeRadioList(new_parent->children());
450 const MenuItem::ExtensionKey& child_key = child->id().extension_key;
451 context_items_[child_key].push_back(child);
452 child->parent_id_.reset(NULL);
453 SanitizeRadioList(context_items_[child_key]);
458 bool MenuManager::RemoveContextMenuItem(const MenuItem::Id& id) {
459 if (!ContainsKey(items_by_id_, id))
462 MenuItem* menu_item = GetItemById(id);
464 const MenuItem::ExtensionKey extension_key = id.extension_key;
465 MenuItemMap::iterator i = context_items_.find(extension_key);
466 if (i == context_items_.end()) {
472 std::set<MenuItem::Id> items_removed;
473 MenuItem::List& list = i->second;
474 MenuItem::List::iterator j;
475 for (j = list.begin(); j < list.end(); ++j) {
476 // See if the current top-level item is a match.
477 if ((*j)->id() == id) {
478 items_removed = (*j)->RemoveAllDescendants();
479 items_removed.insert(id);
483 SanitizeRadioList(list);
486 // See if the item to remove was found as a descendant of the current
488 MenuItem* child = (*j)->ReleaseChild(id, true /* recursive */);
490 items_removed = child->RemoveAllDescendants();
491 items_removed.insert(id);
492 SanitizeRadioList(GetItemById(*child->parent_id())->children());
499 DCHECK(result); // The check at the very top should have prevented this.
501 // Clear entries from the items_by_id_ map.
502 std::set<MenuItem::Id>::iterator removed_iter;
503 for (removed_iter = items_removed.begin();
504 removed_iter != items_removed.end();
506 items_by_id_.erase(*removed_iter);
510 context_items_.erase(extension_key);
511 icon_manager_.RemoveIcon(extension_key.extension_id);
516 void MenuManager::RemoveAllContextItems(
517 const MenuItem::ExtensionKey& extension_key) {
518 MenuItem::List::iterator i;
519 for (i = context_items_[extension_key].begin();
520 i != context_items_[extension_key].end();
523 items_by_id_.erase(item->id());
525 // Remove descendants from this item and erase them from the lookup cache.
526 std::set<MenuItem::Id> removed_ids = item->RemoveAllDescendants();
527 std::set<MenuItem::Id>::const_iterator j;
528 for (j = removed_ids.begin(); j != removed_ids.end(); ++j) {
529 items_by_id_.erase(*j);
532 STLDeleteElements(&context_items_[extension_key]);
533 context_items_.erase(extension_key);
534 icon_manager_.RemoveIcon(extension_key.extension_id);
537 MenuItem* MenuManager::GetItemById(const MenuItem::Id& id) const {
538 std::map<MenuItem::Id, MenuItem*>::const_iterator i =
539 items_by_id_.find(id);
540 if (i != items_by_id_.end())
546 void MenuManager::RadioItemSelected(MenuItem* item) {
547 // If this is a child item, we need to get a handle to the list from its
548 // parent. Otherwise get a handle to the top-level list.
549 const MenuItem::List* list = NULL;
550 if (item->parent_id()) {
551 MenuItem* parent = GetItemById(*item->parent_id());
556 list = &(parent->children());
558 const MenuItem::ExtensionKey& key = item->id().extension_key;
559 if (context_items_.find(key) == context_items_.end()) {
563 list = &context_items_[key];
566 // Find where |item| is in the list.
567 MenuItem::List::const_iterator item_location;
568 for (item_location = list->begin(); item_location != list->end();
570 if (*item_location == item)
573 if (item_location == list->end()) {
574 NOTREACHED(); // We should have found the item.
578 // Iterate backwards from |item| and uncheck any adjacent radio items.
579 MenuItem::List::const_iterator i;
580 if (item_location != list->begin()) {
584 if ((*i)->type() != MenuItem::RADIO)
586 (*i)->SetChecked(false);
587 } while (i != list->begin());
590 // Now iterate forwards from |item| and uncheck any adjacent radio items.
591 for (i = item_location + 1; i != list->end(); ++i) {
592 if ((*i)->type() != MenuItem::RADIO)
594 (*i)->SetChecked(false);
598 static void AddURLProperty(base::DictionaryValue* dictionary,
599 const std::string& key, const GURL& url) {
601 dictionary->SetString(key, url.possibly_invalid_spec());
604 void MenuManager::ExecuteCommand(Profile* profile,
605 WebContents* web_contents,
606 const content::ContextMenuParams& params,
607 const MenuItem::Id& menu_item_id) {
608 EventRouter* event_router = EventRouter::Get(profile);
612 MenuItem* item = GetItemById(menu_item_id);
616 // ExtensionService/Extension can be NULL in unit tests :(
617 ExtensionService* service =
618 ExtensionSystem::Get(profile_)->extension_service();
619 const Extension* extension =
620 service ? service->extensions()->GetByID(item->extension_id()) : NULL;
622 if (item->type() == MenuItem::RADIO)
623 RadioItemSelected(item);
625 scoped_ptr<base::ListValue> args(new base::ListValue());
627 base::DictionaryValue* properties = new base::DictionaryValue();
628 SetIdKeyValue(properties, "menuItemId", item->id());
629 if (item->parent_id())
630 SetIdKeyValue(properties, "parentMenuItemId", *item->parent_id());
632 switch (params.media_type) {
633 case blink::WebContextMenuData::MediaTypeImage:
634 properties->SetString("mediaType", "image");
636 case blink::WebContextMenuData::MediaTypeVideo:
637 properties->SetString("mediaType", "video");
639 case blink::WebContextMenuData::MediaTypeAudio:
640 properties->SetString("mediaType", "audio");
642 default: {} // Do nothing.
645 AddURLProperty(properties, "linkUrl", params.unfiltered_link_url);
646 AddURLProperty(properties, "srcUrl", params.src_url);
647 AddURLProperty(properties, "pageUrl", params.page_url);
648 AddURLProperty(properties, "frameUrl", params.frame_url);
650 if (params.selection_text.length() > 0)
651 properties->SetString("selectionText", params.selection_text);
653 properties->SetBoolean("editable", params.is_editable);
655 WebViewGuest* webview_guest = WebViewGuest::FromWebContents(web_contents);
657 // This is used in webview_custom_bindings.js.
658 // The property is not exposed to developer API.
659 properties->SetInteger("webviewInstanceId",
660 webview_guest->view_instance_id());
663 args->Append(properties);
665 // Add the tab info to the argument list.
666 // No tab info in a platform app.
667 if (!extension || !extension->is_platform_app()) {
668 // Note: web_contents are NULL in unit tests :(
670 args->Append(ExtensionTabUtil::CreateTabValue(web_contents));
672 args->Append(new base::DictionaryValue());
676 if (item->type() == MenuItem::CHECKBOX ||
677 item->type() == MenuItem::RADIO) {
678 bool was_checked = item->checked();
679 properties->SetBoolean("wasChecked", was_checked);
681 // RADIO items always get set to true when you click on them, but CHECKBOX
682 // items get their state toggled.
684 (item->type() == MenuItem::RADIO) ? true : !was_checked;
686 item->SetChecked(checked);
687 properties->SetBoolean("checked", item->checked());
690 WriteToStorage(extension, item->id().extension_key);
693 // Note: web_contents are NULL in unit tests :(
694 if (web_contents && extensions::TabHelper::FromWebContents(web_contents)) {
695 extensions::TabHelper::FromWebContents(web_contents)->
696 active_tab_permission_granter()->GrantIfRequested(extension);
700 // Dispatch to menu item's .onclick handler.
701 scoped_ptr<Event> event(
702 new Event(webview_guest ? kOnWebviewContextMenus
704 scoped_ptr<base::ListValue>(args->DeepCopy())));
705 event->restrict_to_browser_context = profile;
706 event->user_gesture = EventRouter::USER_GESTURE_ENABLED;
707 event_router->DispatchEventToExtension(item->extension_id(), event.Pass());
710 // Dispatch to .contextMenus.onClicked handler.
711 scoped_ptr<Event> event(
712 new Event(webview_guest ? webview::OnClicked::kEventName
713 : context_menus::OnClicked::kEventName,
715 event->restrict_to_browser_context = profile;
716 event->user_gesture = EventRouter::USER_GESTURE_ENABLED;
718 event->filter_info.SetInstanceID(webview_guest->view_instance_id());
719 event_router->DispatchEventToExtension(item->extension_id(), event.Pass());
723 void MenuManager::SanitizeRadioList(const MenuItem::List& item_list) {
724 MenuItem::List::const_iterator i = item_list.begin();
725 while (i != item_list.end()) {
726 if ((*i)->type() != MenuItem::RADIO) {
731 // Uncheck any checked radio items in the run, and at the end reset
732 // the appropriate one to checked. If no check radio items were found,
733 // then check the first radio item in the run.
734 MenuItem::List::const_iterator last_checked = item_list.end();
735 MenuItem::List::const_iterator radio_run_iter;
736 for (radio_run_iter = i; radio_run_iter != item_list.end();
738 if ((*radio_run_iter)->type() != MenuItem::RADIO) {
742 if ((*radio_run_iter)->checked()) {
743 last_checked = radio_run_iter;
744 (*radio_run_iter)->SetChecked(false);
748 if (last_checked != item_list.end())
749 (*last_checked)->SetChecked(true);
751 (*i)->SetChecked(true);
757 bool MenuManager::ItemUpdated(const MenuItem::Id& id) {
758 if (!ContainsKey(items_by_id_, id))
761 MenuItem* menu_item = GetItemById(id);
764 if (menu_item->parent_id()) {
765 SanitizeRadioList(GetItemById(*menu_item->parent_id())->children());
767 MenuItemMap::iterator i =
768 context_items_.find(menu_item->id().extension_key);
769 if (i == context_items_.end()) {
773 SanitizeRadioList(i->second);
779 void MenuManager::WriteToStorage(const Extension* extension,
780 const MenuItem::ExtensionKey& extension_key) {
781 if (!BackgroundInfo::HasLazyBackgroundPage(extension))
783 // <webview> menu items are transient and not stored in storage.
784 if (extension_key.webview_instance_id)
786 const MenuItem::List* top_items = MenuItems(extension_key);
787 MenuItem::List all_items;
789 for (MenuItem::List::const_iterator i = top_items->begin();
790 i != top_items->end(); ++i) {
791 DCHECK(!(*i)->id().extension_key.webview_instance_id);
792 (*i)->GetFlattenedSubtree(&all_items);
797 store_->SetExtensionValue(extension->id(), kContextMenusKey,
798 MenuItemsToValue(all_items));
802 void MenuManager::ReadFromStorage(const std::string& extension_id,
803 scoped_ptr<base::Value> value) {
804 const Extension* extension =
805 ExtensionSystem::Get(profile_)->extension_service()->extensions()->
806 GetByID(extension_id);
810 MenuItem::List items = MenuItemsFromValue(extension_id, value.get());
811 for (size_t i = 0; i < items.size(); ++i) {
814 if (items[i]->parent_id()) {
815 // Parent IDs are stored in the parent_id field for convenience, but
816 // they have not yet been validated. Separate them out here.
817 // Because of the order in which we store items in the prefs, parents will
818 // precede children, so we should already know about any parent items.
819 scoped_ptr<MenuItem::Id> parent_id;
820 parent_id.swap(items[i]->parent_id_);
821 added = AddChildItem(*parent_id, items[i]);
823 added = AddContextItem(extension, items[i]);
831 void MenuManager::OnExtensionLoaded(content::BrowserContext* browser_context,
832 const Extension* extension) {
833 if (store_ && BackgroundInfo::HasLazyBackgroundPage(extension)) {
834 store_->GetExtensionValue(
838 &MenuManager::ReadFromStorage, AsWeakPtr(), extension->id()));
842 void MenuManager::OnExtensionUnloaded(content::BrowserContext* browser_context,
843 const Extension* extension,
844 UnloadedExtensionInfo::Reason reason) {
845 MenuItem::ExtensionKey extension_key(extension->id());
846 if (ContainsKey(context_items_, extension_key)) {
847 RemoveAllContextItems(extension_key);
851 void MenuManager::Observe(int type,
852 const content::NotificationSource& source,
853 const content::NotificationDetails& details) {
854 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type);
855 Profile* profile = content::Source<Profile>(source).ptr();
856 // We cannot use profile_->HasOffTheRecordProfile as it may already be
857 // false at this point, if for example the incognito profile was destroyed
858 // using DestroyOffTheRecordProfile.
859 if (profile->GetOriginalProfile() == profile_ &&
860 profile->GetOriginalProfile() != profile) {
861 RemoveAllIncognitoContextItems();
865 const SkBitmap& MenuManager::GetIconForExtension(
866 const std::string& extension_id) {
867 return icon_manager_.GetIcon(extension_id);
870 void MenuManager::RemoveAllIncognitoContextItems() {
871 // Get all context menu items with "incognito" set to "split".
872 std::set<MenuItem::Id> items_to_remove;
873 std::map<MenuItem::Id, MenuItem*>::const_iterator iter;
874 for (iter = items_by_id_.begin();
875 iter != items_by_id_.end();
877 if (iter->first.incognito)
878 items_to_remove.insert(iter->first);
881 std::set<MenuItem::Id>::iterator remove_iter;
882 for (remove_iter = items_to_remove.begin();
883 remove_iter != items_to_remove.end();
885 RemoveContextMenuItem(*remove_iter);
888 MenuItem::ExtensionKey::ExtensionKey() : webview_instance_id(0) {}
890 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id,
891 int webview_instance_id)
892 : extension_id(extension_id), webview_instance_id(webview_instance_id) {}
894 MenuItem::ExtensionKey::ExtensionKey(const std::string& extension_id)
895 : extension_id(extension_id), webview_instance_id(0) {}
897 bool MenuItem::ExtensionKey::operator==(const ExtensionKey& other) const {
898 return extension_id == other.extension_id &&
899 webview_instance_id == other.webview_instance_id;
902 bool MenuItem::ExtensionKey::operator<(const ExtensionKey& other) const {
903 if (extension_id != other.extension_id)
904 return extension_id < other.extension_id;
906 return webview_instance_id < other.webview_instance_id;
909 bool MenuItem::ExtensionKey::operator!=(const ExtensionKey& other) const {
910 return !(*this == other);
913 bool MenuItem::ExtensionKey::empty() const {
914 return extension_id.empty() && !webview_instance_id;
917 MenuItem::Id::Id() : incognito(false), uid(0) {}
919 MenuItem::Id::Id(bool incognito, const MenuItem::ExtensionKey& extension_key)
920 : incognito(incognito), extension_key(extension_key), uid(0) {}
922 MenuItem::Id::~Id() {
925 bool MenuItem::Id::operator==(const Id& other) const {
926 return (incognito == other.incognito &&
927 extension_key == other.extension_key && uid == other.uid &&
928 string_uid == other.string_uid);
931 bool MenuItem::Id::operator!=(const Id& other) const {
932 return !(*this == other);
935 bool MenuItem::Id::operator<(const Id& other) const {
936 if (incognito < other.incognito)
938 if (incognito == other.incognito) {
939 if (extension_key < other.extension_key)
941 if (extension_key == other.extension_key) {
944 if (uid == other.uid)
945 return string_uid < other.string_uid;
951 } // namespace extensions