Move PepperPluginRegistry to content, while leaving the Chrome specific bits (NaCl, registration of Chrome plugins like pdf/remoting/flash) behind.
Review URL: http://codereview.chromium.org/6869051

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@81959 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/extensions/extension_service.cc b/chrome/browser/extensions/extension_service.cc
index 3ddccd1..37ed6a2 100644
--- a/chrome/browser/extensions/extension_service.cc
+++ b/chrome/browser/extensions/extension_service.cc
@@ -11,6 +11,7 @@
 #include "base/command_line.h"
 #include "base/file_util.h"
 #include "base/metrics/histogram.h"
+#include "base/path_service.h"
 #include "base/stl_util-inl.h"
 #include "base/string16.h"
 #include "base/string_number_conversions.h"
@@ -53,6 +54,7 @@
 #include "chrome/browser/themes/theme_service_factory.h"
 #include "chrome/browser/ui/webui/shown_sections_handler.h"
 #include "chrome/common/child_process_logging.h"
+#include "chrome/common/chrome_paths.h"
 #include "chrome/common/chrome_switches.h"
 #include "chrome/common/extensions/extension.h"
 #include "chrome/common/extensions/extension_constants.h"
@@ -60,7 +62,6 @@
 #include "chrome/common/extensions/extension_file_util.h"
 #include "chrome/common/extensions/extension_l10n_util.h"
 #include "chrome/common/extensions/extension_resource.h"
-#include "chrome/common/pepper_plugin_registry.h"
 #include "chrome/common/pref_names.h"
 #include "chrome/common/url_constants.h"
 #include "content/browser/browser_thread.h"
@@ -69,6 +70,7 @@
 #include "content/common/json_value_serializer.h"
 #include "content/common/notification_service.h"
 #include "content/common/notification_type.h"
+#include "content/common/pepper_plugin_registry.h"
 #include "googleurl/src/gurl.h"
 #include "net/base/registry_controlled_domain.h"
 #include "webkit/database/database_tracker.h"
@@ -139,6 +141,12 @@
 ExtensionService::ExtensionRuntimeData::~ExtensionRuntimeData() {
 }
 
+ExtensionService::NaClModuleInfo::NaClModuleInfo() {
+}
+
+ExtensionService::NaClModuleInfo::~NaClModuleInfo() {
+}
+
 // ExtensionService.
 
 const char* ExtensionService::kInstallDirectoryName = "Extensions";
@@ -1061,13 +1069,12 @@
   bool nacl_modules_changed = false;
   for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
     const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
-    PepperPluginRegistry::GetInstance()->RegisterNaClModule(module.url,
-                                                            module.mime_type);
+    RegisterNaClModule(module.url, module.mime_type);
     nacl_modules_changed = true;
   }
 
   if (nacl_modules_changed)
-    PepperPluginRegistry::GetInstance()->UpdatePluginListWithNaClModules();
+    UpdatePluginListWithNaClModules();
 
   if (plugins_changed || nacl_modules_changed)
     PluginService::GetInstance()->PurgePluginListCache(false);
@@ -1113,12 +1120,12 @@
   bool nacl_modules_changed = false;
   for (size_t i = 0; i < extension->nacl_modules().size(); ++i) {
     const Extension::NaClModuleInfo& module = extension->nacl_modules()[i];
-    PepperPluginRegistry::GetInstance()->UnregisterNaClModule(module.url);
+    UnregisterNaClModule(module.url);
     nacl_modules_changed = true;
   }
 
   if (nacl_modules_changed)
-    PepperPluginRegistry::GetInstance()->UpdatePluginListWithNaClModules();
+    UpdatePluginListWithNaClModules();
 
   if (plugins_changed || nacl_modules_changed)
     PluginService::GetInstance()->PurgePluginListCache(false);
@@ -1984,3 +1991,54 @@
 PropertyBag* ExtensionService::GetPropertyBag(const Extension* extension) {
   return &extension_runtime_data_[extension->id()].property_bag;
 }
