From 91314de9d95ccf3996af9688e4489e2b96a802ed Mon Sep 17 00:00:00 2001 From: "jcampan@chromium.org" Date: Thu, 14 Jan 2010 18:01:08 +0000 Subject: One of my previous CL accidentaly reverted the change 35951 for this 2 files. (not sure how it happened). Relanding the change. BUG=None TEST=None Review URL: http://codereview.chromium.org/548035 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@36246 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/renderer/render_view.cc | 111 +++++++++++++++++++++++++++++++---------- chrome/renderer/render_view.h | 14 ++++-- 2 files changed, 93 insertions(+), 32 deletions(-) diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc index ebc1cd6..77429d5 100644 --- a/chrome/renderer/render_view.cc +++ b/chrome/renderer/render_view.cc @@ -17,10 +17,12 @@ #include "base/command_line.h" #include "base/compiler_specific.h" #include "base/field_trial.h" +#include "base/histogram.h" #include "base/process_util.h" #include "base/singleton.h" #include "base/string_piece.h" #include "base/string_util.h" +#include "base/time.h" #include "build/build_config.h" #include "chrome/common/bindings_policy.h" #include "chrome/common/child_process_logging.h" @@ -73,6 +75,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebAccessibilityObject.h" #include "third_party/WebKit/WebKit/chromium/public/WebCString.h" #include "third_party/WebKit/WebKit/chromium/public/WebDataSource.h" +#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h" #include "third_party/WebKit/WebKit/chromium/public/WebDevToolsAgent.h" #include "third_party/WebKit/WebKit/chromium/public/WebDragData.h" #include "third_party/WebKit/WebKit/chromium/public/WebFormElement.h" @@ -178,7 +181,6 @@ using WebKit::WebWorkerClient; // define to write the time necessary for thumbnail/DOM text retrieval, // respectively, into the system debug log -// #define TIME_BITMAP_RETRIEVAL // #define TIME_TEXT_RETRIEVAL // maximum number of characters in the document to index, any text beyond this @@ -239,6 +241,35 @@ static bool UrlMatchesPermissions( return false; } +static bool PaintViewIntoCanvas(WebView* view, + skia::PlatformCanvas& canvas) { + view->layout(); + const WebSize& size = view->size(); + + if (!canvas.initialize(size.width, size.height, true)) + return false; + + view->paint(webkit_glue::ToWebCanvas(&canvas), + WebRect(0, 0, size.width, size.height)); + // TODO: Add a way to snapshot the whole page, not just the currently + // visible part. + + return true; +} + +// Calculates how "boring" a thumbnail is. The boring score is the +// 0,1 ranged percentage of pixels that are the most common +// luma. Higher boring scores indicate that a higher percentage of a +// bitmap are all the same brightness. +static double CalculateBoringScore(SkBitmap* bitmap) { + int histogram[256] = {0}; + color_utils::BuildLumaHistogram(bitmap, histogram); + + int color_count = *std::max_element(histogram, histogram + 256); + int pixel_count = bitmap->width() * bitmap->height(); + return static_cast(color_count) / pixel_count; +} + /////////////////////////////////////////////////////////////////////////////// int32 RenderView::next_page_id_ = 1; @@ -431,6 +462,7 @@ void RenderView::OnMessageReceived(const IPC::Message& message) { IPC_BEGIN_MESSAGE_MAP(RenderView, message) IPC_MESSAGE_HANDLER(ViewMsg_CaptureThumbnail, SendThumbnail) + IPC_MESSAGE_HANDLER(ViewMsg_CaptureSnapshot, SendSnapshot) IPC_MESSAGE_HANDLER(ViewMsg_PrintPages, OnPrintPages) IPC_MESSAGE_HANDLER(ViewMsg_PrintingDone, OnPrintingDone) IPC_MESSAGE_HANDLER(ViewMsg_Navigate, OnNavigate) @@ -560,6 +592,24 @@ void RenderView::SendThumbnail() { Send(new ViewHostMsg_Thumbnail(routing_id_, url, score, thumbnail)); } +void RenderView::SendSnapshot() { + SkBitmap snapshot; + bool error = false; + + WebFrame* main_frame = webview()->mainFrame(); + if (!main_frame) + error = true; + + if (!error && !CaptureSnapshot(webview(), &snapshot)) + error = true; + + DCHECK(error == snapshot.empty()) << + "Snapshot should be empty on error, non-empty otherwise."; + + // Send the snapshot to the browser process. + Send(new ViewHostMsg_Snapshot(routing_id_, snapshot)); +} + void RenderView::OnPrintPages() { DCHECK(webview()); if (webview()) { @@ -676,18 +726,13 @@ bool RenderView::CaptureThumbnail(WebView* view, int h, SkBitmap* thumbnail, ThumbnailScore* score) { -#ifdef TIME_BITMAP_RETRIEVAL - double begin = time_util::GetHighResolutionTimeNow(); -#endif - - view->layout(); - const WebSize& size = view->size(); + base::TimeTicks beginning_time = base::TimeTicks::Now(); skia::PlatformCanvas canvas; - if (!canvas.initialize(size.width, size.height, true)) + + // Paint |view| into |canvas|. + if (!PaintViewIntoCanvas(view, canvas)) return false; - view->paint(webkit_glue::ToWebCanvas(&canvas), - WebRect(0, 0, size.width, size.height)); skia::BitmapPlatformDevice& device = static_cast(canvas.getTopPlatformDevice()); @@ -735,22 +780,28 @@ bool RenderView::CaptureThumbnail(WebView* view, score->boring_score = CalculateBoringScore(thumbnail); -#ifdef TIME_BITMAP_RETRIEVAL - double end = time_util::GetHighResolutionTimeNow(); - char buf[128]; - sprintf_s(buf, "thumbnail in %gms\n", (end - begin) * 1000); - OutputDebugStringA(buf); -#endif + HISTOGRAM_TIMES("Renderer4.Thumbnail", + base::TimeTicks::Now() - beginning_time); return true; } -double RenderView::CalculateBoringScore(SkBitmap* bitmap) { - int histogram[256] = {0}; - color_utils::BuildLumaHistogram(bitmap, histogram); +bool RenderView::CaptureSnapshot(WebView* view, SkBitmap* snapshot) { + base::TimeTicks beginning_time = base::TimeTicks::Now(); - int color_count = *std::max_element(histogram, histogram + 256); - int pixel_count = bitmap->width() * bitmap->height(); - return static_cast(color_count) / pixel_count; + skia::PlatformCanvas canvas; + if (!PaintViewIntoCanvas(view, canvas)) + return false; + + skia::BitmapPlatformDevice& device = + static_cast(canvas.getTopPlatformDevice()); + + const SkBitmap& bitmap = device.accessBitmap(false); + if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config)) + return false; + + HISTOGRAM_TIMES("Renderer4.Snapshot", + base::TimeTicks::Now() - beginning_time); + return true; } void RenderView::OnNavigate(const ViewMsg_Navigate_Params& params) { @@ -2379,6 +2430,8 @@ void RenderView::didFinishDocumentLoad(WebFrame* frame) { } navigation_state->user_script_idle_scheduler()->DidFinishDocumentLoad(); + + frame->addEventListener(frame->document(), "DOMSubtreeModified", this, false); } void RenderView::OnUserScriptIdleTriggered(WebFrame* frame) { @@ -2690,21 +2743,21 @@ webkit_glue::WebPluginDelegate* RenderView::CreatePluginDelegate( } } if (in_process_plugin) { -#if defined(OS_WIN) // In-proc plugins aren't supported on Linux or Mac. if (use_pepper_host) { return WebPluginDelegatePepper::Create( path, *mime_type_to_use, AsWeakPtr(), - gfx::NativeViewFromId(host_window_)); + 0); } else { +#if defined(OS_WIN) // In-proc plugins aren't supported on Linux or Mac. return WebPluginDelegateImpl::Create( path, *mime_type_to_use, gfx::NativeViewFromId(host_window_)); - } #else - NOTIMPLEMENTED(); - return NULL; + NOTIMPLEMENTED(); + return NULL; #endif + } } return new WebPluginDelegateProxy(*mime_type_to_use, AsWeakPtr()); @@ -3045,6 +3098,10 @@ void RenderView::DnsPrefetch(const std::vector& host_names) { Send(new ViewHostMsg_DnsPrefetch(host_names)); } +void RenderView::handleEvent(WebKit::WebEvent* event) { + LOG(ERROR) << "Handling event!"; +} + void RenderView::OnZoom(PageZoom::Function function) { if (!webview()) // Not sure if this can happen, but no harm in being safe. return; diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h index f390af6..05b15dc 100644 --- a/chrome/renderer/render_view.h +++ b/chrome/renderer/render_view.h @@ -40,6 +40,7 @@ #include "testing/gtest/include/gtest/gtest_prod.h" #include "third_party/WebKit/WebKit/chromium/public/WebConsoleMessage.h" #include "third_party/WebKit/WebKit/chromium/public/WebContextMenuData.h" +#include "third_party/WebKit/WebKit/chromium/public/WebEventListener.h" #include "third_party/WebKit/WebKit/chromium/public/WebFrameClient.h" #include "third_party/WebKit/WebKit/chromium/public/WebMediaPlayerAction.h" #include "third_party/WebKit/WebKit/chromium/public/WebNode.h" @@ -115,6 +116,7 @@ typedef base::RefCountedData SharedRenderViewCounter; // communication interface with an embedding application process // class RenderView : public RenderWidget, + public WebKit::WebEventListener, public WebKit::WebViewClient, public WebKit::WebFrameClient, public WebKit::WebPageSerializerClient, @@ -183,6 +185,9 @@ class RenderView : public RenderWidget, virtual void UserMetricsRecordAction(const std::string& action); virtual void DnsPrefetch(const std::vector& host_names); + // WebKit::WebEventListener + virtual void handleEvent(WebKit::WebEvent* event); + // WebKit::WebViewClient virtual WebKit::WebView* createView(WebKit::WebFrame* creator); virtual WebKit::WebWidget* createPopupMenu(bool activatable); @@ -515,11 +520,9 @@ class RenderView : public RenderWidget, SkBitmap* thumbnail, ThumbnailScore* score); - // Calculates how "boring" a thumbnail is. The boring score is the - // 0,1 ranged percentage of pixels that are the most common - // luma. Higher boring scores indicate that a higher percentage of a - // bitmap are all the same brightness. - double CalculateBoringScore(SkBitmap* bitmap); + // Capture a snapshot of a view. This is used to allow an extension + // to get a snapshot of a tab using chrome.tabs.captureVisibleTab(). + bool CaptureSnapshot(WebKit::WebView* view, SkBitmap* snapshot); bool RunJavaScriptMessage(int type, const std::wstring& message, @@ -533,6 +536,7 @@ class RenderView : public RenderWidget, // RenderView IPC message handlers void SendThumbnail(); + void SendSnapshot(); void OnPrintPages(); void OnPrintingDone(int document_cookie, bool success); void OnNavigate(const ViewMsg_Navigate_Params& params); -- cgit v1.1