Skip to content

Commit

Permalink
Implemented GetWindowSnapshot on RenderViewImpl
Browse files Browse the repository at this point in the history
This necessitated the relocation of the previous chrome::GrabWindowSnapshot code
to ui/snapshot, which has been turned into it's own component to avoid circular dependencies with aura.
A new variant of GrabWindowSnapshot, GrabViewSnapshot, has been added as well to facilitate easier usage by views. chrome::GrabWindowSnapshotForUser was left in place to accomodate existing calls
to the API, but now calls up to the ui/snapshot code.

This is a subset of the prior CL 11362023, which has been broken apart to facilitate easier reviews

BUG=157479

Review URL: https://chromiumcodereview.appspot.com/11399002

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@173329 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
bajones@chromium.org committed Dec 15, 2012
1 parent d72acb0 commit c9bc8f1
Show file tree
Hide file tree
Showing 23 changed files with 330 additions and 63 deletions.
7 changes: 4 additions & 3 deletions chrome/browser/ui/window_snapshot/window_snapshot.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "chrome/browser/browser_process.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/common/pref_names.h"
#include "ui/snapshot/snapshot.h"

namespace chrome {

Expand All @@ -16,12 +17,12 @@ bool GrabWindowSnapshotForUser(
const gfx::Rect& snapshot_bounds) {
if (g_browser_process->local_state()->GetBoolean(prefs::kDisableScreenshots))
return false;
return internal::GrabWindowSnapshot(window, png_representation,
snapshot_bounds);
return ui::GrabWindowSnapshot(window, png_representation,
snapshot_bounds);
}

void RegisterScreenshotPrefs(PrefService* service) {
service->RegisterBooleanPref(prefs::kDisableScreenshots, false);
}

} // namespace browser
} // namespace chrome
14 changes: 1 addition & 13 deletions chrome/browser/ui/window_snapshot/window_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,13 @@ void RegisterScreenshotPrefs(PrefService* service);
// the primary monitor. This takes into account calling user context (ie. checks
// policy settings if taking screenshots is allowed), and is intended to be used
// by browser code. If you need to take a screenshot for debugging purposes,
// consider using GrabWindowSnapshot.
// consider using ui::GrabWindowSnapshot.
// Returns true if the operation is successful (ie. permitted).
bool GrabWindowSnapshotForUser(
gfx::NativeWindow window,
std::vector<unsigned char>* png_representation,
const gfx::Rect& snapshot_bounds);

namespace internal {

// Like GrabWindowSnapshotForUser, but does not perform additional security
// checks - just grabs a snapshot. This is intended to be used for debugging
// purposes where no BrowserProcess instance is available (ie. tests).
// DO NOT use in a result of user action.
bool GrabWindowSnapshot(
gfx::NativeWindow window,
std::vector<unsigned char>* png_representation,
const gfx::Rect& snapshot_bounds);

} // namespace internal
} // namespace chrome

