Introduce default implementation for TabContentsDelegate::ViewSourceForTab.

Review URL: http://codereview.chromium.org/5869002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69267 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/tab_contents/tab_contents.cc b/chrome/browser/tab_contents/tab_contents.cc
index 7769d346..a5cf474 100644
--- a/chrome/browser/tab_contents/tab_contents.cc
+++ b/chrome/browser/tab_contents/tab_contents.cc
@@ -1523,8 +1523,14 @@
 
 void TabContents::ViewSource()
 {
-  if (delegate_)
-    delegate_->ViewSourceForTab(this);
+  if (!delegate_)
+    return;
+
+  NavigationEntry* active_entry = controller().GetActiveEntry();
+  if (!active_entry)
+    return;
+
+  delegate_->ViewSourceForTab(this, active_entry->url());
 }
 
 // Notifies the RenderWidgetHost instance about the fact that the page is
diff --git a/chrome/browser/tab_contents/tab_contents_delegate.cc b/chrome/browser/tab_contents/tab_contents_delegate.cc
index 319407d..3b16cf9 100644
--- a/chrome/browser/tab_contents/tab_contents_delegate.cc
+++ b/chrome/browser/tab_contents/tab_contents_delegate.cc
@@ -5,6 +5,7 @@
 #include "chrome/browser/tab_contents/tab_contents_delegate.h"
 
 #include "chrome/browser/search_engines/template_url.h"
+#include "chrome/common/url_constants.h"
 #include "gfx/rect.h"
 
 std::string TabContentsDelegate::GetNavigationHeaders(const GURL& url) {
@@ -131,7 +132,18 @@
                                        bool show_history) {
 }
 
-void TabContentsDelegate::ViewSourceForTab(TabContents* source) {
+void TabContentsDelegate::ViewSourceForTab(TabContents* source,
+                                           const GURL& page_url) {
+  // Fall back implementation based entirely on the view-source scheme.
+  // It suffers from http://crbug.com/523 and that is why browser overrides
+  // it with proper implementation.
+  GURL url = GURL(chrome::kViewSourceScheme + std::string(":") +
+                      page_url.spec());
+  OpenURLFromTab(source,
+                 url,
+                 GURL(),
+                 NEW_FOREGROUND_TAB,
+                 PageTransition::LINK);
 }
 
 bool TabContentsDelegate::PreHandleKeyboardEvent(
diff --git a/chrome/browser/tab_contents/tab_contents_delegate.h b/chrome/browser/tab_contents/tab_contents_delegate.h
index c9ace5d9..09c560d 100644
--- a/chrome/browser/tab_contents/tab_contents_delegate.h
+++ b/chrome/browser/tab_contents/tab_contents_delegate.h
@@ -249,8 +249,9 @@
                             const NavigationEntry::SSLStatus& ssl,
                             bool show_history);
 
-  // Opens source view for given tab contents.
-  virtual void ViewSourceForTab(TabContents* source);
+  // Opens source view for given tab contents that is navigated to the given
+  // page url.
+  virtual void ViewSourceForTab(TabContents* source, const GURL& page_url);
 
   // Allows delegates to handle keyboard events before sending to the renderer.
   // Returns true if the |event| was handled. Otherwise, if the |event| would be
diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc
index 8e8f55f7..8ab93c0 100644
--- a/chrome/browser/ui/browser.cc
+++ b/chrome/browser/ui/browser.cc
@@ -3051,9 +3051,9 @@
   window()->ShowPageInfo(profile, url, ssl, show_history);
 }
 
-void Browser::ViewSourceForTab(TabContents* contents) {
-  DCHECK(contents);
-  int index = tabstrip_model()->GetWrapperIndex(contents);
+void Browser::ViewSourceForTab(TabContents* source, const GURL& page_url) {
+  DCHECK(source);
+  int index = tabstrip_model()->GetWrapperIndex(source);
   TabContentsWrapper* wrapper = tabstrip_model()->GetTabContentsAt(index);
   ViewSource(wrapper);
 }
@@ -4133,6 +4133,9 @@
   view_source_contents->controller().PruneAllButActive();
   NavigationEntry* active_entry =
       view_source_contents->controller().GetActiveEntry();
+  if (!active_entry)
+    return;
+
   GURL url = GURL(chrome::kViewSourceScheme + std::string(":") +
       active_entry->url().spec());
   active_entry->set_virtual_url(url);
diff --git a/chrome/browser/ui/browser.h b/chrome/browser/ui/browser.h
index e7f6f458..ba12486 100644
--- a/chrome/browser/ui/browser.h
+++ b/chrome/browser/ui/browser.h
@@ -778,7 +778,7 @@
                             const GURL& url,
                             const NavigationEntry::SSLStatus& ssl,
                             bool show_history);
-  virtual void ViewSourceForTab(TabContents* source);
+  virtual void ViewSourceForTab(TabContents* source, const GURL& page_url);
   virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
                                         bool* is_keyboard_shortcut);
   virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event);