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_function_dispatcher.h b/extensions/browser/extension_function_dispatcher.h
index 074c241..9aa833f4 100644
--- a/extensions/browser/extension_function_dispatcher.h
+++ b/extensions/browser/extension_function_dispatcher.h
@@ -101,7 +101,8 @@
 
   // Called when an ExtensionFunction is done executing, after it has sent
   // a response (if any) to the extension.
-  void OnExtensionFunctionCompleted(const Extension* extension);
+  void OnExtensionFunctionCompleted(const Extension* extension,
+                                    bool is_from_service_worker);
 
   // See the Delegate class for documentation on these methods.
   // TODO(devlin): None of these belong here. We should kill