Tracking push events for lucid sleep

Phase 2 of lucid sleep involves allowing GCM-enabled apps and extensions
to wake up the system to process push messages.  This brings up the
problem of knowing when those apps/extensions are done processing the
event so that the system can go back to sleep.

This CL introduces a new class whose responsibility is to monitor
GCM-enabled apps and extensions and delay the system suspend while they
are processing push messages.  Additionally, network requests that are
started while the app/extension is processing a push message are
considered related to that message and can further delay the system
suspend even after the push message itself has been acked by the
extension.

BUG=397328

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

Cr-Commit-Position: refs/heads/master@{#314219}
diff --git a/extensions/browser/extension_host.h b/extensions/browser/extension_host.h
index 4f81501..62925fc3 100644
--- a/extensions/browser/extension_host.h
+++ b/extensions/browser/extension_host.h
@@ -5,11 +5,13 @@
 #ifndef EXTENSIONS_BROWSER_EXTENSION_HOST_H_
 #define EXTENSIONS_BROWSER_EXTENSION_HOST_H_
 
+#include <set>
 #include <string>
 #include <vector>
 
 #include "base/logging.h"
 #include "base/memory/scoped_ptr.h"
+#include "base/observer_list.h"
 #include "base/timer/elapsed_timer.h"
 #include "content/public/browser/notification_observer.h"
 #include "content/public/browser/notification_registrar.h"
@@ -31,6 +33,7 @@
 namespace extensions {
 class Extension;
 class ExtensionHostDelegate;
+class ExtensionHostObserver;
 class WindowController;
 
 // This class is the browser component of an extension component's RenderView.
@@ -75,6 +78,22 @@
   // (can be NULL). This happens delayed to avoid locking the UI.
   void CreateRenderViewSoon();
 
+  // Typical observer interface.
+  void AddObserver(ExtensionHostObserver* observer);
+  void RemoveObserver(ExtensionHostObserver* observer);
+
+  // Called when an IPC message is dispatched to the RenderView associated with
+  // this ExtensionHost.
+  void OnMessageDispatched(const std::string& event_name, int message_id);
+
+  // Called by the ProcessManager when a network request is started by the
+  // extension corresponding to this ExtensionHost.
+  void OnNetworkRequestStarted(uint64 request_id);
+
+  // Called by the ProcessManager when a previously started network request is
+  // finished.
+  void OnNetworkRequestDone(uint64 request_id);
+
   // content::WebContentsObserver
   bool OnMessageReceived(const IPC::Message& message) override;
   void RenderViewCreated(content::RenderViewHost* render_view_host) override;
@@ -135,7 +154,7 @@
 
   // Message handlers.
   void OnRequest(const ExtensionHostMsg_Request_Params& params);
-  void OnEventAck();
+  void OnEventAck(int message_id);
   void OnIncrementLazyKeepaliveCount();
   void OnDecrementLazyKeepaliveCount();
 
@@ -168,6 +187,9 @@
   // The original URL of the page being hosted.
   GURL initial_url_;
 
+  // Messages sent out to the renderer that have not been acknowledged yet.
+  std::set<int> unacked_messages_;
+
   content::NotificationRegistrar registrar_;
 
   ExtensionFunctionDispatcher extension_function_dispatcher_;
@@ -178,6 +200,8 @@
   // Used to measure how long it's been since the host was created.
   base::ElapsedTimer since_created_;
 
+  ObserverList<ExtensionHostObserver> observer_list_;
+
   DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
 };