blob: a4d51c875b48697bcf1400e3c90a58e59580de61 [file] [log] [blame]
[email protected]67a46b7f2009-06-16 21:41:021// 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]67a46b7f2009-06-16 21:41:028
[email protected]d65adb12010-04-28 17:26:499#include <map>
10#include <utility>
[email protected]038d52e12009-10-14 16:53:4111#include <vector>
12
[email protected]67a46b7f2009-06-16 21:41:0213#include "base/basictypes.h"
[email protected]d65adb12010-04-28 17:26:4914#include "base/callback.h"
15#include "base/linked_ptr.h"
16#include "base/lock.h"
[email protected]67a46b7f2009-06-16 21:41:0217#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]d65adb12010-04-28 17:26:4922class RenderViewHost;
[email protected]67a46b7f2009-06-16 21:41:0223class RenderWidgetHost;
24class SkBitmap;
[email protected]d65adb12010-04-28 17:26:4925class TabContents;
[email protected]67a46b7f2009-06-16 21:41:0226
27// This class MUST be destroyed after the RenderWidgetHosts, since it installs
28// a painting observer that is not removed.
29class ThumbnailGenerator : public RenderWidgetHostPaintingObserver,
30 public NotificationObserver {
31 public:
[email protected]d65adb12010-04-28 17:26:4932 typedef Callback1<const SkBitmap&>::Type ThumbnailReadyCallback;
[email protected]58dca552009-06-17 00:35:0233 // This class will do nothing until you call StartThumbnailing.
[email protected]67a46b7f2009-06-16 21:41:0234 ThumbnailGenerator();
35 ~ThumbnailGenerator();
36
[email protected]58dca552009-06-17 00:35:0237 // 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]d65adb12010-04-28 17:26:4941 // This registers a callback that can receive the resulting SkBitmap
42 // from the renderer when it is done rendering it. This differs
[email protected]948f7ab72010-05-28 23:48:0843 // from GetThumbnailForRenderer in that it may be asynchronous, and
[email protected]d65adb12010-04-28 17:26:4944 // 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]948f7ab72010-05-28 23:48:0846 // 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]d65adb12010-04-28 17:26:4963
[email protected]948f7ab72010-05-28 23:48:0864 // This returns a thumbnail of a fixed, small size for the given
65 // renderer.
[email protected]67a46b7f2009-06-16 21:41:0266 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]d65adb12010-04-28 17:26:4982 virtual void WidgetDidReceivePaintAtSizeAck(
83 RenderWidgetHost* widget,
[email protected]c88c9442010-07-19 18:55:0984 int tag,
[email protected]d65adb12010-04-28 17:26:4985 const gfx::Size& size);
86
[email protected]67a46b7f2009-06-16 21:41:0287 // 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]d65adb12010-04-28 17:26:4999 // 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]67a46b7f2009-06-16 21:41:02103 // 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]c88c9442010-07-19 18:55:09122 // Map of callback objects by sequence number.
[email protected]d65adb12010-04-28 17:26:49123 struct AsyncRequestInfo;
[email protected]c88c9442010-07-19 18:55:09124 typedef std::map<int,
[email protected]d65adb12010-04-28 17:26:49125 linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap;
126 ThumbnailCallbackMap callback_map_;
127
[email protected]67a46b7f2009-06-16 21:41:02128 DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator);
129};
130
131#endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_