#endif // CHROME_BROWSER_UI_WINDOW_SNAPSHOT_WINDOW_SNAPSHOT_H_
6 changes: 1 addition & 5 deletions chrome/chrome_browser_ui.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'../third_party/zlib/zlib.gyp:zlib',
'../ui/base/strings/ui_strings.gyp:ui_strings',
'../ui/native_theme/native_theme.gyp:native_theme',
'../ui/snapshot/snapshot.gyp:snapshot',
'../ui/ui.gyp:ui',
'../ui/ui.gyp:ui_resources',
'../webkit/support/webkit_support.gyp:user_agent',
Expand Down Expand Up @@ -2104,10 +2105,6 @@
'browser/ui/window_sizer/window_sizer_win.cc',
'browser/ui/window_snapshot/window_snapshot.cc',
'browser/ui/window_snapshot/window_snapshot.h',
'browser/ui/window_snapshot/window_snapshot_aura.cc',
'browser/ui/window_snapshot/window_snapshot_gtk.cc',
'browser/ui/window_snapshot/window_snapshot_mac.mm',
'browser/ui/window_snapshot/window_snapshot_win.cc',
'browser/ui/zoom/zoom_controller.cc',
'browser/ui/zoom/zoom_controller.h',
'browser/ui/zoom/zoom_observer.h',
Expand Down Expand Up @@ -2351,7 +2348,6 @@
['exclude', '^browser/ui/views/user_data_dir_dialog_view.cc'],
['exclude', '^browser/ui/webui/help/version_updater_win.cc'],
['exclude', '^browser/ui/window_sizer/window_sizer_win.cc'],
['exclude', '^browser/ui/window_snapshot/window_snapshot_win.cc'],
# TODO: (stevenjb/beng): Find a home for these.
['include', '^browser/ui/views/simple_message_box_views.cc'],
['include', '^browser/ui/webui/certificate_viewer_webui.cc'],
Expand Down
1 change: 0 additions & 1 deletion chrome/chrome_tests_unit.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -1407,7 +1407,6 @@
'browser/ui/window_sizer/window_sizer_ash_unittest.cc',
'browser/ui/window_sizer/window_sizer_common_unittest.cc',
'browser/ui/window_sizer/window_sizer_unittest.cc',
'browser/ui/window_snapshot/window_snapshot_mac_unittest.mm',
'browser/ui/zoom/zoom_controller_unittest.cc',
'browser/chrome_to_mobile_service_unittest.cc',
'browser/user_style_sheet_watcher_unittest.cc',
Expand Down
4 changes: 2 additions & 2 deletions chrome/test/base/ui_test_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/omnibox/location_bar.h"
#include "chrome/browser/ui/omnibox/omnibox_view.h"
#include "chrome/browser/ui/window_snapshot/window_snapshot.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/pref_names.h"
Expand Down Expand Up @@ -73,6 +72,7 @@
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkColor.h"
#include "ui/gfx/size.h"
#include "ui/snapshot/snapshot.h"
#include "ui/ui_controls/ui_controls.h"

