Fix a crash bug in memory_utils.cc caused by assuming that the
RenderViewHost's delegate is a WebContents.
Review URL: http://codereview.chromium.org/23025

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@9789 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/memory_details.cc b/chrome/browser/memory_details.cc
index e1279fe..927b2eb 100644
--- a/chrome/browser/memory_details.cc
+++ b/chrome/browser/memory_details.cc
@@ -222,9 +222,9 @@
           continue;
 
         RenderViewHost* host = static_cast<RenderViewHost*>(widget);
-        TabContents* contents =
-            static_cast<WebContents*>(host->delegate());
-        DCHECK(contents);
+        TabContents* contents = NULL;
+        if (host->delegate())
+          contents = host->delegate()->GetAsWebContents();
         if (!contents)
           continue;
         std::wstring title = contents->GetTitle();
diff --git a/chrome/browser/renderer_host/render_view_host_delegate.h b/chrome/browser/renderer_host/render_view_host_delegate.h
index b701b21d..7cfe6f9 100644
--- a/chrome/browser/renderer_host/render_view_host_delegate.h
+++ b/chrome/browser/renderer_host/render_view_host_delegate.h
@@ -151,6 +151,9 @@
   // Retrieves the profile to be used.
   virtual Profile* GetProfile() const = 0;
 
+  // Return this object cast to a WebContents, if it is one.
+  virtual WebContents* GetAsWebContents() { return NULL; }
+
   // The RenderView is being constructed (message sent to the renderer process
   // to construct a RenderView).  Now is a good time to send other setup events
   // to the RenderView.  This precedes any other commands to the RenderView.
diff --git a/chrome/browser/tab_contents/tab_util.cc b/chrome/browser/tab_contents/tab_util.cc
index 99432797..3437f1c 100644
--- a/chrome/browser/tab_contents/tab_util.cc
+++ b/chrome/browser/tab_contents/tab_util.cc
@@ -34,6 +34,5 @@
   if (!render_view_host)
     return NULL;
 
-  return static_cast<WebContents*>(render_view_host->delegate());
+  return render_view_host->delegate()->GetAsWebContents();
 }
-
diff --git a/chrome/browser/tab_contents/web_contents.h b/chrome/browser/tab_contents/web_contents.h
index 98150a0..275f6f3f 100644
--- a/chrome/browser/tab_contents/web_contents.h
+++ b/chrome/browser/tab_contents/web_contents.h
@@ -246,6 +246,7 @@
   virtual RenderViewHostDelegate::View* GetViewDelegate() const;
   virtual RenderViewHostDelegate::Save* GetSaveDelegate() const;
   virtual Profile* GetProfile() const;
+  virtual WebContents* GetAsWebContents() { return this; }
   virtual void RendererReady(RenderViewHost* render_view_host);
   virtual void RendererGone(RenderViewHost* render_view_host);
   virtual void DidNavigate(RenderViewHost* render_view_host,