OnLazyBackgroundPageActive must ensure the close_sequence_id
is unique to avoid any outstanding OnLazyBackgroundPageIdle
callback will not incorrectly send ExtensionMsg_Suspend.

Two errors are fixed in this patch:

1. A unique last_background_close_sequence_id_ is maintained
by ProcessManager. This corrects the error that previously a
BackgroundPageData struct was cleared and had its ID reset,
allowing it to then be incremented again to match the ID of
an outstanding callback.

2. OnLazyBackgroundPageActive previously checked for an
ExtensionHost object. However, when first launching an app
the methods are called OnLazyBackgroundPageActive then
OnBackgroundHostCreated.  Thus, the
last_background_close_sequence_id_ is now incremented
in OnLazyBackgroundPageActive without checking for an
ExtensionHost object.

BUG=316097
TEST=Manual test required as per https://code.google.com/p/chromium/issues/detail?id=316097#c10

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@283053 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/extensions/browser/process_manager.h b/extensions/browser/process_manager.h
index 664c942..e580bbd 100644
--- a/extensions/browser/process_manager.h
+++ b/extensions/browser/process_manager.h
@@ -108,7 +108,7 @@
 
   // Handles a response to the ShouldSuspend message, used for lazy background
   // pages.
-  void OnShouldSuspendAck(const std::string& extension_id, int sequence_id);
+  void OnShouldSuspendAck(const std::string& extension_id, uint64 sequence_id);
 
   // Same as above, for the Suspend message.
   void OnSuspendAck(const std::string& extension_id);
@@ -198,10 +198,10 @@
   // These are called when the extension transitions between idle and active.
   // They control the process of closing the background page when idle.
   void OnLazyBackgroundPageIdle(const std::string& extension_id,
-                                int sequence_id);
+                                uint64 sequence_id);
   void OnLazyBackgroundPageActive(const std::string& extension_id);
   void CloseLazyBackgroundPageNow(const std::string& extension_id,
-                                  int sequence_id);
+                                  uint64 sequence_id);
 
   // Potentially registers a RenderViewHost, if it is associated with an
   // extension. Does nothing if this is not an extension renderer.
@@ -246,6 +246,22 @@
 
   ObserverList<ProcessManagerObserver> observer_list_;
 
+  // ID Counter used to set ProcessManager::BackgroundPageData close_sequence_id
+  // members. These IDs are tracked per extension in background_page_data_ and
+  // are used to verify that nothing has interrupted the process of closing a
+  // lazy background process.
+  //
+  // Any interruption obtains a new ID by incrementing
+  // last_background_close_sequence_id_ and storing it in background_page_data_
+  // for a particular extension. Callbacks and round-trip IPC messages store the
+  // value of the extension's close_sequence_id at the beginning of the process.
+  // Thus comparisons can be done to halt when IDs no longer match.
+  //
+  // This counter provides unique IDs even when BackgroundPageData objects are
+  // reset.
+  uint64 last_background_close_sequence_id_;
+
+  // Must be last member, see doc on WeakPtrFactory.
   base::WeakPtrFactory<ProcessManager> weak_ptr_factory_;
 
   DISALLOW_COPY_AND_ASSIGN(ProcessManager);