[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 1 | // Copyright (c) 2009 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_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ |
| 6 | #define CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame^] | 7 | #pragma once |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 8 | |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <utility> |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 13 | #include "base/basictypes.h" |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 14 | #include "base/callback.h" |
| 15 | #include "base/linked_ptr.h" |
| 16 | #include "base/lock.h" |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 17 | #include "base/timer.h" |
| 18 | #include "chrome/browser/renderer_host/render_widget_host_painting_observer.h" |
| 19 | #include "chrome/common/notification_observer.h" |
| 20 | #include "chrome/common/notification_registrar.h" |
| 21 | |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 22 | class RenderViewHost; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 23 | class RenderWidgetHost; |
| 24 | class SkBitmap; |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 25 | class TabContents; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 26 | |
| 27 | // This class MUST be destroyed after the RenderWidgetHosts, since it installs |
| 28 | // a painting observer that is not removed. |
| 29 | class ThumbnailGenerator : public RenderWidgetHostPaintingObserver, |
| 30 | public NotificationObserver { |
| 31 | public: |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 32 | typedef Callback1<const SkBitmap&>::Type ThumbnailReadyCallback; |
[email protected] | 58dca55 | 2009-06-17 00:35:02 | [diff] [blame] | 33 | // This class will do nothing until you call StartThumbnailing. |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 34 | ThumbnailGenerator(); |
| 35 | ~ThumbnailGenerator(); |
| 36 | |
[email protected] | 58dca55 | 2009-06-17 00:35:02 | [diff] [blame] | 37 | // Ensures that we're properly hooked in to generated thumbnails. This can |
| 38 | // be called repeatedly and with wild abandon to no ill effect. |
| 39 | void StartThumbnailing(); |
| 40 | |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 41 | // This registers a callback that can receive the resulting SkBitmap |
| 42 | // from the renderer when it is done rendering it. This differs |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 43 | // from GetThumbnailForRenderer in that it may be asynchronous, and |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 44 | // because it will also fetch the bitmap even if the tab is hidden. |
| 45 | // In addition, if the renderer has to be invoked, the scaling of |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 46 | // the thumbnail happens on the rendering thread. |
| 47 | // |
| 48 | // Takes ownership of the callback object. |
| 49 | // |
| 50 | // If |prefer_backing_store| is set, then the function will try and |
| 51 | // use the backing store for the page if it exists. |page_size| is |
| 52 | // the size to render the page, and |desired_size| is the size to |
| 53 | // scale the resulting rendered page to (which is done efficiently |
| 54 | // if done in the rendering thread). If |prefer_backing_store| is |
| 55 | // set, and the backing store is used, then the resulting image will |
| 56 | // be less then twice the size of the |desired_size| in both |
| 57 | // dimensions, but might not be the exact size requested. |
| 58 | void AskForSnapshot(RenderWidgetHost* renderer, |
| 59 | bool prefer_backing_store, |
| 60 | ThumbnailReadyCallback* callback, |
| 61 | gfx::Size page_size, |
| 62 | gfx::Size desired_size); |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 63 | |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 64 | // This returns a thumbnail of a fixed, small size for the given |
| 65 | // renderer. |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 66 | SkBitmap GetThumbnailForRenderer(RenderWidgetHost* renderer) const; |
| 67 | |
| 68 | #ifdef UNIT_TEST |
| 69 | // When true, the class will not use a timeout to do the expiration. This |
| 70 | // will cause expiration to happen on the next run of the message loop. |
| 71 | // Unit tests case use this to test expiration by choosing when the message |
| 72 | // loop runs. |
| 73 | void set_no_timeout(bool no_timeout) { no_timeout_ = no_timeout; } |
| 74 | #endif |
| 75 | |
| 76 | private: |
| 77 | // RenderWidgetHostPaintingObserver implementation. |
| 78 | virtual void WidgetWillDestroyBackingStore(RenderWidgetHost* widget, |
| 79 | BackingStore* backing_store); |
| 80 | virtual void WidgetDidUpdateBackingStore(RenderWidgetHost* widget); |
| 81 | |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 82 | virtual void WidgetDidReceivePaintAtSizeAck( |
| 83 | RenderWidgetHost* widget, |
[email protected] | c88c944 | 2010-07-19 18:55:09 | [diff] [blame] | 84 | int tag, |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 85 | const gfx::Size& size); |
| 86 | |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 87 | // NotificationObserver interface. |
| 88 | virtual void Observe(NotificationType type, |
| 89 | const NotificationSource& source, |
| 90 | const NotificationDetails& details); |
| 91 | |
| 92 | // Indicates that the given widget has changed is visibility. |
| 93 | void WidgetShown(RenderWidgetHost* widget); |
| 94 | void WidgetHidden(RenderWidgetHost* widget); |
| 95 | |
| 96 | // Called when the given widget is destroyed. |
| 97 | void WidgetDestroyed(RenderWidgetHost* widget); |
| 98 | |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 99 | // Called when the given tab contents are disconnected (either |
| 100 | // through being closed, or because the renderer is no longer there). |
| 101 | void TabContentsDisconnected(TabContents* contents); |
| 102 | |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 103 | // Timer function called on a delay after a tab has been shown. It will |
| 104 | // invalidate the thumbnail for hosts with expired thumbnails in shown_hosts_. |
| 105 | void ShownDelayHandler(); |
| 106 | |
| 107 | // Removes the given host from the shown_hosts_ list, if it is there. |
| 108 | void EraseHostFromShownList(RenderWidgetHost* host); |
| 109 | |
| 110 | NotificationRegistrar registrar_; |
| 111 | |
| 112 | base::OneShotTimer<ThumbnailGenerator> timer_; |
| 113 | |
| 114 | // A list of all RWHs that have been shown and need to have their thumbnail |
| 115 | // expired at some time in the future with the "slop" time has elapsed. This |
| 116 | // list will normally have 0 or 1 items in it. |
| 117 | std::vector<RenderWidgetHost*> shown_hosts_; |
| 118 | |
| 119 | // See the setter above. |
| 120 | bool no_timeout_; |
| 121 | |
[email protected] | c88c944 | 2010-07-19 18:55:09 | [diff] [blame] | 122 | // Map of callback objects by sequence number. |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 123 | struct AsyncRequestInfo; |
[email protected] | c88c944 | 2010-07-19 18:55:09 | [diff] [blame] | 124 | typedef std::map<int, |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 125 | linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap; |
| 126 | ThumbnailCallbackMap callback_map_; |
| 127 | |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 128 | DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator); |
| 129 | }; |
| 130 | |
| 131 | #endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ |