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" |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "chrome/browser/guestview/webview/webview_guest.h" | 21 #include "chrome/browser/guestview/webview/webview_guest.h" |
22 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/common/extensions/api/context_menus.h" | 23 #include "chrome/common/extensions/api/context_menus.h" |
24 #include "chrome/common/extensions/api/webview.h" | 24 #include "chrome/common/extensions/api/webview.h" |
25 #include "content/public/browser/notification_details.h" | 25 #include "content/public/browser/notification_details.h" |
26 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
27 #include "content/public/browser/notification_source.h" | 27 #include "content/public/browser/notification_source.h" |
28 #include "content/public/browser/web_contents.h" | 28 #include "content/public/browser/web_contents.h" |
29 #include "content/public/common/context_menu_params.h" | 29 #include "content/public/common/context_menu_params.h" |
30 #include "extensions/browser/event_router.h" | 30 #include "extensions/browser/event_router.h" |
| 31 #include "extensions/browser/extension_registry.h" |
31 #include "extensions/browser/extension_system.h" | 32 #include "extensions/browser/extension_system.h" |
32 #include "extensions/common/extension.h" | 33 #include "extensions/common/extension.h" |
33 #include "extensions/common/manifest_handlers/background_info.h" | 34 #include "extensions/common/manifest_handlers/background_info.h" |
34 #include "ui/gfx/favicon_size.h" | 35 #include "ui/gfx/favicon_size.h" |
35 #include "ui/gfx/text_elider.h" | 36 #include "ui/gfx/text_elider.h" |
36 | 37 |
37 using content::WebContents; | 38 using content::WebContents; |
38 using extensions::ExtensionSystem; | 39 using extensions::ExtensionSystem; |
39 | 40 |
40 namespace extensions { | 41 namespace extensions { |
(...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 } | 296 } |
296 } | 297 } |
297 return true; | 298 return true; |
298 } | 299 } |
299 | 300 |
300 // static | 301 // static |
301 const char MenuManager::kOnContextMenus[] = "contextMenus"; | 302 const char MenuManager::kOnContextMenus[] = "contextMenus"; |
302 const char MenuManager::kOnWebviewContextMenus[] = "webview.contextMenus"; | 303 const char MenuManager::kOnWebviewContextMenus[] = "webview.contextMenus"; |
303 | 304 |
304 MenuManager::MenuManager(Profile* profile, StateStore* store) | 305 MenuManager::MenuManager(Profile* profile, StateStore* store) |
305 : profile_(profile), store_(store) { | 306 : extension_registry_observer_(this), profile_(profile), store_(store) { |
306 registrar_.Add(this, | 307 extension_registry_observer_.Add(ExtensionRegistry::Get(profile_)); |
307 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
308 content::Source<Profile>(profile)); | |
309 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | |
310 content::Source<Profile>(profile)); | |
311 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | 308 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, |
312 content::NotificationService::AllSources()); | 309 content::NotificationService::AllSources()); |
313 if (store_) | 310 if (store_) |
314 store_->RegisterKey(kContextMenusKey); | 311 store_->RegisterKey(kContextMenusKey); |
315 } | 312 } |
316 | 313 |
317 MenuManager::~MenuManager() { | 314 MenuManager::~MenuManager() { |
318 MenuItemMap::iterator i; | 315 MenuItemMap::iterator i; |
319 for (i = context_items_.begin(); i != context_items_.end(); ++i) { | 316 for (i = context_items_.begin(); i != context_items_.end(); ++i) { |
320 STLDeleteElements(&(i->second)); | 317 STLDeleteElements(&(i->second)); |
(...skipping 503 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
824 added = AddChildItem(*parent_id, items[i]); | 821 added = AddChildItem(*parent_id, items[i]); |
825 } else { | 822 } else { |
826 added = AddContextItem(extension, items[i]); | 823 added = AddContextItem(extension, items[i]); |
827 } | 824 } |
828 | 825 |
829 if (!added) | 826 if (!added) |
830 delete items[i]; | 827 delete items[i]; |
831 } | 828 } |
832 } | 829 } |
833 | 830 |
| 831 void MenuManager::OnExtensionLoaded(content::BrowserContext* browser_context, |
| 832 const Extension* extension) { |
| 833 if (store_ && BackgroundInfo::HasLazyBackgroundPage(extension)) { |
| 834 store_->GetExtensionValue( |
| 835 extension->id(), |
| 836 kContextMenusKey, |
| 837 base::Bind( |
| 838 &MenuManager::ReadFromStorage, AsWeakPtr(), extension->id())); |
| 839 } |
| 840 } |
| 841 |
| 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); |
| 848 } |
| 849 } |
| 850 |
834 void MenuManager::Observe(int type, | 851 void MenuManager::Observe(int type, |
835 const content::NotificationSource& source, | 852 const content::NotificationSource& source, |
836 const content::NotificationDetails& details) { | 853 const content::NotificationDetails& details) { |
837 switch (type) { | 854 DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type); |
838 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | 855 Profile* profile = content::Source<Profile>(source).ptr(); |
839 // Remove menu items for disabled/uninstalled extensions. | 856 // We cannot use profile_->HasOffTheRecordProfile as it may already be |
840 const Extension* extension = | 857 // false at this point, if for example the incognito profile was destroyed |
841 content::Details<UnloadedExtensionInfo>(details)->extension; | 858 // using DestroyOffTheRecordProfile. |
842 MenuItem::ExtensionKey extension_key(extension->id()); | 859 if (profile->GetOriginalProfile() == profile_ && |
843 if (ContainsKey(context_items_, extension_key)) { | 860 profile->GetOriginalProfile() != profile) { |
844 RemoveAllContextItems(extension_key); | 861 RemoveAllIncognitoContextItems(); |
845 } | |
846 break; | |
847 } | |
848 case chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED: { | |
849 const Extension* extension = | |
850 content::Details<const Extension>(details).ptr(); | |
851 if (store_ && BackgroundInfo::HasLazyBackgroundPage(extension)) { | |
852 store_->GetExtensionValue(extension->id(), kContextMenusKey, | |
853 base::Bind(&MenuManager::ReadFromStorage, | |
854 AsWeakPtr(), extension->id())); | |
855 } | |
856 break; | |
857 } | |
858 case chrome::NOTIFICATION_PROFILE_DESTROYED: { | |
859 Profile* profile = content::Source<Profile>(source).ptr(); | |
860 // We cannot use profile_->HasOffTheRecordProfile as it may already be | |
861 // false at this point, if for example the incognito profile was destroyed | |
862 // using DestroyOffTheRecordProfile. | |
863 if (profile->GetOriginalProfile() == profile_ && | |
864 profile->GetOriginalProfile() != profile) { | |
865 RemoveAllIncognitoContextItems(); | |
866 } | |
867 break; | |
868 } | |
869 default: | |
870 NOTREACHED(); | |
871 break; | |
872 } | 862 } |
873 } | 863 } |
874 | 864 |
875 const SkBitmap& MenuManager::GetIconForExtension( | 865 const SkBitmap& MenuManager::GetIconForExtension( |
876 const std::string& extension_id) { | 866 const std::string& extension_id) { |
877 return icon_manager_.GetIcon(extension_id); | 867 return icon_manager_.GetIcon(extension_id); |
878 } | 868 } |
879 | 869 |
880 void MenuManager::RemoveAllIncognitoContextItems() { | 870 void MenuManager::RemoveAllIncognitoContextItems() { |
881 // Get all context menu items with "incognito" set to "split". | 871 // Get all context menu items with "incognito" set to "split". |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
952 if (uid < other.uid) | 942 if (uid < other.uid) |
953 return true; | 943 return true; |
954 if (uid == other.uid) | 944 if (uid == other.uid) |
955 return string_uid < other.string_uid; | 945 return string_uid < other.string_uid; |
956 } | 946 } |
957 } | 947 } |
958 return false; | 948 return false; |
959 } | 949 } |
960 | 950 |
961 } // namespace extensions | 951 } // namespace extensions |
OLD | NEW |