diff options
author | justinlin@chromium.org <justinlin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-23 16:30:49 +0000 |
---|---|---|
committer | justinlin@chromium.org <justinlin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-23 16:30:49 +0000 |
commit | 44f774648086e268e81b5d386102bfd6f08b219f (patch) | |
tree | 495e7914f1804c4904a4778644f9a90418615a57 | |
parent | 4710df256c5d79d6144d807fd2459065521b0f3d (diff) | |
download | chromium_src-44f774648086e268e81b5d386102bfd6f08b219f.zip chromium_src-44f774648086e268e81b5d386102bfd6f08b219f.tar.gz chromium_src-44f774648086e268e81b5d386102bfd6f08b219f.tar.bz2 |
Restore SnapshotTabHelper temporarily until dependencies are fixed (reverts deletions in r189969). Do not use this class in Chrome.
BUG=223374
TBR=jam@, sky@
Review URL: https://codereview.chromium.org/12770014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@190027 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/ui/browser_tab_contents.cc | 2 | ||||
-rw-r--r-- | chrome/browser/ui/snapshot_tab_helper.cc | 47 | ||||
-rw-r--r-- | chrome/browser/ui/snapshot_tab_helper.h | 38 | ||||
-rw-r--r-- | chrome/chrome_browser_ui.gypi | 2 | ||||
-rw-r--r-- | chrome/common/chrome_content_client.cc | 6 | ||||
-rw-r--r-- | chrome/common/render_messages.h | 8 | ||||
-rw-r--r-- | chrome/renderer/chrome_render_view_observer.cc | 48 | ||||
-rw-r--r-- | chrome/renderer/chrome_render_view_observer.h | 5 |
8 files changed, 156 insertions, 0 deletions
diff --git a/chrome/browser/ui/browser_tab_contents.cc b/chrome/browser/ui/browser_tab_contents.cc index fad09c1..642370f 100644 --- a/chrome/browser/ui/browser_tab_contents.cc +++ b/chrome/browser/ui/browser_tab_contents.cc @@ -38,6 +38,7 @@ #include "chrome/browser/ui/sad_tab_helper.h" #include "chrome/browser/ui/search/search_tab_helper.h" #include "chrome/browser/ui/search_engines/search_engine_tab_helper.h" +#include "chrome/browser/ui/snapshot_tab_helper.h" #include "chrome/browser/ui/sync/tab_contents_synced_tab_delegate.h" #include "chrome/browser/ui/tab_contents/core_tab_helper.h" #include "chrome/browser/ui/web_contents_modal_dialog_manager.h" @@ -144,6 +145,7 @@ void BrowserTabContents::AttachTabHelpers(WebContents* web_contents) { safe_browsing::SafeBrowsingTabObserver::CreateForWebContents(web_contents); SearchEngineTabHelper::CreateForWebContents(web_contents); chrome::search::SearchTabHelper::CreateForWebContents(web_contents); + SnapshotTabHelper::CreateForWebContents(web_contents); SSLTabHelper::CreateForWebContents(web_contents); TabContentsSyncedTabDelegate::CreateForWebContents(web_contents); TabSpecificContentSettings::CreateForWebContents(web_contents); diff --git a/chrome/browser/ui/snapshot_tab_helper.cc b/chrome/browser/ui/snapshot_tab_helper.cc new file mode 100644 index 0000000..76ed5ca --- /dev/null +++ b/chrome/browser/ui/snapshot_tab_helper.cc @@ -0,0 +1,47 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/ui/snapshot_tab_helper.h" + +#include "chrome/common/chrome_notification_types.h" +#include "chrome/common/render_messages.h" +#include "content/public/browser/notification_service.h" +#include "content/public/browser/web_contents.h" + +using content::WebContents; + +DEFINE_WEB_CONTENTS_USER_DATA_KEY(SnapshotTabHelper); + +SnapshotTabHelper::SnapshotTabHelper(WebContents* web_contents) + : content::WebContentsObserver(web_contents) { +} + +SnapshotTabHelper::~SnapshotTabHelper() { +} + +void SnapshotTabHelper::CaptureSnapshot() { + Send(new ChromeViewMsg_CaptureSnapshot(routing_id())); +} + +//////////////////////////////////////////////////////////////////////////////// +// WebContentsObserver overrides + +bool SnapshotTabHelper::OnMessageReceived(const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(SnapshotTabHelper, message) + IPC_MESSAGE_HANDLER(ChromeViewHostMsg_Snapshot, OnSnapshot) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +//////////////////////////////////////////////////////////////////////////////// +// Internal helpers + +void SnapshotTabHelper::OnSnapshot(const SkBitmap& bitmap) { + content::NotificationService::current()->Notify( + chrome::NOTIFICATION_TAB_SNAPSHOT_TAKEN, + content::Source<WebContents>(web_contents()), + content::Details<const SkBitmap>(&bitmap)); +} diff --git a/chrome/browser/ui/snapshot_tab_helper.h b/chrome/browser/ui/snapshot_tab_helper.h new file mode 100644 index 0000000..bde61b1 --- /dev/null +++ b/chrome/browser/ui/snapshot_tab_helper.h @@ -0,0 +1,38 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_UI_SNAPSHOT_TAB_HELPER_H_ +#define CHROME_BROWSER_UI_SNAPSHOT_TAB_HELPER_H_ + +#include "content/public/browser/web_contents_observer.h" +#include "content/public/browser/web_contents_user_data.h" + +class SkBitmap; + +// Per-tab class to handle snapshot functionality. +class SnapshotTabHelper + : public content::WebContentsObserver, + public content::WebContentsUserData<SnapshotTabHelper> { + public: + virtual ~SnapshotTabHelper(); + + // Captures a snapshot of the page. + void CaptureSnapshot(); + + private: + explicit SnapshotTabHelper(content::WebContents* web_contents); + friend class content::WebContentsUserData<SnapshotTabHelper>; + + // content::WebContentsObserver overrides: + virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; + + // Internal helpers ---------------------------------------------------------- + + // Message handler. + void OnSnapshot(const SkBitmap& bitmap); + + DISALLOW_COPY_AND_ASSIGN(SnapshotTabHelper); +}; + +#endif // CHROME_BROWSER_UI_SNAPSHOT_TAB_HELPER_H_ diff --git a/chrome/chrome_browser_ui.gypi b/chrome/chrome_browser_ui.gypi index 1e9bed1..1671876 100644 --- a/chrome/chrome_browser_ui.gypi +++ b/chrome/chrome_browser_ui.gypi @@ -1306,6 +1306,8 @@ 'browser/ui/simple_message_box.h', 'browser/ui/singleton_tabs.cc', 'browser/ui/singleton_tabs.h', + 'browser/ui/snapshot_tab_helper.cc', + 'browser/ui/snapshot_tab_helper.h', 'browser/ui/startup/autolaunch_prompt.cc', 'browser/ui/startup/autolaunch_prompt_win.cc', 'browser/ui/startup/autolaunch_prompt.h', diff --git a/chrome/common/chrome_content_client.cc b/chrome/common/chrome_content_client.cc index 0bcc7a3..aaee0d8 100644 --- a/chrome/common/chrome_content_client.cc +++ b/chrome/common/chrome_content_client.cc @@ -399,6 +399,12 @@ bool ChromeContentClient::CanHandleWhileSwappedOut( // Any Chrome-specific messages (apart from those listed in // CanSendWhileSwappedOut) that must be handled by the browser when sent from // swapped out renderers. + switch (msg.type()) { + case ChromeViewHostMsg_Snapshot::ID: + return true; + default: + break; + } return false; } diff --git a/chrome/common/render_messages.h b/chrome/common/render_messages.h index 57e54f7..39e1077 100644 --- a/chrome/common/render_messages.h +++ b/chrome/common/render_messages.h @@ -263,6 +263,10 @@ IPC_MESSAGE_ROUTED4(ChromeViewMsg_WebUIJavaScript, int, /* ID */ bool /* If true, result is sent back. */) +// Tells the render view to capture a thumbnail image of the page. The +// render view responds with a ChromeViewHostMsg_Snapshot. +IPC_MESSAGE_ROUTED0(ChromeViewMsg_CaptureSnapshot) + // Set the content setting rules stored by the renderer. IPC_MESSAGE_CONTROL1(ChromeViewMsg_SetContentSettingRules, RendererContentSettingRules /* rules */) @@ -551,6 +555,10 @@ IPC_MESSAGE_ROUTED1(ChromeViewHostMsg_CouldNotLoadPlugin, IPC_MESSAGE_ROUTED1(ChromeViewHostMsg_NPAPINotSupported, std::string /* identifer */) +// Send a snapshot of the tab contents to the render host. +IPC_MESSAGE_ROUTED1(ChromeViewHostMsg_Snapshot, + SkBitmap /* bitmap */) + // A message for an external host. IPC_MESSAGE_ROUTED3(ChromeViewHostMsg_ForwardMessageToExternalHost, std::string /* message */, diff --git a/chrome/renderer/chrome_render_view_observer.cc b/chrome/renderer/chrome_render_view_observer.cc index 48651a7..020979a 100644 --- a/chrome/renderer/chrome_render_view_observer.cc +++ b/chrome/renderer/chrome_render_view_observer.cc @@ -194,6 +194,7 @@ bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) { bool handled = true; IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message) IPC_MESSAGE_HANDLER(ChromeViewMsg_WebUIJavaScript, OnWebUIJavaScript) + IPC_MESSAGE_HANDLER(ChromeViewMsg_CaptureSnapshot, OnCaptureSnapshot) IPC_MESSAGE_HANDLER(ChromeViewMsg_HandleMessageFromExternalHost, OnHandleMessageFromExternalHost) IPC_MESSAGE_HANDLER(ChromeViewMsg_JavaScriptStressTestControl, @@ -235,6 +236,24 @@ void ChromeRenderViewObserver::OnWebUIJavaScript( webui_javascript_->notify_result = notify_result; } +void ChromeRenderViewObserver::OnCaptureSnapshot() { + SkBitmap snapshot; + bool error = false; + + WebFrame* main_frame = render_view()->GetWebView()->mainFrame(); + if (!main_frame) + error = true; + + if (!error && !CaptureSnapshot(render_view()->GetWebView(), &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 ChromeViewHostMsg_Snapshot(routing_id(), snapshot)); +} + void ChromeRenderViewObserver::OnHandleMessageFromExternalHost( const std::string& message, const std::string& origin, @@ -776,6 +795,35 @@ void ChromeRenderViewObserver::CaptureText(WebFrame* frame, } } +bool ChromeRenderViewObserver::CaptureSnapshot(WebView* view, + SkBitmap* snapshot) { + base::TimeTicks beginning_time = base::TimeTicks::Now(); + + view->layout(); + const WebSize& size = view->size(); + + skia::RefPtr<SkCanvas> canvas = skia::AdoptRef( + skia::CreatePlatformCanvas( + size.width, size.height, true, NULL, skia::RETURN_NULL_ON_FAILURE)); + if (!canvas) + return false; + + view->paint(webkit_glue::ToWebCanvas(canvas.get()), + WebRect(0, 0, size.width, size.height)); + // TODO: Add a way to snapshot the whole page, not just the currently + // visible part. + + SkDevice* device = skia::GetTopDevice(*canvas); + + const SkBitmap& bitmap = device->accessBitmap(false); + if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config)) + return false; + + UMA_HISTOGRAM_TIMES("Renderer4.Snapshot", + base::TimeTicks::Now() - beginning_time); + return true; +} + ExternalHostBindings* ChromeRenderViewObserver::GetExternalHostBindings() { if (!external_host_bindings_.get()) { external_host_bindings_.reset(new ExternalHostBindings( diff --git a/chrome/renderer/chrome_render_view_observer.h b/chrome/renderer/chrome_render_view_observer.h index d5297c8..80247ff 100644 --- a/chrome/renderer/chrome_render_view_observer.h +++ b/chrome/renderer/chrome_render_view_observer.h @@ -126,6 +126,7 @@ class ChromeRenderViewObserver : public content::RenderViewObserver, const string16& jscript, int id, bool notify_result); + void OnCaptureSnapshot(); void OnHandleMessageFromExternalHost(const std::string& message, const std::string& origin, const std::string& target); @@ -149,6 +150,10 @@ class ChromeRenderViewObserver : public content::RenderViewObserver, // maximum amount kMaxIndexChars will be placed into the given buffer. void CaptureText(WebKit::WebFrame* frame, string16* contents); + // 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); + ExternalHostBindings* GetExternalHostBindings(); // Determines if a host is in the strict security host set. |