Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / plugin_manager.cc
index 0c88dc6..86bbe93 100644 (file)
 #include "base/lazy_instance.h"
 #include "base/path_service.h"
 #include "base/strings/utf_string_conversions.h"
-#include "chrome/browser/chrome_notification_types.h"
+#include "chrome/browser/extensions/extension_service.h"
 #include "chrome/browser/extensions/plugin_manager.h"
 #include "chrome/browser/plugins/chrome_plugin_service_filter.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/common/chrome_paths.h"
 #include "chrome/common/extensions/api/plugins/plugins_handler.h"
-#include "content/public/browser/notification_details.h"
-#include "content/public/browser/notification_source.h"
+#include "chrome/common/extensions/manifest_handlers/mime_types_handler.h"
 #include "content/public/browser/plugin_service.h"
 #include "content/public/common/pepper_plugin_info.h"
+#include "extensions/browser/extension_registry.h"
 #include "extensions/common/extension.h"
 #include "url/gurl.h"
 
 using content::PluginService;
 
-static const char* kNaClPluginMimeType = "application/x-nacl";
+#if !defined(DISABLE_NACL)
+static const char kNaClPluginMimeType[] = "application/x-nacl";
+#endif
 
 namespace extensions {
 
-PluginManager::PluginManager(Profile* profile) : profile_(profile) {
-  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
-                 content::Source<Profile>(profile));
-  registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
-                 content::Source<Profile>(profile));
+PluginManager::PluginManager(content::BrowserContext* context)
+    : profile_(Profile::FromBrowserContext(context)),
+      extension_registry_observer_(this) {
+  extension_registry_observer_.Add(ExtensionRegistry::Get(profile_));
 }
 
 PluginManager::~PluginManager() {
 }
 
-static base::LazyInstance<ProfileKeyedAPIFactory<PluginManager> >
+static base::LazyInstance<BrowserContextKeyedAPIFactory<PluginManager> >
     g_factory = LAZY_INSTANCE_INITIALIZER;
 
 // static
