Add ref count to service workers for extension API.

We need a way to keep a service worker alive
1) during extension function's request->response roundtrip completes.
2) when an event is about to be dispatched to a (stopped) service worker.

This CL shows a way to do #1. #2 can follow later.

The CL adds plumbing to expose functions to increment/decrement ref
counting to an ServiceWorkerVersion. This is done by adding a way to
add "external requests" to a ServiceWorkerVersion: when a worker has
external requests pending, it will be considered to be in working state,
i.e ServiceWorkerVersion::HasWork() will return true.
The public interface is exposed through ServiceWorkerContext. And the
interface methods expect a GUID/nonce for each such requests from service
worker renderer:
ServiceWorkerContext::StartingExternalRequest() and
ServiceWorkerContext::FinishedExternalRequest()

Extension APIs that are expected to be long running aren't handled in
this CL. For example: an extension API showing a dialog to user that
waits for user action.

BUG=602442
Test=There's no easy way to test it without tweaking the code, I've
used the following steps to make sure that we keep SW alive when an extension
API is in-flight:
Change the stop worker idle timeout and worker timeout to sth small, e.g. 3s.
Call an extension function that runs for 7s (> 3s + 3s). Without the CL, the
extension function's callback won't be called because the worker would shut
down after 6s.
The added test ServiceWorkerTest.WorkerRefCount tests this at a bit lower level:
by checking ref count (= count of external requests for a ServiceWorkerVersion).

Review-Url: https://codereview.chromium.org/2166523003
Cr-Commit-Position: refs/heads/master@{#425824}
diff --git a/extensions/browser/extension_util.cc b/extensions/browser/extension_util.cc
index 4981790a..142e1e4 100644
--- a/extensions/browser/extension_util.cc
+++ b/extensions/browser/extension_util.cc
@@ -4,6 +4,8 @@
 
 #include "extensions/browser/extension_util.h"
 
+#include "content/public/browser/browser_context.h"
+#include "content/public/browser/site_instance.h"
 #include "extensions/browser/extension_prefs.h"
 #include "extensions/browser/extension_registry.h"
 #include "extensions/common/manifest_handlers/app_isolation_info.h"
@@ -43,5 +45,16 @@
           extension->location() == Manifest::COMPONENT);
 }
 
+content::StoragePartition* GetStoragePartitionForExtensionId(
+    const std::string& extension_id,
+    content::BrowserContext* browser_context) {
+  GURL site_url = content::SiteInstance::GetSiteForURL(
+      browser_context, Extension::GetBaseURLFromExtensionId(extension_id));
+  content::StoragePartition* storage_partition =
+      content::BrowserContext::GetStoragePartitionForSite(browser_context,
+                                                          site_url);
+  return storage_partition;
+}
+
 }  // namespace util
 }  // namespace extensions