OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/extensions/menu_manager.h" | 5 #include "chrome/browser/extensions/menu_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
16 #include "chrome/browser/extensions/event_names.h" | 16 #include "chrome/browser/extensions/event_names.h" |
17 #include "chrome/browser/extensions/event_router.h" | 17 #include "chrome/browser/extensions/event_router.h" |
18 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
19 #include "chrome/browser/extensions/extension_system.h" | 19 #include "chrome/browser/extensions/extension_system.h" |
20 #include "chrome/browser/extensions/extension_tab_util.h" | 20 #include "chrome/browser/extensions/extension_tab_util.h" |
| 21 #include "chrome/browser/extensions/menu_manager_factory.h" |
21 #include "chrome/browser/extensions/state_store.h" | 22 #include "chrome/browser/extensions/state_store.h" |
22 #include "chrome/browser/extensions/tab_helper.h" | 23 #include "chrome/browser/extensions/tab_helper.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/common/extensions/api/context_menus.h" | 25 #include "chrome/common/extensions/api/context_menus.h" |
25 #include "chrome/common/extensions/extension.h" | 26 #include "chrome/common/extensions/extension.h" |
26 #include "content/public/browser/notification_details.h" | 27 #include "content/public/browser/notification_details.h" |
| 28 #include "content/public/browser/notification_service.h" |
27 #include "content/public/browser/notification_source.h" | 29 #include "content/public/browser/notification_source.h" |
28 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
29 #include "content/public/common/context_menu_params.h" | 31 #include "content/public/common/context_menu_params.h" |
30 #include "extensions/common/manifest_handlers/background_info.h" | 32 #include "extensions/common/manifest_handlers/background_info.h" |
31 #include "ui/gfx/favicon_size.h" | 33 #include "ui/gfx/favicon_size.h" |
32 #include "ui/gfx/text_elider.h" | 34 #include "ui/gfx/text_elider.h" |
33 | 35 |
34 using content::WebContents; | 36 using content::WebContents; |
35 using extensions::ExtensionSystem; | 37 using extensions::ExtensionSystem; |
36 | 38 |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 } | 287 } |
286 if (target_url_patterns) { | 288 if (target_url_patterns) { |
287 if (!target_url_patterns_.Populate( | 289 if (!target_url_patterns_.Populate( |
288 *target_url_patterns, URLPattern::SCHEME_ALL, true, error)) { | 290 *target_url_patterns, URLPattern::SCHEME_ALL, true, error)) { |
289 return false; | 291 return false; |
290 } | 292 } |
291 } | 293 } |
292 return true; | 294 return true; |
293 } | 295 } |
294 | 296 |
295 MenuManager::MenuManager(Profile* profile) | 297 MenuManager::MenuManager(Profile* profile, StateStore* store) |
296 : profile_(profile) { | 298 : profile_(profile), store_(store) { |
297 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 299 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
298 content::Source<Profile>(profile)); | 300 content::Source<Profile>(profile)); |
299 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 301 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
300 content::Source<Profile>(profile)); | 302 content::Source<Profile>(profile)); |
301 | 303 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
302 StateStore* store = ExtensionSystem::Get(profile_)->state_store(); | 304 content::NotificationService::AllSources()); |
303 if (store) | 305 if (store_) |
304 store->RegisterKey(kContextMenusKey); | 306 store_->RegisterKey(kContextMenusKey); |
305 } | 307 } |
306 | 308 |
307 MenuManager::~MenuManager() { | 309 MenuManager::~MenuManager() { |
308 MenuItemMap::iterator i; | 310 MenuItemMap::iterator i; |
309 for (i = context_items_.begin(); i != context_items_.end(); ++i) { | 311 for (i = context_items_.begin(); i != context_items_.end(); ++i) { |
310 STLDeleteElements(&(i->second)); | 312 STLDeleteElements(&(i->second)); |
311 } | 313 } |
312 } | 314 } |
313 | 315 |
| 316 // static |
| 317 MenuManager* MenuManager::Get(Profile* profile) { |
| 318 return MenuManagerFactory::GetForProfile(profile); |
| 319 } |
| 320 |
314 std::set<std::string> MenuManager::ExtensionIds() { | 321 std::set<std::string> MenuManager::ExtensionIds() { |
315 std::set<std::string> id_set; | 322 std::set<std::string> id_set; |
316 for (MenuItemMap::const_iterator i = context_items_.begin(); | 323 for (MenuItemMap::const_iterator i = context_items_.begin(); |
317 i != context_items_.end(); ++i) { | 324 i != context_items_.end(); ++i) { |
318 id_set.insert(i->first); | 325 id_set.insert(i->first); |
319 } | 326 } |
320 return id_set; | 327 return id_set; |
321 } | 328 } |
322 | 329 |
323 const MenuItem::List* MenuManager::MenuItems( | 330 const MenuItem::List* MenuManager::MenuItems( |
(...skipping 429 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
753 return; | 760 return; |
754 const MenuItem::List* top_items = MenuItems(extension->id()); | 761 const MenuItem::List* top_items = MenuItems(extension->id()); |
755 MenuItem::List all_items; | 762 MenuItem::List all_items; |
756 if (top_items) { | 763 if (top_items) { |
757 for (MenuItem::List::const_iterator i = top_items->begin(); | 764 for (MenuItem::List::const_iterator i = top_items->begin(); |
758 i != top_items->end(); ++i) { | 765 i != top_items->end(); ++i) { |
759 (*i)->GetFlattenedSubtree(&all_items); | 766 (*i)->GetFlattenedSubtree(&all_items); |
760 } | 767 } |
761 } | 768 } |
762 | 769 |
763 StateStore* store = ExtensionSystem::Get(profile_)->state_store(); | 770 if (store_) |
764 if (store) | 771 store_->SetExtensionValue(extension->id(), kContextMenusKey, |
765 store->SetExtensionValue(extension->id(), kContextMenusKey, | 772 MenuItemsToValue(all_items)); |
766 MenuItemsToValue(all_items)); | |
767 } | 773 } |
768 | 774 |
769 void MenuManager::ReadFromStorage(const std::string& extension_id, | 775 void MenuManager::ReadFromStorage(const std::string& extension_id, |
770 scoped_ptr<base::Value> value) { | 776 scoped_ptr<base::Value> value) { |
771 const Extension* extension = | 777 const Extension* extension = |
772 ExtensionSystem::Get(profile_)->extension_service()->extensions()-> | 778 ExtensionSystem::Get(profile_)->extension_service()->extensions()-> |
773 GetByID(extension_id); | 779 GetByID(extension_id); |
774 if (!extension) | 780 if (!extension) |
775 return; | 781 return; |
776 | 782 |
777 MenuItem::List items = MenuItemsFromValue(extension_id, value.get()); | 783 MenuItem::List items = MenuItemsFromValue(extension_id, value.get()); |
778 for (size_t i = 0; i < items.size(); ++i) { | 784 for (size_t i = 0; i < items.size(); ++i) { |
779 if (items[i]->parent_id()) { | 785 if (items[i]->parent_id()) { |
780 // Parent IDs are stored in the parent_id field for convenience, but | 786 // Parent IDs are stored in the parent_id field for convenience, but |
781 // they have not yet been validated. Separate them out here. | 787 // they have not yet been validated. Separate them out here. |
782 // Because of the order in which we store items in the prefs, parents will | 788 // Because of the order in which we store items in the prefs, parents will |
783 // precede children, so we should already know about any parent items. | 789 // precede children, so we should already know about any parent items. |
784 scoped_ptr<MenuItem::Id> parent_id; | 790 scoped_ptr<MenuItem::Id> parent_id; |
785 parent_id.swap(items[i]->parent_id_); | 791 parent_id.swap(items[i]->parent_id_); |
786 AddChildItem(*parent_id, items[i]); | 792 AddChildItem(*parent_id, items[i]); |
787 } else { | 793 } else { |
788 AddContextItem(extension, items[i]); | 794 AddContextItem(extension, items[i]); |
789 } | 795 } |
790 } | 796 } |
791 } | 797 } |
792 | 798 |
793 void MenuManager::Observe(int type, | 799 void MenuManager::Observe(int type, |
794 const content::NotificationSource& source, | 800 const content::NotificationSource& source, |
795 const content::NotificationDetails& details) { | 801 const content::NotificationDetails& details) { |
796 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { | 802 switch (type) { |
797 // Remove menu items for disabled/uninstalled extensions. | 803 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
798 const Extension* extension = | 804 // Remove menu items for disabled/uninstalled extensions. |
799 content::Details<UnloadedExtensionInfo>(details)->extension; | 805 const Extension* extension = |
800 if (ContainsKey(context_items_, extension->id())) { | 806 content::Details<UnloadedExtensionInfo>(details)->extension; |
801 RemoveAllContextItems(extension->id()); | 807 if (ContainsKey(context_items_, extension->id())) { |
| 808 RemoveAllContextItems(extension->id()); |
| 809 } |
| 810 break; |
802 } | 811 } |
803 } else if (type == chrome::NOTIFICATION_EXTENSION_LOADED) { | 812 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
804 const Extension* extension = | 813 const Extension* extension = |
805 content::Details<const Extension>(details).ptr(); | 814 content::Details<const Extension>(details).ptr(); |
806 StateStore* store = ExtensionSystem::Get(profile_)->state_store(); | 815 if (store_ && BackgroundInfo::HasLazyBackgroundPage(extension)) { |
807 if (store && BackgroundInfo::HasLazyBackgroundPage(extension)) { | 816 store_->GetExtensionValue(extension->id(), kContextMenusKey, |
808 store->GetExtensionValue(extension->id(), kContextMenusKey, | 817 base::Bind(&MenuManager::ReadFromStorage, |
809 base::Bind(&MenuManager::ReadFromStorage, | 818 AsWeakPtr(), extension->id())); |
810 AsWeakPtr(), extension->id())); | 819 } |
| 820 break; |
811 } | 821 } |
| 822 case chrome::NOTIFICATION_PROFILE_DESTROYED: { |
| 823 Profile* profile = content::Source<Profile>(source).ptr(); |
| 824 // We cannot use profile_->HasOffTheRecordProfile as it may already be |
| 825 // false at this point, if for example the incognito profile was destroyed |
| 826 // using DestroyOffTheRecordProfile. |
| 827 if (profile->GetOriginalProfile() == profile_ && |
| 828 profile->GetOriginalProfile() != profile) { |
| 829 RemoveAllIncognitoContextItems(); |
| 830 } |
| 831 break; |
| 832 } |
| 833 default: |
| 834 NOTREACHED(); |
| 835 break; |
812 } | 836 } |
813 } | 837 } |
814 | 838 |
815 const SkBitmap& MenuManager::GetIconForExtension( | 839 const SkBitmap& MenuManager::GetIconForExtension( |
816 const std::string& extension_id) { | 840 const std::string& extension_id) { |
817 return icon_manager_.GetIcon(extension_id); | 841 return icon_manager_.GetIcon(extension_id); |
818 } | 842 } |
819 | 843 |
820 void MenuManager::RemoveAllIncognitoContextItems() { | 844 void MenuManager::RemoveAllIncognitoContextItems() { |
821 // Get all context menu items with "incognito" set to "split". | 845 // Get all context menu items with "incognito" set to "split". |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
864 if (uid < other.uid) | 888 if (uid < other.uid) |
865 return true; | 889 return true; |
866 if (uid == other.uid) | 890 if (uid == other.uid) |
867 return string_uid < other.string_uid; | 891 return string_uid < other.string_uid; |
868 } | 892 } |
869 } | 893 } |
870 return false; | 894 return false; |
871 } | 895 } |
872 | 896 |
873 } // namespace extensions | 897 } // namespace extensions |
OLD | NEW |