-ProfileKeyedAPIFactory<PluginManager>* PluginManager::GetFactoryInstance() {
+BrowserContextKeyedAPIFactory<PluginManager>*
+PluginManager::GetFactoryInstance() {
   return g_factory.Pointer();
 }
 
-void PluginManager::Observe(int type,
-                            const content::NotificationSource& source,
-                            const content::NotificationDetails& details) {
-  if (type == chrome::NOTIFICATION_EXTENSION_LOADED) {
-    const Extension* extension =
-        content::Details<const Extension>(details).ptr();
-
-    bool plugins_or_nacl_changed = false;
-    if (PluginInfo::HasPlugins(extension)) {
-      const PluginInfo::PluginVector* plugins =
-          PluginInfo::GetPlugins(extension);
-      CHECK(plugins);
-      plugins_or_nacl_changed = true;
-      for (PluginInfo::PluginVector::const_iterator plugin = plugins->begin();
-           plugin != plugins->end(); ++plugin) {
-        PluginService::GetInstance()->RefreshPlugins();
-        PluginService::GetInstance()->AddExtraPluginPath(plugin->path);
-        ChromePluginServiceFilter* filter =
-            ChromePluginServiceFilter::GetInstance();
-        if (plugin->is_public) {
-          filter->RestrictPluginToProfileAndOrigin(
-              plugin->path, profile_, GURL());
-        } else {
-          filter->RestrictPluginToProfileAndOrigin(
-              plugin->path, profile_, extension->url());
-        }
+void PluginManager::OnExtensionLoaded(content::BrowserContext* browser_context,
+                                      const Extension* extension) {
+  bool plugins_or_nacl_changed = false;
+  if (PluginInfo::HasPlugins(extension)) {
+    const PluginInfo::PluginVector* plugins = PluginInfo::GetPlugins(extension);
+    CHECK(plugins);
+    plugins_or_nacl_changed = true;
+    for (PluginInfo::PluginVector::const_iterator plugin = plugins->begin();
+         plugin != plugins->end();
+         ++plugin) {
+      PluginService::GetInstance()->RefreshPlugins();
+      PluginService::GetInstance()->AddExtraPluginPath(plugin->path);
+      ChromePluginServiceFilter* filter =
+          ChromePluginServiceFilter::GetInstance();
+      if (plugin->is_public) {
+        filter->RestrictPluginToProfileAndOrigin(
+            plugin->path, profile_, GURL());
+      } else {
+        filter->RestrictPluginToProfileAndOrigin(
+            plugin->path, profile_, extension->url());
       }
     }
+  }
 
-    const NaClModuleInfo::List* nacl_modules =
-        NaClModuleInfo::GetNaClModules(extension);
-    if (nacl_modules) {
-      plugins_or_nacl_changed = true;
-      for (NaClModuleInfo::List::const_iterator module = nacl_modules->begin();
-           module != nacl_modules->end(); ++module) {
-        RegisterNaClModule(*module);
-      }
-      UpdatePluginListWithNaClModules();
+#if !defined(DISABLE_NACL)
+  const NaClModuleInfo::List* nacl_modules =
+      NaClModuleInfo::GetNaClModules(extension);
+  if (nacl_modules) {
+    plugins_or_nacl_changed = true;
+    for (NaClModuleInfo::List::const_iterator module = nacl_modules->begin();
+         module != nacl_modules->end();
+         ++module) {
+      RegisterNaClModule(*module);
     }
-
-    if (plugins_or_nacl_changed)
-      PluginService::GetInstance()->PurgePluginListCache(profile_, false);
-
-  } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
-    const Extension* extension =
-        content::Details<UnloadedExtensionInfo>(details)->extension;
-
-    bool plugins_or_nacl_changed = false;
-    if (PluginInfo::HasPlugins(extension)) {
-      const PluginInfo::PluginVector* plugins =
-          PluginInfo::GetPlugins(extension);
-      plugins_or_nacl_changed = true;
-      for (PluginInfo::PluginVector::const_iterator plugin = plugins->begin();
-           plugin != plugins->end(); ++plugin) {
-        PluginService::GetInstance()->ForcePluginShutdown(plugin->path);
-        PluginService::GetInstance()->RefreshPlugins();
-        PluginService::GetInstance()->RemoveExtraPluginPath(plugin->path);
-        ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(
-            plugin->path);
-      }
+    UpdatePluginListWithNaClModules();
+  }
+#endif
+
+  const MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
+  if (handler && !handler->handler_url().empty()) {
+    plugins_or_nacl_changed = true;
+
+    content::WebPluginInfo info;
+    info.type = content::WebPluginInfo::PLUGIN_TYPE_BROWSER_PLUGIN;
+    info.name = base::UTF8ToUTF16(handler->extension_id());
+    info.path = base::FilePath::FromUTF8Unsafe(handler->extension_id());
+
+    for (std::set<std::string>::const_iterator mime_type =
+         handler->mime_type_set().begin();
+         mime_type != handler->mime_type_set().end(); ++mime_type) {
+      content::WebPluginMimeType mime_type_info;
+      mime_type_info.mime_type = *mime_type;
+      info.mime_types.push_back(mime_type_info);
     }
 
-    const NaClModuleInfo::List* nacl_modules =
-        NaClModuleInfo::GetNaClModules(extension);
-    if (nacl_modules) {
-      plugins_or_nacl_changed = true;
-      for (NaClModuleInfo::List::const_iterator module = nacl_modules->begin();
-           module != nacl_modules->end(); ++module) {
-        UnregisterNaClModule(*module);
-      }
-      UpdatePluginListWithNaClModules();
-    }
+    PluginService::GetInstance()->RefreshPlugins();
+    PluginService::GetInstance()->RegisterInternalPlugin(info, true);
+  }
 
-    if (plugins_or_nacl_changed)
-      PluginService::GetInstance()->PurgePluginListCache(profile_, false);
+  if (plugins_or_nacl_changed)
+    PluginService::GetInstance()->PurgePluginListCache(profile_, false);
+}
 
-  } else {
-    NOTREACHED();
+void PluginManager::OnExtensionUnloaded(
+    content::BrowserContext* browser_context,
+    const Extension* extension,
+    UnloadedExtensionInfo::Reason reason) {
+  bool plugins_or_nacl_changed = false;
+  if (PluginInfo::HasPlugins(extension)) {
+    const PluginInfo::PluginVector* plugins = PluginInfo::GetPlugins(extension);
+    plugins_or_nacl_changed = true;
+    for (PluginInfo::PluginVector::const_iterator plugin = plugins->begin();
+         plugin != plugins->end();
+         ++plugin) {
+      PluginService::GetInstance()->ForcePluginShutdown(plugin->path);
+      PluginService::GetInstance()->RefreshPlugins();
+      PluginService::GetInstance()->RemoveExtraPluginPath(plugin->path);
+      ChromePluginServiceFilter::GetInstance()->UnrestrictPlugin(plugin->path);
+    }
+  }
+
+#if !defined(DISABLE_NACL)
+  const NaClModuleInfo::List* nacl_modules =
+      NaClModuleInfo::GetNaClModules(extension);
+  if (nacl_modules) {
+    plugins_or_nacl_changed = true;
+    for (NaClModuleInfo::List::const_iterator module = nacl_modules->begin();
+         module != nacl_modules->end();
+         ++module) {
+      UnregisterNaClModule(*module);
+    }
+    UpdatePluginListWithNaClModules();
   }
+#endif
+
+  const MimeTypesHandler* handler = MimeTypesHandler::GetHandler(extension);
+  if (handler && !handler->handler_url().empty()) {
+    plugins_or_nacl_changed = true;
+    base::FilePath path =
+        base::FilePath::FromUTF8Unsafe(handler->extension_id());
+    PluginService::GetInstance()->UnregisterInternalPlugin(path);
+    PluginService::GetInstance()->ForcePluginShutdown(path);
+    PluginService::GetInstance()->RefreshPlugins();
+  }
+
+  if (plugins_or_nacl_changed)
+    PluginService::GetInstance()->PurgePluginListCache(profile_, false);
 }
 
+#if !defined(DISABLE_NACL)
+
 void PluginManager::RegisterNaClModule(const NaClModuleInfo& info) {
   DCHECK(FindNaClModule(info.url) == nacl_module_list_.end());
   nacl_module_list_.push_front(info);
@@ -192,4 +224,6 @@ NaClModuleInfo::List::iterator PluginManager::FindNaClModule(const GURL& url) {
   return nacl_module_list_.end();
 }
 
+#endif  // !defined(DISABLE_NACL)
+
 }  // namespace extensions