Move GetSiteForExtensionId() from ExtensionService to extension_util

BUG=351891
[email protected] (c/b/sync_file_system)
[email protected] (c/b/chromeos/file_manager)
(No functional difference for either TBR)

Review URL: https://codereview.chromium.org/198053005

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257297 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/chromeos/file_manager/fileapi_util.cc b/chrome/browser/chromeos/file_manager/fileapi_util.cc
index e9466509..4f7860a 100644
--- a/chrome/browser/chromeos/file_manager/fileapi_util.cc
+++ b/chrome/browser/chromeos/file_manager/fileapi_util.cc
@@ -7,13 +7,13 @@
 #include "base/files/file.h"
 #include "base/files/file_path.h"
 #include "chrome/browser/chromeos/drive/file_system_util.h"
-#include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/extensions/extension_util.h"
 #include "chrome/browser/profiles/profile.h"
 #include "content/public/browser/browser_thread.h"
 #include "content/public/browser/render_view_host.h"
 #include "content/public/browser/site_instance.h"
 #include "content/public/browser/storage_partition.h"
-#include "extensions/browser/extension_system.h"
+#include "extensions/common/extension.h"
 #include "net/base/escape.h"
 #include "url/gurl.h"
 #include "webkit/browser/fileapi/file_system_context.h"
