Generate thumbnails in the browser process.

The feature is now behind --enable-in-browser-thumbnailing flag.

The in-browser thumbnailing works as follows:

- The scroll offset is sent from the renderer to the browser.
- The thumbnail is taken when the page info is sent from the renderer process.
- Since thumbnails are generated in the browser, we can avoid the generation when unnecessary (ex. off-the-record mode, or New Tab Page).
- The quality of thumbnails is as good as before, as the patch doesn't change timing heuristics.
- The drawback is that we cannot take thumbnails from background tabs.

New functions are added to ThumbnailGenerator for clipping thumbnails, with tests.

BUG=65936
TEST=add unit tests for thumbnail_generator. confirmed that the thumbnails are updated with and without --enable-in-browser-thumbnailing

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72115 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc
index 151d927..c7df8bf 100644
--- a/chrome/renderer/render_view.cc
+++ b/chrome/renderer/render_view.cc
@@ -1227,7 +1227,13 @@
         TranslateHelper::IsPageTranslatable(&document)));
   }
 
-  OnCaptureThumbnail();
+  // Generate the thumbnail here if the in-browser thumbnailing isn't
+  // enabled. TODO(satorux): Remove this and related code once
+  // crbug.com/65936 is complete.
+  if (!CommandLine::ForCurrentProcess()->HasSwitch(
+          switches::kEnableInBrowserThumbnailing)) {
+    OnCaptureThumbnail();
+  }
 
   if (phishing_delegate_.get())
     phishing_delegate_->FinishedLoad(&contents);
@@ -5175,6 +5181,11 @@
       paint_bounds, dib, location, clip);
 }
 
+gfx::Size RenderView::GetScrollOffset() {
+  WebKit::WebSize scroll_offset = webview()->mainFrame()->scrollOffset();
+  return gfx::Size(scroll_offset.width, scroll_offset.height);
+}
+
 void RenderView::OnClearFocusedNode() {
   if (webview())
     webview()->clearFocusedNode();
diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h
index 2f4729a..fa357c47 100644
--- a/chrome/renderer/render_view.h
+++ b/chrome/renderer/render_view.h
@@ -665,6 +665,7 @@
       TransportDIB** dib,
       gfx::Rect* location,
       gfx::Rect* clip);
+  virtual gfx::Size GetScrollOffset();
   virtual void DidHandleKeyEvent();
   virtual void DidHandleMouseEvent(const WebKit::WebMouseEvent& event);
   virtual void OnSetFocus(bool enable);
diff --git a/chrome/renderer/render_widget.cc b/chrome/renderer/render_widget.cc
index 7fb9e4a..ba01020 100644
--- a/chrome/renderer/render_widget.cc
+++ b/chrome/renderer/render_widget.cc
@@ -585,6 +585,7 @@
   params.resizer_rect = resizer_rect_;
   params.plugin_window_moves.swap(plugin_window_moves_);
   params.flags = next_paint_flags_;
+  params.scroll_offset = GetScrollOffset();
 
   update_reply_pending_ = true;
   Send(new ViewHostMsg_UpdateRect(routing_id_, params));
@@ -920,6 +921,11 @@
   return NULL;
 }
 
+gfx::Size RenderWidget::GetScrollOffset() {
+  // Bare RenderWidgets don't support scroll offset.
+  return gfx::Size(0, 0);
+}
+
 void RenderWidget::SetHidden(bool hidden) {
   if (is_hidden_ == hidden)
     return;
diff --git a/chrome/renderer/render_widget.h b/chrome/renderer/render_widget.h
index 33d0205..71a065e 100644
--- a/chrome/renderer/render_widget.h
+++ b/chrome/renderer/render_widget.h
@@ -219,6 +219,10 @@
       gfx::Rect* location,
       gfx::Rect* clip);
 
+  // Gets the scroll offset of this widget, if this widget has a notion of
+  // scroll offset.
+  virtual gfx::Size GetScrollOffset();
+
   // Sets the "hidden" state of this widget.  All accesses to is_hidden_ should
   // use this method so that we can properly inform the RenderThread of our
   // state.