Avoid creating StoragePartitions unnecessarily

Instead check several conditions earlier to avoid getting (and thus creating)
StoragePartitions that are never used. Also adds a new parameter to
BrowserContext::GetStoragePartition to allow checking existence without
creating the StoragePartition.

Finally remove the hack in BackgroundFetchContext which preserved a
scoped_refptr to URLRequestContextGetter to ensure it was deleted on the UI
thread, now that the StoragePartition is not created and so won't create it.

Bug: 762960
Change-Id: I57055c19f485be706485477125d1434e5ae99181
Reviewed-on: https://chromium-review.googlesource.com/654874
Commit-Queue: Dan Elphick <[email protected]>
Reviewed-by: Alex Moshchuk <[email protected]>
Cr-Commit-Position: refs/heads/master@{#501226}
diff --git a/content/browser/browser_context.cc b/content/browser/browser_context.cc
index 42b00290..8cfe16c 100644
--- a/content/browser/browser_context.cc
+++ b/content/browser/browser_context.cc
@@ -118,14 +118,16 @@
     BrowserContext* browser_context,
     const std::string& partition_domain,
     const std::string& partition_name,
-    bool in_memory) {
+    bool in_memory,
+    bool can_create) {
   StoragePartitionImplMap* partition_map =
       GetStoragePartitionMap(browser_context);
 
   if (browser_context->IsOffTheRecord())
     in_memory = true;
 
-  return partition_map->Get(partition_domain, partition_name, in_memory);
+  return partition_map->Get(partition_domain, partition_name, in_memory,
+                            can_create);
 }
 
 void SaveSessionStateOnIOThread(
@@ -257,7 +259,8 @@
 
 StoragePartition* BrowserContext::GetStoragePartition(
     BrowserContext* browser_context,
-    SiteInstance* site_instance) {
+    SiteInstance* site_instance,
+    bool can_create) {
   std::string partition_domain;
   std::string partition_name;
   bool in_memory = false;
@@ -268,13 +271,14 @@
         &partition_domain, &partition_name, &in_memory);
   }
 
-  return GetStoragePartitionFromConfig(
-      browser_context, partition_domain, partition_name, in_memory);
+  return GetStoragePartitionFromConfig(browser_context, partition_domain,
+                                       partition_name, in_memory, can_create);
 }
 
 StoragePartition* BrowserContext::GetStoragePartitionForSite(
     BrowserContext* browser_context,
-    const GURL& site) {
+    const GURL& site,
+    bool can_create) {
   std::string partition_domain;
   std::string partition_name;
   bool in_memory;
@@ -283,8 +287,8 @@
       browser_context, site, true, &partition_domain, &partition_name,
       &in_memory);
 
-  return GetStoragePartitionFromConfig(
-      browser_context, partition_domain, partition_name, in_memory);
+  return GetStoragePartitionFromConfig(browser_context, partition_domain,
+                                       partition_name, in_memory, can_create);
 }
 
 void BrowserContext::ForEachStoragePartition(