#if defined(USE_AURA)
Expand Down Expand Up @@ -634,7 +634,7 @@ bool SaveScreenSnapshotToDirectory(const FilePath& directory,
std::vector<unsigned char> png_data;
gfx::Rect bounds(
gfx::Size(rect.right - rect.left, rect.bottom - rect.top));
if (chrome::internal::GrabWindowSnapshot(NULL, &png_data, bounds) &&
if (ui::GrabWindowSnapshot(NULL, &png_data, bounds) &&
png_data.size() <= INT_MAX) {
int bytes = static_cast<int>(png_data.size());
int written = file_util::WriteFile(
Expand Down
1 change: 1 addition & 0 deletions content/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ include_rules = [
"+ui/gfx",
"+ui/gl",
"+ui/native_theme",
"+ui/snapshot",
"+ui/surface",
# Content knows about grd files, but the specifics of how to get a resource
# given its id is left to the embedder.
Expand Down
25 changes: 25 additions & 0 deletions content/browser/renderer_host/render_view_host_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
#include "ui/base/dialogs/selected_file_info.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/snapshot/snapshot.h"
#include "webkit/fileapi/isolated_context.h"
#include "webkit/glue/webdropdata.h"
#include "webkit/glue/webkit_glue.h"
Expand Down Expand Up @@ -1020,6 +1021,7 @@ bool RenderViewHostImpl::OnMessageReceived(const IPC::Message& msg) {
IPC_MESSAGE_HANDLER(ViewHostMsg_ScriptEvalResponse, OnScriptEvalResponse)
IPC_MESSAGE_HANDLER(ViewHostMsg_DidZoomURL, OnDidZoomURL)
IPC_MESSAGE_HANDLER(ViewHostMsg_MediaNotification, OnMediaNotification)
IPC_MESSAGE_HANDLER(ViewHostMsg_GetWindowSnapshot, OnGetWindowSnapshot)
#if defined(OS_ANDROID)
IPC_MESSAGE_HANDLER(ViewHostMsg_StartContentIntent, OnStartContentIntent)
IPC_MESSAGE_HANDLER(ViewHostMsg_DidChangeBodyBackgroundColor,
Expand Down Expand Up @@ -2058,4 +2060,27 @@ void RenderViewHostImpl::ClearPowerSaveBlockers() {
STLDeleteValues(&power_save_blockers_);
}

void RenderViewHostImpl::OnGetWindowSnapshot(const int snapshot_id) {
std::vector<unsigned char> png;

// This feature is behind the kEnableGpuBenchmarking command line switch
// because it poses security concerns and should only be used for testing.
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kEnableGpuBenchmarking)) {
gfx::Rect view_bounds = GetView()->GetViewBounds();
gfx::Rect snapshot_bounds(view_bounds.size());
gfx::Size snapshot_size = snapshot_bounds.size();

if (ui::GrabViewSnapshot(GetView()->GetNativeView(),
&png, snapshot_bounds)) {
Send(new ViewMsg_WindowSnapshotCompleted(
GetRoutingID(), snapshot_id, snapshot_size, png));
return;
}
}

Send(new ViewMsg_WindowSnapshotCompleted(
GetRoutingID(), snapshot_id, gfx::Size(), png));
}

} // namespace content
1 change: 1 addition & 0 deletions content/browser/renderer_host/render_view_host_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ class CONTENT_EXPORT RenderViewHostImpl
void OnDomOperationResponse(const std::string& json_string,
int automation_id);
void OnFrameTreeUpdated(const std::string& frame_tree);
void OnGetWindowSnapshot(const int snapshot_id);

#if defined(OS_MACOSX) || defined(OS_ANDROID)
void OnMsgShowPopup(const ViewHostMsg_ShowPopup_Params& params);
Expand Down
10 changes: 10 additions & 0 deletions content/common/view_messages.h
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,12 @@ IPC_MESSAGE_ROUTED1(ViewMsg_SelectPopupMenuItem,
IPC_MESSAGE_ROUTED1(ViewMsg_ReleaseDisambiguationPopupDIB,
TransportDIB::Handle /* DIB handle */)

// Notifies the renderer that a snapshot has been retrieved.
IPC_MESSAGE_ROUTED3(ViewMsg_WindowSnapshotCompleted,
int /* snapshot_id */,
gfx::Size /* size */,
std::vector<unsigned char> /* png */)

// -----------------------------------------------------------------------------
// Messages sent from the renderer to the browser.

Expand Down Expand Up @@ -1946,6 +1952,10 @@ IPC_MESSAGE_ROUTED3(ViewHostMsg_WebUISend,
std::string /* message */,
base::ListValue /* args */)

// Requests a snapshot of the given window.
IPC_MESSAGE_ROUTED1(ViewHostMsg_GetWindowSnapshot,
int /* snapshot_id */)

// A renderer sends this to the browser process when it wants to create a ppapi
// plugin. The browser will create the plugin process if necessary, and will
// return a handle to the channel on success.
Expand Down
1 change: 1 addition & 0 deletions content/content_browser.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'../net/net.gyp:net',
'../skia/skia.gyp:skia',
'../third_party/zlib/zlib.gyp:zlib',
'../ui/snapshot/snapshot.gyp:snapshot',
'../ui/ui.gyp:ui',
'../ui/ui.gyp:ui_resources',
],
Expand Down
17 changes: 17 additions & 0 deletions content/renderer/render_view_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,7 @@ RenderViewImpl::RenderViewImpl(RenderViewImplParams* params)
#endif
session_storage_namespace_id_(params->session_storage_namespace_id),
handling_select_range_(false),
next_snapshot_id_(0),
#if defined(OS_WIN)
focused_plugin_id_(-1),
#endif
Expand Down Expand Up @@ -1043,6 +1044,8 @@ bool RenderViewImpl::OnMessageReceived(const IPC::Message& message) {
#endif
IPC_MESSAGE_HANDLER(ViewMsg_ReleaseDisambiguationPopupDIB,
OnReleaseDisambiguationPopupDIB)
IPC_MESSAGE_HANDLER(ViewMsg_WindowSnapshotCompleted,
OnWindowSnapshotCompleted)

// Have the super handle all other messages.
IPC_MESSAGE_UNHANDLED(handled = RenderWidget::OnMessageReceived(message))
Expand Down Expand Up @@ -1766,6 +1769,20 @@ bool RenderViewImpl::SendAndRunNestedMessageLoop(IPC::SyncMessage* message) {
return Send(message);
}

void RenderViewImpl::GetWindowSnapshot(const WindowSnapshotCallback& callback) {
int id = next_snapshot_id_++;
pending_snapshots_.insert(std::make_pair(id, callback));
Send(new ViewHostMsg_GetWindowSnapshot(routing_id_, id));
}

void RenderViewImpl::OnWindowSnapshotCompleted(const int snapshot_id,
const gfx::Size& size, const std::vector<unsigned char>& png) {
PendingSnapshotMap::iterator it = pending_snapshots_.find(snapshot_id);
DCHECK(it != pending_snapshots_.end());
it->second.Run(size, png);
pending_snapshots_.erase(it);
}

// WebKit::WebViewClient ------------------------------------------------------

WebView* RenderViewImpl::createView(
Expand Down
16 changes: 16 additions & 0 deletions content/renderer/render_view_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ class CONTENT_EXPORT RenderViewImpl
// supported PPAPI plug-ins.
bool HasIMETextFocus();

// Callback for use with GetWindowSnapshot.
typedef base::Callback<void(
const gfx::Size&, const std::vector<unsigned char>&)>
WindowSnapshotCallback;

void GetWindowSnapshot(const WindowSnapshotCallback& callback);

// IPC::Listener implementation ----------------------------------------------

virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
Expand Down Expand Up @@ -1032,6 +1039,9 @@ class CONTENT_EXPORT RenderViewImpl
const gfx::Rect& view_frame);
#endif

void OnWindowSnapshotCompleted(const int snapshot_id,
const gfx::Size& size, const std::vector<unsigned char>& png);


// Adding a new message handler? Please add it in alphabetical order above
// and put it in the same position in the .cc file.
Expand Down Expand Up @@ -1500,6 +1510,12 @@ class CONTENT_EXPORT RenderViewImpl
// Wraps the |webwidget_| as a MouseLockDispatcher::LockTarget interface.
scoped_ptr<MouseLockDispatcher::LockTarget> webwidget_mouse_lock_target_;

// State associated with the GetWindowSnapshot function.
int next_snapshot_id_;
typedef std::map<int, WindowSnapshotCallback>
PendingSnapshotMap;
PendingSnapshotMap pending_snapshots_;

// Plugins -------------------------------------------------------------------

// All the currently active plugin delegates for this RenderView; kept so
Expand Down
7 changes: 7 additions & 0 deletions ui/snapshot/DEPS
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
include_rules = [
"+skia",
"+ui/aura",
"+ui/base",
"+ui/compositor",
"+ui/gfx",
]
65 changes: 65 additions & 0 deletions ui/snapshot/snapshot.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# 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.

{
'variables': {
'chromium_code': 1,
},
'targets': [
{
'target_name': 'snapshot',
'type': '<(component)',
'dependencies': [
'../../skia/skia.gyp:skia',
'../../base/base.gyp:base',
'../ui.gyp:ui',
],
'defines': [
'SNAPSHOT_IMPLEMENTATION',
],
'sources': [
'snapshot.h',
'snapshot_android.cc',
'snapshot_aura.cc',
'snapshot_export.h',
'snapshot_gtk.cc',
'snapshot_ios.mm',
'snapshot_mac.mm',
'snapshot_win.cc',
],
'include_dirs': [
'..',
],
'conditions': [
['use_aura==1', {
'dependencies': [
'../aura/aura.gyp:aura',
'../compositor/compositor.gyp:compositor',
],
}],
['use_aura==1 and OS=="win"', {
'sources/': [
['exclude', 'snapshot_win.cc'],
],
}],
],
},
{
'target_name': 'snapshot_unittests',
'type': '<(gtest_target_type)',
'dependencies': [
'../../skia/skia.gyp:skia',
'../../base/base.gyp:base',
'../../testing/gtest.gyp:gtest',
'../../testing/gmock.gyp:gmock',
'../../testing/gtest.gyp:gtest',
'../ui.gyp:ui',
'snapshot'
],
'sources': [
'snapshot_mac_unittest.mm',
]
},
],
}
34 changes: 34 additions & 0 deletions ui/snapshot/snapshot.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 UI_SNAPSHOT_SNAPSHOT_H_
#define UI_SNAPSHOT_SNAPSHOT_H_

#include <vector>

#include "ui/gfx/native_widget_types.h"
#include "ui/snapshot/snapshot_export.h"

namespace gfx {
class Rect;
}

namespace ui {

// Grabs a snapshot of the window/view. No security checks are done.
// This is intended to be used for debugging purposes where no BrowserProcess
// instance is available (ie. tests). DO NOT use in a result of user action.
SNAPSHOT_EXPORT bool GrabWindowSnapshot(
gfx::NativeWindow window,
std::vector<unsigned char>* png_representation,
const gfx::Rect& snapshot_bounds);

SNAPSHOT_EXPORT bool GrabViewSnapshot(
gfx::NativeView view,
std::vector<unsigned char>* png_representation,
const gfx::Rect& snapshot_bounds);

} // namespace ui

#endif // UI_SNAPSHOT_SNAPSHOT_H_
Loading

0 comments on commit c9bc8f1

Please sign in to comment.