1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_THUMBNAILS_RENDER_WIDGET_SNAPSHOT_TAKER_H_
6 #define CHROME_BROWSER_THUMBNAILS_RENDER_WIDGET_SNAPSHOT_TAKER_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/linked_ptr.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
20 class RenderWidgetHost;
27 class RenderWidgetSnapshotTaker : public content::NotificationObserver {
29 typedef base::Callback<void(const SkBitmap&)> SnapshotReadyCallback;
31 RenderWidgetSnapshotTaker();
32 virtual ~RenderWidgetSnapshotTaker();
34 // This registers a callback that can receive the resulting SkBitmap
35 // from the renderer when it is done rendering it. This is asynchronous,
36 // and it will also fetch the bitmap even if the tab is hidden.
37 // In addition, if the renderer has to be invoked, the scaling of
38 // the thumbnail happens on the rendering thread.
40 // Takes ownership of the callback object.
42 // |page_size| is the size to render the page, and |desired_size| is
43 // the size to scale the resulting rendered page to (which is done
44 // efficiently if done in the rendering thread). The resulting image
45 // will be less then twice the size of the |desired_size| in both
46 // dimensions, but might not be the exact size requested.
47 void AskForSnapshot(content::RenderWidgetHost* renderer,
48 const SnapshotReadyCallback& callback,
50 gfx::Size desired_size);
52 // Cancel any pending snapshots requested via AskForSnapshot.
53 void CancelSnapshot(content::RenderWidgetHost* renderer);
56 FRIEND_TEST_ALL_PREFIXES(RenderWidgetSnapshotTakerTest,
57 WidgetDidReceivePaintAtSizeAck);
58 FRIEND_TEST_ALL_PREFIXES(RenderWidgetSnapshotTakerTest,
59 WidgetDidReceivePaintAtSizeAckFail);
61 // content::NotificationObserver overrides.
62 virtual void Observe(int type,
63 const content::NotificationSource& source,
64 const content::NotificationDetails& details) OVERRIDE;
66 // Start or stop monitoring notifications for |renderer| based on the value
68 void MonitorRenderer(content::RenderWidgetHost* renderer, bool monitor);
70 void WidgetDidReceivePaintAtSizeAck(content::RenderWidgetHost* widget,
72 const gfx::Size& size);
74 content::NotificationRegistrar registrar_;
76 // Map of callback objects by sequence number.
77 struct AsyncRequestInfo;
79 linked_ptr<AsyncRequestInfo> > SnapshotCallbackMap;
80 SnapshotCallbackMap callback_map_;
82 // Count of how many outstanding snapshot requests there are for each
84 std::map<content::RenderWidgetHost*, int> host_monitor_counts_;
86 DISALLOW_COPY_AND_ASSIGN(RenderWidgetSnapshotTaker);
89 #endif // CHROME_BROWSER_THUMBNAILS_RENDER_WIDGET_SNAPSHOT_TAKER_H_