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