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. 4 5 #ifndef CHROME_BROWSER_THUMBNAILS_RENDER_WIDGET_SNAPSHOT_TAKER_H_ 6 #define CHROME_BROWSER_THUMBNAILS_RENDER_WIDGET_SNAPSHOT_TAKER_H_ 7 8 #include <map> 9 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" 16 17 class SkBitmap; 18 19 namespace content { 20 class RenderWidgetHost; 21 } 22 23 namespace gfx { 24 class Size; 25 } 26 27 class RenderWidgetSnapshotTaker : public content::NotificationObserver { 28 public: 29 typedef base::Callback<void(const SkBitmap&)> SnapshotReadyCallback; 30 31 RenderWidgetSnapshotTaker(); 32 virtual ~RenderWidgetSnapshotTaker(); 33 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. 39 // 40 // Takes ownership of the callback object. 41 // 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, 49 gfx::Size page_size, 50 gfx::Size desired_size); 51 52 // Cancel any pending snapshots requested via AskForSnapshot. 53 void CancelSnapshot(content::RenderWidgetHost* renderer); 54 55 private: 56 FRIEND_TEST_ALL_PREFIXES(RenderWidgetSnapshotTakerTest, 57 WidgetDidReceivePaintAtSizeAck); 58 FRIEND_TEST_ALL_PREFIXES(RenderWidgetSnapshotTakerTest, 59 WidgetDidReceivePaintAtSizeAckFail); 60 61 // content::NotificationObserver overrides. 62 virtual void Observe(int type, 63 const content::NotificationSource& source, 64 const content::NotificationDetails& details) OVERRIDE; 65 66 // Start or stop monitoring notifications for |renderer| based on the value 67 // of |monitor|. 68 void MonitorRenderer(content::RenderWidgetHost* renderer, bool monitor); 69 70 void WidgetDidReceivePaintAtSizeAck(content::RenderWidgetHost* widget, 71 int tag, 72 const gfx::Size& size); 73 74 content::NotificationRegistrar registrar_; 75 76 // Map of callback objects by sequence number. 77 struct AsyncRequestInfo; 78 typedef std::map<int, 79 linked_ptr<AsyncRequestInfo> > SnapshotCallbackMap; 80 SnapshotCallbackMap callback_map_; 81 82 // Count of how many outstanding snapshot requests there are for each 83 // RenderWidgetHost. 84 std::map<content::RenderWidgetHost*, int> host_monitor_counts_; 85 86 DISALLOW_COPY_AND_ASSIGN(RenderWidgetSnapshotTaker); 87 }; 88 89 #endif // CHROME_BROWSER_THUMBNAILS_RENDER_WIDGET_SNAPSHOT_TAKER_H_ 90