Introduce ContentBrowserSanityChecker, which currently just adds checks to
validate the sanity of the WebContentsObserver invocations, but which hopefully
will grow to include other too-heavy-for-production-code bug detection logic.

The currently supported check, WebContentsObserverSanityChecker, is a fleshed-
out version of the FrameLifetimeConsistencyChecker previously used in
RenderFrameHost unittests.

This sanity check managed to identify the root cause for bug 438951, so it's
already proved itself useful. Enabling it on all tests should prevent future
regressions of that kind.

To install this on all WebContentses, we need to un-deprecate the
WebContentsImpl::AddCreatedCallback/RemoveCreatedCallback mechanism. To make
that safer, the dangerous methods are moved to the FriendZone (get it?) which is
what I call what Dr. Dobbs calls, stodgily, the "C++ attorney/client idiom"
http://goo.gl/13oS7e. They also now have, cleverly, ForTesting in their names.

Lastly, we change the invocation time of the g_created_callbacks to be at the
end of WebContents::Init, instead of at the end of the ctor, so we're not
inviting tests to muck around with a half-inflated WebContents.

BUG=438951, 444722, 444717
[email protected]

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

Cr-Commit-Position: refs/heads/master@{#309571}
diff --git a/content/browser/web_contents/web_contents_impl.h b/content/browser/web_contents/web_contents_impl.h
index 8369487..873b0e71 100644
--- a/content/browser/web_contents/web_contents_impl.h
+++ b/content/browser/web_contents/web_contents_impl.h
@@ -99,6 +99,8 @@
       public NON_EXPORTED_BASE(NavigationControllerDelegate),
       public NON_EXPORTED_BASE(NavigatorDelegate) {
  public:
+  class FriendZone;
+
   ~WebContentsImpl() override;
 
   static WebContentsImpl* CreateWithOpener(
@@ -665,8 +667,6 @@
   }
 
  private:
-  friend class TestNavigationObserver;
-  friend class WebContentsAddedObserver;
   friend class WebContentsObserver;
   friend class WebContents;  // To implement factory methods.
 
@@ -944,11 +944,6 @@
   void RemoveAllMediaPlayerEntries(RenderFrameHost* render_frame_host,
                                    ActiveMediaPlayerMap* player_map);
 
-  // Adds/removes a callback called on creation of each new WebContents.
-  // Deprecated, about to remove.
-  static void AddCreatedCallback(const CreatedCallback& callback);
-  static void RemoveCreatedCallback(const CreatedCallback& callback);
-
   // Data for core operation ---------------------------------------------------
 
   // Delegate for notifying our owner about stuff. Not owned by us.
@@ -1236,6 +1231,23 @@
   DISALLOW_COPY_AND_ASSIGN(WebContentsImpl);
 };
 
+// Dangerous methods which should never be made part of the public API, so we
+// grant their use only to an explicit friend list (c++ attorney/client idiom).
+class CONTENT_EXPORT WebContentsImpl::FriendZone {
+ private:
+  friend class TestNavigationObserver;
+  friend class WebContentsAddedObserver;
+  friend class ContentBrowserSanityChecker;
+
+  FriendZone();  // Not instantiable.
+
+  // Adds/removes a callback called on creation of each new WebContents.
+  static void AddCreatedCallbackForTesting(const CreatedCallback& callback);
+  static void RemoveCreatedCallbackForTesting(const CreatedCallback& callback);
+
+  DISALLOW_COPY_AND_ASSIGN(FriendZone);
+};
+
 }  // namespace content
 
 #endif  // CONTENT_BROWSER_WEB_CONTENTS_WEB_CONTENTS_IMPL_H_