Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / menu_manager.cc
index 27accff..dc71cc6 100644 (file)
@@ -18,7 +18,7 @@
 #include "chrome/browser/extensions/menu_manager_factory.h"
 #include "chrome/browser/extensions/state_store.h"
 #include "chrome/browser/extensions/tab_helper.h"
-#include "chrome/browser/guestview/webview/webview_guest.h"
+#include "chrome/browser/guest_view/web_view/web_view_guest.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/common/extensions/api/context_menus.h"
 #include "chrome/common/extensions/api/webview.h"
@@ -28,6 +28,7 @@
 #include "content/public/browser/web_contents.h"
 #include "content/public/common/context_menu_params.h"
 #include "extensions/browser/event_router.h"
+#include "extensions/browser/extension_registry.h"
 #include "extensions/browser/extension_system.h"
 #include "extensions/common/extension.h"
 #include "extensions/common/manifest_handlers/background_info.h"
@@ -302,11 +303,8 @@ const char MenuManager::kOnContextMenus[] = "contextMenus";
 const char MenuManager::kOnWebviewContextMenus[] = "webview.contextMenus";
 
 MenuManager::MenuManager(Profile* profile, StateStore* store)
-    : profile_(profile), store_(store) {
-  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
-                 content::Source<Profile>(profile));
-  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED,
-                 content::Source<Profile>(profile));
+    : extension_registry_observer_(this), profile_(profile), store_(store) {
+  extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
   registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED,
                  content::NotificationService::AllSources());
   if (store_)
@@ -607,8 +605,7 @@ void MenuManager::ExecuteCommand(Profile* profile,
                                  WebContents* web_contents,
                                  const content::ContextMenuParams& params,
                                  const MenuItem::Id& menu_item_id) {
-  EventRouter* event_router = extensions::ExtensionSystem::Get(profile)->
-      event_router();
+  EventRouter* event_router = EventRouter::Get(profile);
   if (!event_router)
     return;
 
@@ -812,6 +809,8 @@ void MenuManager::ReadFromStorage(const std::string& extension_id,
 
   MenuItem::List items = MenuItemsFromValue(extension_id, value.get());
   for (size_t i = 0; i < items.size(); ++i) {
+    bool added = false;
+
     if (items[i]->parent_id()) {
       // Parent IDs are stored in the parent_id field for convenience, but
       // they have not yet been validated. Separate them out here.
@@ -819,51 +818,47 @@ void MenuManager::ReadFromStorage(const std::string& extension_id,
       // precede children, so we should already know about any parent items.
       scoped_ptr<MenuItem::Id> parent_id;
       parent_id.swap(items[i]->parent_id_);
-      AddChildItem(*parent_id, items[i]);
+      added = AddChildItem(*parent_id, items[i]);
     } else {
-      AddContextItem(extension, items[i]);
+      added = AddContextItem(extension, items[i]);
     }
+
+    if (!added)
+      delete items[i];
+  }
+}
+
+void MenuManager::OnExtensionLoaded(content::BrowserContext* browser_context,
+                                    const Extension* extension) {
+  if (store_ && BackgroundInfo::HasLazyBackgroundPage(extension)) {
+    store_->GetExtensionValue(
+        extension->id(),
+        kContextMenusKey,
+        base::Bind(
+            &MenuManager::ReadFromStorage, AsWeakPtr(), extension->id()));
+  }
+}
+
+void MenuManager::OnExtensionUnloaded(content::BrowserContext* browser_context,
+                                      const Extension* extension,
+                                      UnloadedExtensionInfo::Reason reason) {
+  MenuItem::ExtensionKey extension_key(extension->id());
+  if (ContainsKey(context_items_, extension_key)) {
+    RemoveAllContextItems(extension_key);
   }
 }
 
 void MenuManager::Observe(int type,
                           const content::NotificationSource& source,
                           const content::NotificationDetails& details) {
-  switch (type) {
-    case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: {
-      // Remove menu items for disabled/uninstalled extensions.
-      const Extension* extension =
-          content::Details<UnloadedExtensionInfo>(details)->extension;
-      MenuItem::ExtensionKey extension_key(extension->id());
-      if (ContainsKey(context_items_, extension_key)) {
-        RemoveAllContextItems(extension_key);
-      }
-      break;
-    }
-    case chrome::NOTIFICATION_EXTENSION_LOADED: {
-      const Extension* extension =
-          content::Details<const Extension>(details).ptr();
-      if (store_ && BackgroundInfo::HasLazyBackgroundPage(extension)) {
-        store_->GetExtensionValue(extension->id(), kContextMenusKey,
-            base::Bind(&MenuManager::ReadFromStorage,
-                       AsWeakPtr(), extension->id()));
-      }
-      break;
-    }
-    case chrome::NOTIFICATION_PROFILE_DESTROYED: {
-      Profile* profile = content::Source<Profile>(source).ptr();
-      // We cannot use profile_->HasOffTheRecordProfile as it may already be
-      // false at this point, if for example the incognito profile was destroyed
-      // using DestroyOffTheRecordProfile.
-      if (profile->GetOriginalProfile() == profile_ &&
-          profile->GetOriginalProfile() != profile) {
-        RemoveAllIncognitoContextItems();
-      }
-      break;
-    }
-    default:
-      NOTREACHED();
-      break;
+  DCHECK_EQ(chrome::NOTIFICATION_PROFILE_DESTROYED, type);
+  Profile* profile = content::Source<Profile>(source).ptr();
+  // We cannot use profile_->HasOffTheRecordProfile as it may already be
+  // false at this point, if for example the incognito profile was destroyed
+  // using DestroyOffTheRecordProfile.
+  if (profile->GetOriginalProfile() == profile_ &&
+      profile->GetOriginalProfile() != profile) {
+    RemoveAllIncognitoContextItems();
   }
 }