@@ -99,18 +99,13 @@
       result_(new EntryDefinitionList) {
   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
 
-  // May be NULL during unit_tests.
-  ExtensionService* service =
-      extensions::ExtensionSystem::Get(profile)->extension_service();
-  if (service) {
-    // File browser APIs are meant to be used only from extension context, so
-    // the extension's site is the one in whose file system context the virtual
-    // path should be found.
-    GURL site = service->GetSiteForExtensionId(extension_id_);
-    file_system_context_ =
-        content::BrowserContext::GetStoragePartitionForSite(
-            profile, site)->GetFileSystemContext();
-  }
+  // File browser APIs are meant to be used only from extension context, so
+  // the extension's site is the one in whose file system context the virtual
+  // path should be found.
+  GURL site = extensions::util::GetSiteForExtensionId(extension_id_, profile);
+  file_system_context_ =
+      content::BrowserContext::GetStoragePartitionForSite(
+          profile, site)->GetFileSystemContext();
 
   // Deletes the converter, once the scoped pointer gets out of scope. It is
   // either, if the conversion is finished, or ResolveURL() is terminated, and
@@ -223,8 +218,7 @@
 fileapi::FileSystemContext* GetFileSystemContextForExtensionId(
     Profile* profile,
     const std::string& extension_id) {
-  GURL site = extensions::ExtensionSystem::Get(profile)->
-      extension_service()->GetSiteForExtensionId(extension_id);
+  GURL site = extensions::util::GetSiteForExtensionId(extension_id, profile);
   return content::BrowserContext::GetStoragePartitionForSite(profile, site)->
       GetFileSystemContext();
 }
@@ -285,16 +279,10 @@
     const std::string& extension_id,
     const base::FilePath& absolute_path,
     base::FilePath* virtual_path) {
-  ExtensionService* service =
-      extensions::ExtensionSystem::Get(profile)->extension_service();
-  // May be NULL during unit_tests.
-  if (!service)
-    return false;
-
   // File browser APIs are meant to be used only from extension context, so the
   // extension's site is the one in whose file system context the virtual path
   // should be found.
-  GURL site = service->GetSiteForExtensionId(extension_id);
+  GURL site = extensions::util::GetSiteForExtensionId(extension_id, profile);
   fileapi::ExternalFileSystemBackend* backend =
       content::BrowserContext::GetStoragePartitionForSite(profile, site)->
           GetFileSystemContext()->external_backend();
diff --git a/chrome/browser/extensions/data_deleter.cc b/chrome/browser/extensions/data_deleter.cc
index c3eac660..946a90ea 100644
--- a/chrome/browser/extensions/data_deleter.cc
+++ b/chrome/browser/extensions/data_deleter.cc
@@ -6,6 +6,7 @@
 
 #include "chrome/browser/extensions/extension_service.h"
 #include "chrome/browser/extensions/extension_special_storage_policy.h"
+#include "chrome/browser/extensions/extension_util.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/common/extensions/manifest_handlers/app_isolation_info.h"
 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
@@ -15,6 +16,7 @@
 #include "content/public/browser/storage_partition.h"
 #include "extensions/browser/api/storage/storage_frontend.h"
 #include "extensions/browser/extension_prefs.h"
+#include "extensions/browser/extension_system.h"
 #include "extensions/common/constants.h"
 #include "extensions/common/extension.h"
 #include "net/url_request/url_request_context_getter.h"
@@ -24,6 +26,8 @@
 using content::BrowserThread;
 using content::StoragePartition;
 
+namespace extensions {
+
 namespace {
 
 // Helper function that deletes data of a given |storage_origin| in a given
@@ -35,7 +39,7 @@
   DCHECK(profile);
   DCHECK(partition);
 
-  if (origin.SchemeIs(extensions::kExtensionScheme)) {
+  if (origin.SchemeIs(kExtensionScheme)) {
     // TODO(ajwong): Cookies are not properly isolated for
     // chrome-extension:// scheme.  (http://crbug.com/158386).
     //
@@ -66,28 +70,26 @@
 void OnNeedsToGarbageCollectIsolatedStorage(WeakPtr<ExtensionService> es) {
   if (!es)
     return;
-  extensions::ExtensionPrefs::Get(es->profile())
-      ->SetNeedsStorageGarbageCollection(true);
+  ExtensionPrefs::Get(es->profile())->SetNeedsStorageGarbageCollection(true);
 }
 
 } // namespace
 
-namespace extensions {
-
 // static
 void DataDeleter::StartDeleting(Profile* profile, const Extension* extension) {
   DCHECK(profile);
   DCHECK(extension);
 
-  if (extensions::AppIsolationInfo::HasIsolatedStorage(extension)) {
+  if (AppIsolationInfo::HasIsolatedStorage(extension)) {
     BrowserContext::AsyncObliterateStoragePartition(
         profile,
-        profile->GetExtensionService()->GetSiteForExtensionId(extension->id()),
-        base::Bind(&OnNeedsToGarbageCollectIsolatedStorage,
-                   profile->GetExtensionService()->AsWeakPtr()));
+        util::GetSiteForExtensionId(extension->id(), profile),
+        base::Bind(
+            &OnNeedsToGarbageCollectIsolatedStorage,
+            ExtensionSystem::Get(profile)->extension_service()->AsWeakPtr()));
   } else {
     GURL launch_web_url_origin(
-      extensions::AppLaunchInfo::GetLaunchWebURL(extension).GetOrigin());
+        AppLaunchInfo::GetLaunchWebURL(extension).GetOrigin());
 
     StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(
         profile,
diff --git a/chrome/browser/extensions/extension_service.cc b/chrome/browser/extensions/extension_service.cc
index 992d2623..80cb9b7c4 100644
--- a/chrome/browser/extensions/extension_service.cc
+++ b/chrome/browser/extensions/extension_service.cc
@@ -69,7 +69,6 @@
 #include "content/public/browser/notification_service.h"
 #include "content/public/browser/notification_types.h"
 #include "content/public/browser/render_process_host.h"
-#include "content/public/browser/site_instance.h"
 #include "content/public/browser/storage_partition.h"
 #include "content/public/browser/url_data_source.h"
 #include "extensions/browser/app_sorting.h"
@@ -493,12 +492,6 @@
   return registry_->GetExtensionById(id, include_mask);
 }
 
-GURL ExtensionService::GetSiteForExtensionId(const std::string& extension_id) {
-  return content::SiteInstance::GetSiteForURL(
-      profile_,
-      Extension::GetBaseURLFromExtensionId(extension_id));
-}
-
 void ExtensionService::Init() {
   CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
 
@@ -1232,7 +1225,8 @@
   // Revoke external file access for the extension from its file system context.
   // It is safe to access the extension's storage partition at this point. The
   // storage partition may get destroyed only after the extension gets unloaded.
-  GURL site = GetSiteForExtensionId(extension->id());
+  GURL site =
+      extensions::util::GetSiteForExtensionId(extension->id(), profile_);
   fileapi::FileSystemContext* filesystem_context =
       BrowserContext::GetStoragePartitionForSite(profile_, site)->
           GetFileSystemContext();
@@ -2697,7 +2691,9 @@
        it != extensions.end(); ++it) {
     if (extensions::AppIsolationInfo::HasIsolatedStorage(it->get())) {
       active_paths->insert(BrowserContext::GetStoragePartitionForSite(
-          profile_, GetSiteForExtensionId((*it)->id()))->GetPath());
+                               profile_,
+                               extensions::util::GetSiteForExtensionId(
+                                   (*it)->id(), profile()))->GetPath());
     }
   }
 
diff --git a/chrome/browser/extensions/extension_service.h b/chrome/browser/extensions/extension_service.h
index 52fab6b..6a859c5c 100644
--- a/chrome/browser/extensions/extension_service.h
+++ b/chrome/browser/extensions/extension_service.h
@@ -215,10 +215,6 @@
   virtual const extensions::Extension* GetExtensionById(
       const std::string& id, bool include_disabled) const OVERRIDE;
 
-  // Returns the site of the given |extension_id|. Suitable for use with
-  // BrowserContext::GetStoragePartitionForSite().
-  GURL GetSiteForExtensionId(const std::string& extension_id);
-
   // Looks up a terminated (crashed) extension by ID.
   // DEPRECATED: Replace with:
   // ExtensionRegistry::GetExtensionById(id, ExtensionRegistry::TERMINATED).
diff --git a/chrome/browser/extensions/extension_util.cc b/chrome/browser/extensions/extension_util.cc
index 0643b53..04897f5d 100644
--- a/chrome/browser/extensions/extension_util.cc
+++ b/chrome/browser/extensions/extension_util.cc
@@ -171,5 +171,11 @@
   return extension && !extension->is_ephemeral();
 }
 
+GURL GetSiteForExtensionId(const std::string& extension_id,
+                           content::BrowserContext* context) {
+  return content::SiteInstance::GetSiteForURL(
+      context, Extension::GetBaseURLFromExtensionId(extension_id));
+}
+
 }  // namespace util
 }  // namespace extensions
diff --git a/chrome/browser/extensions/extension_util.h b/chrome/browser/extensions/extension_util.h
index d352993..44f0514f 100644
--- a/chrome/browser/extensions/extension_util.h
+++ b/chrome/browser/extensions/extension_util.h
@@ -7,6 +7,8 @@
 
 #include <string>
 
+#include "url/gurl.h"
+
 namespace content {
 class BrowserContext;
 }
@@ -66,6 +68,11 @@
 bool IsExtensionInstalledPermanently(const std::string& extension_id,
                                      content::BrowserContext* context);
 
+// Returns the site of the |extension_id|, given the associated |context|.
+// Suitable for use with BrowserContext::GetStoragePartitionForSite().
+GURL GetSiteForExtensionId(const std::string& extension_id,
+                           content::BrowserContext* context);
+
 }  // namespace util
 }  // namespace extensions
 
diff --git a/chrome/browser/sync_file_system/local/local_file_sync_service.cc b/chrome/browser/sync_file_system/local/local_file_sync_service.cc
index c0842c9..002f83ba 100644
--- a/chrome/browser/sync_file_system/local/local_file_sync_service.cc
+++ b/chrome/browser/sync_file_system/local/local_file_sync_service.cc
@@ -6,6 +6,7 @@
 
 #include "base/stl_util.h"
 #include "chrome/browser/extensions/extension_service.h"
+#include "chrome/browser/extensions/extension_util.h"
 #include "chrome/browser/profiles/profile.h"
 #include "chrome/browser/sync_file_system/file_change.h"
 #include "chrome/browser/sync_file_system/local/local_file_change_tracker.h"
@@ -223,7 +224,8 @@
                    SyncFileMetadata(), FileChangeList());
       return;
     }
-    GURL site_url = extension_service->GetSiteForExtensionId(extension->id());
+    GURL site_url =
+        extensions::util::GetSiteForExtensionId(extension->id(), profile_);
     DCHECK(!site_url.is_empty());
     scoped_refptr<fileapi::FileSystemContext> file_system_context =
         content::BrowserContext::GetStoragePartitionForSite(