Extension SW: make renderer aware of ActivationSequence.

This CL sends ActivationSequence info on extension load
(through ExtensionMsg_Load IPC) to renderers. Therefore, worker
notifications from renderer now carry the sequence back to
browser process / ServiceWorkerTaskQueue. This makes renderer
notifications deterministic-ally map-able to an extension load sequence,
as opposed to guessing an ActivationSequence (esp. when an extension is
deactivated and then activated).

This CL also drops the knowledge of a WorkerState upon extension
deactivation as we can safely ignore expired renderer notifications
arriving after that.

RendererExtensionRegistry stores ActivationSequences for all
worker based extensions.

Bug: 1022247, 1003244
Change-Id: I1f1bc013f0993bbb3927674576f2844d6354777f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1970854
Reviewed-by: Sam McNally <[email protected]>
Reviewed-by: Devlin <[email protected]>
Commit-Queue: Istiaque Ahmed <[email protected]>
Cr-Commit-Position: refs/heads/master@{#727832}
diff --git a/extensions/browser/service_worker_task_queue.h b/extensions/browser/service_worker_task_queue.h
index 2e5e215b..76b2285 100644
--- a/extensions/browser/service_worker_task_queue.h
+++ b/extensions/browser/service_worker_task_queue.h
@@ -72,6 +72,12 @@
 class ServiceWorkerTaskQueue : public KeyedService,
                                public LazyContextTaskQueue {
  public:
+  // Unique identifier for an extension's activation->deactivation span.
+  // TODO(lazyboy): Move this under extensions/common/ for consistency, so that
+  // renderer process can use this instead of using "int" directly. We'd also
+  // want StrongAlias for this.
+  using ActivationSequence = int;
+
   explicit ServiceWorkerTaskQueue(content::BrowserContext* browser_context);
   ~ServiceWorkerTaskQueue() override;
 
@@ -102,16 +108,24 @@
   // has completed executing.
   void DidStartServiceWorkerContext(int render_process_id,
                                     const ExtensionId& extension_id,
+                                    int activation_sequence,
                                     const GURL& service_worker_scope,
                                     int64_t service_worker_version_id,
                                     int thread_id);
   // Called once an extension Service Worker was destroyed.
   void DidStopServiceWorkerContext(int render_process_id,
                                    const ExtensionId& extension_id,
+                                   int activation_sequence,
                                    const GURL& service_worker_scope,
                                    int64_t service_worker_version_id,
                                    int thread_id);
 
+  // Returns the current ActivationSequence for an extension, if the extension
+  // is currently activated. Returns base::nullopt if the extension isn't
+  // activated.
+  base::Optional<ActivationSequence> GetCurrentSequence(
+      const ExtensionId& extension_id) const;
+
   class TestObserver {
    public:
     TestObserver();
@@ -130,14 +144,9 @@
   static void SetObserverForTest(TestObserver* observer);
 
  private:
-  // Unique identifier for an extension's activation->deactivation span.
-  using ActivationSequence = int;
   using SequencedContextId = std::pair<LazyContextId, ActivationSequence>;
 
-  // Key used to identify a WorkerState within the worker container.
-  using WorkerKey = std::pair<LazyContextId, WorkerId>;
-
-  struct WorkerState;
+  class WorkerState;
 
   static void DidStartWorkerForScopeOnCoreThread(
       const SequencedContextId& context_id,
@@ -184,40 +193,19 @@
   // If the worker with |context_id| has seen worker start
   // (DidStartWorkerForScope) and load (DidStartServiceWorkerContext) then runs
   // all pending tasks for that worker.
-  void RunPendingTasksIfWorkerReady(const LazyContextId& context_id,
-                                    int64_t version_id,
-                                    int process_id,
-                                    int thread_id);
-
-  void ClearPendingTasks(const SequencedContextId& context_id);
+  void RunPendingTasksIfWorkerReady(const SequencedContextId& context_id);
 
   // Returns true if |sequence| is the current activation sequence for
   // |extension_id|.
   bool IsCurrentSequence(const ExtensionId& extension_id,
                          ActivationSequence sequence) const;
 
-  // Returns the current ActivationSequence for an extension, if the extension
-  // is currently activated. Returns base::nullopt if the extension isn't
-  // activated.
-  base::Optional<ActivationSequence> GetCurrentSequence(
-      const ExtensionId& extension_id) const;
-
-  WorkerState* GetOrCreateWorkerState(const WorkerKey& worker_key);
-  WorkerState* GetWorkerState(const WorkerKey& worker_key);
-  void ClearBrowserReadyForWorkers(const LazyContextId& context_id,
-                                   ActivationSequence sequence);
+  WorkerState* GetWorkerState(const SequencedContextId& context_id);
 
   ActivationSequence next_activation_sequence_ = 0;
 
-  // Set of extension ids that hasn't completed Service Worker registration.
-  std::set<SequencedContextId> pending_registrations_;
-
-  // The state of each workers we know about.
-  std::map<WorkerKey, WorkerState> worker_state_map_;
-
-  // Pending tasks for a |LazyContextId| with an ActivationSequence.
-  // These tasks will be run once the corresponding worker becomes ready.
-  std::map<SequencedContextId, std::vector<PendingTask>> pending_tasks_;
+  // The state of worker of each activated extension.
+  std::map<SequencedContextId, WorkerState> worker_state_map_;
 
   content::BrowserContext* const browser_context_ = nullptr;