+
+void ExtensionService::RegisterNaClModule(const GURL& url,
+                                          const std::string& mime_type) {
+  NaClModuleInfo info;
+  info.url = url;
+  info.mime_type = mime_type;
+
+  DCHECK(FindNaClModule(url) == nacl_module_list_.end());
+  nacl_module_list_.push_front(info);
+}
+
+void ExtensionService::UnregisterNaClModule(const GURL& url) {
+  NaClModuleInfoList::iterator iter = FindNaClModule(url);
+  DCHECK(iter != nacl_module_list_.end());
+  nacl_module_list_.erase(iter);
+}
+
+void ExtensionService::UpdatePluginListWithNaClModules() {
+  FilePath path;
+  PathService::Get(chrome::FILE_NACL_PLUGIN, &path);
+
+  webkit::npapi::PluginList::Singleton()->UnregisterInternalPlugin(path);
+
+  const PepperPluginInfo* pepper_info =
+      PepperPluginRegistry::GetInstance()->GetInfoForPlugin(path);
+  webkit::npapi::WebPluginInfo info = pepper_info->ToWebPluginInfo();
+
+  DCHECK(nacl_module_list_.size() <= 1);
+  for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
+       iter != nacl_module_list_.end(); ++iter) {
+    webkit::npapi::WebPluginMimeType mime_type_info;
+    mime_type_info.mime_type = iter->mime_type;
+    mime_type_info.additional_param_names.push_back(UTF8ToUTF16("nacl"));
+    mime_type_info.additional_param_values.push_back(
+        UTF8ToUTF16(iter->url.spec()));
+    info.mime_types.push_back(mime_type_info);
+  }
+
+  webkit::npapi::PluginList::Singleton()->RefreshPlugins();
+  webkit::npapi::PluginList::Singleton()->RegisterInternalPlugin(info);
+}
+
+ExtensionService::NaClModuleInfoList::iterator
+    ExtensionService::FindNaClModule(const GURL& url) {
+  for (NaClModuleInfoList::iterator iter = nacl_module_list_.begin();
+       iter != nacl_module_list_.end(); ++iter) {
+    if (iter->url == url)
+      return iter;
+  }
+  return nacl_module_list_.end();
+}
diff --git a/chrome/browser/extensions/extension_service.h b/chrome/browser/extensions/extension_service.h
index c63ec78..600d12a 100644
--- a/chrome/browser/extensions/extension_service.h
+++ b/chrome/browser/extensions/extension_service.h
@@ -473,6 +473,15 @@
   };
   typedef std::map<std::string, ExtensionRuntimeData> ExtensionRuntimeDataMap;
 
+  struct NaClModuleInfo {
+    NaClModuleInfo();
+    ~NaClModuleInfo();
+
+    GURL url;
+    std::string mime_type;
+  };
+  typedef std::list<NaClModuleInfo> NaClModuleInfoList;
+
   virtual ~ExtensionService();
 
   // Clear all persistent data that may have been stored by the extension.
@@ -502,6 +511,21 @@
   // Helper method. Loads extension from prefs.
   void LoadInstalledExtension(const ExtensionInfo& info, bool write_to_prefs);
 
+  // We implement some Pepper plug-ins using NaCl to take advantage of NaCl's
+  // strong sandbox. Typically, these NaCl modules are stored in extensions
+  // and registered here. Not all NaCl modules need to register for a MIME
+  // type, just the ones that are responsible for rendering a particular MIME
+  // type, like application/pdf. Note: We only register NaCl modules in the
+  // browser process.
+  void RegisterNaClModule(const GURL& url, const std::string& mime_type);
+  void UnregisterNaClModule(const GURL& url);
+
+  // Call UpdatePluginListWithNaClModules() after registering or unregistering
+  // a NaCl module to see those changes reflected in the PluginList.
+  void UpdatePluginListWithNaClModules();
+
+  NaClModuleInfoList::iterator FindNaClModule(const GURL& url);
+
   // The profile this ExtensionService is part of.
   Profile* profile_;
 
@@ -603,6 +627,8 @@
   // if an update check is needed to install pending extensions.
   bool external_extension_url_added_;
 
+  NaClModuleInfoList nacl_module_list_;
+
   FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,
                            InstallAppsWithUnlimtedStorage);
   FRIEND_TEST_ALL_PREFIXES(ExtensionServiceTest,