blob: 8329f29e0e3e3aba287f22d9b940ddbbb453c94d [file] [log] [blame]
[email protected]ebbbb9f2011-03-09 13:16:141// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]67a46b7f2009-06-16 21:41:022// 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]8b43b062011-05-10 03:49:4314#include "base/callback_old.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/linked_ptr.h"
[email protected]67a46b7f2009-06-16 21:41:0216#include "base/timer.h"
[email protected]5de634712011-03-02 00:20:1917#include "content/browser/renderer_host/backing_store.h"
[email protected]eb0b24e2011-05-31 08:36:1618#include "content/browser/tab_contents/tab_contents_observer.h"
[email protected]ebbbb9f2011-03-09 13:16:1419#include "content/common/notification_observer.h"
20#include "content/common/notification_registrar.h"
[email protected]67a46b7f2009-06-16 21:41:0221
[email protected]d54169e92011-01-21 09:19:5222class GURL;
[email protected]754e33822011-01-27 05:57:4523class Profile;
[email protected]67a46b7f2009-06-16 21:41:0224class RenderWidgetHost;
25class SkBitmap;
[email protected]d65adb12010-04-28 17:26:4926class TabContents;
[email protected]67a46b7f2009-06-16 21:41:0227
[email protected]754e33822011-01-27 05:57:4528namespace history {
29class TopSites;
30}
31
[email protected]eb0b24e2011-05-31 08:36:1632class ThumbnailGenerator : public NotificationObserver,
33 public TabContentsObserver {
[email protected]67a46b7f2009-06-16 21:41:0234 public:
[email protected]bbdd2982011-10-08 18:14:2435 typedef base::Callback<void(const SkBitmap&)> ThumbnailReadyCallback;
[email protected]d54169e92011-01-21 09:19:5236 // The result of clipping. This can be used to determine if the
37 // generated thumbnail is good or not.
38 enum ClipResult {
39 // The source image is smaller.
40 kSourceIsSmaller,
41 // Wider than tall, clip horizontally.
42 kWiderThanTall,
43 // Taller than wide, clip vertically.
44 kTallerThanWide,
45 // The source and destination aspect ratios are identical.
46 kNotClipped,
47 };
48
49 // Bitmasks of options for generating a thumbnail.
50 enum ThumbnailOptions {
51 // No options.
52 kNoOptions = 0,
53 // Request a clipped thumbnail with the aspect ratio preserved.
54 kClippedThumbnail = 1 << 0,
55 };
56
[email protected]58dca552009-06-17 00:35:0257 // This class will do nothing until you call StartThumbnailing.
[email protected]67a46b7f2009-06-16 21:41:0258 ThumbnailGenerator();
[email protected]3690ebe02011-05-25 09:08:1959 virtual ~ThumbnailGenerator();
[email protected]67a46b7f2009-06-16 21:41:0260
[email protected]e82f36672011-04-27 03:34:4061 // Starts taking thumbnails of the given tab contents.
62 void StartThumbnailing(TabContents* tab_contents);
[email protected]58dca552009-06-17 00:35:0263
[email protected]d65adb12010-04-28 17:26:4964 // This registers a callback that can receive the resulting SkBitmap
65 // from the renderer when it is done rendering it. This differs
[email protected]948f7ab72010-05-28 23:48:0866 // from GetThumbnailForRenderer in that it may be asynchronous, and
[email protected]d65adb12010-04-28 17:26:4967 // because it will also fetch the bitmap even if the tab is hidden.
68 // In addition, if the renderer has to be invoked, the scaling of
[email protected]948f7ab72010-05-28 23:48:0869 // the thumbnail happens on the rendering thread.
70 //
71 // Takes ownership of the callback object.
72 //
73 // If |prefer_backing_store| is set, then the function will try and
74 // use the backing store for the page if it exists. |page_size| is
75 // the size to render the page, and |desired_size| is the size to
76 // scale the resulting rendered page to (which is done efficiently
77 // if done in the rendering thread). If |prefer_backing_store| is
78 // set, and the backing store is used, then the resulting image will
79 // be less then twice the size of the |desired_size| in both
80 // dimensions, but might not be the exact size requested.
81 void AskForSnapshot(RenderWidgetHost* renderer,
82 bool prefer_backing_store,
[email protected]bbdd2982011-10-08 18:14:2483 const ThumbnailReadyCallback& callback,
[email protected]948f7ab72010-05-28 23:48:0884 gfx::Size page_size,
85 gfx::Size desired_size);
[email protected]d65adb12010-04-28 17:26:4986
[email protected]948f7ab72010-05-28 23:48:0887 // This returns a thumbnail of a fixed, small size for the given
88 // renderer.
[email protected]67a46b7f2009-06-16 21:41:0289 SkBitmap GetThumbnailForRenderer(RenderWidgetHost* renderer) const;
90
[email protected]d54169e92011-01-21 09:19:5291 // This returns a thumbnail of a fixed, small size for the given
92 // renderer. |options| is a bitmask of ThumbnailOptions. If
93 // |clip_result| is non-NULL, the result of clipping will be written.
94 SkBitmap GetThumbnailForRendererWithOptions(RenderWidgetHost* renderer,
95 int options,
96 ClipResult* clip_result) const;
97
[email protected]89d8a8f22011-01-12 21:12:1098 // Start or stop monitoring notifications for |renderer| based on the value
99 // of |monitor|.
100 void MonitorRenderer(RenderWidgetHost* renderer, bool monitor);
101
[email protected]d54169e92011-01-21 09:19:52102 // Calculates how "boring" a thumbnail is. The boring score is the
103 // 0,1 ranged percentage of pixels that are the most common
104 // luma. Higher boring scores indicate that a higher percentage of a
105 // bitmap are all the same brightness.
106 static double CalculateBoringScore(SkBitmap* bitmap);
107
108 // Gets the clipped bitmap from |bitmap| per the aspect ratio of the
109 // desired width and the desired height. For instance, if the input
110 // bitmap is vertically long (ex. 400x900) and the desired size is
111 // square (ex. 100x100), the clipped bitmap will be the top half of the
112 // input bitmap (400x400).
113 static SkBitmap GetClippedBitmap(const SkBitmap& bitmap,
114 int desired_width,
115 int desired_height,
116 ClipResult* clip_result);
117
[email protected]1718509f2011-04-12 07:04:34118 // Update the thumbnail of the given tab contents if necessary.
119 void UpdateThumbnailIfNecessary(TabContents* tab_contents);
[email protected]d54169e92011-01-21 09:19:52120
[email protected]754e33822011-01-27 05:57:45121 // Returns true if we should update the thumbnail of the given URL.
122 static bool ShouldUpdateThumbnail(Profile* profile,
123 history::TopSites* top_sites,
124 const GURL& url);
125
[email protected]eb0b24e2011-05-31 08:36:16126 // TabContentsObserver overrides.
127 virtual void DidStartLoading();
128 virtual void StopNavigation();
129
[email protected]67a46b7f2009-06-16 21:41:02130 private:
[email protected]d65adb12010-04-28 17:26:49131 virtual void WidgetDidReceivePaintAtSizeAck(
132 RenderWidgetHost* widget,
[email protected]c88c9442010-07-19 18:55:09133 int tag,
[email protected]d65adb12010-04-28 17:26:49134 const gfx::Size& size);
135
[email protected]67a46b7f2009-06-16 21:41:02136 // NotificationObserver interface.
[email protected]432115822011-07-10 15:52:27137 virtual void Observe(int type,
[email protected]67a46b7f2009-06-16 21:41:02138 const NotificationSource& source,
139 const NotificationDetails& details);
140
141 // Indicates that the given widget has changed is visibility.
[email protected]67a46b7f2009-06-16 21:41:02142 void WidgetHidden(RenderWidgetHost* widget);
143
[email protected]d65adb12010-04-28 17:26:49144 // Called when the given tab contents are disconnected (either
145 // through being closed, or because the renderer is no longer there).
146 void TabContentsDisconnected(TabContents* contents);
147
[email protected]67a46b7f2009-06-16 21:41:02148 NotificationRegistrar registrar_;
149
[email protected]c88c9442010-07-19 18:55:09150 // Map of callback objects by sequence number.
[email protected]d65adb12010-04-28 17:26:49151 struct AsyncRequestInfo;
[email protected]c88c9442010-07-19 18:55:09152 typedef std::map<int,
[email protected]d65adb12010-04-28 17:26:49153 linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap;
154 ThumbnailCallbackMap callback_map_;
155
[email protected]eb0b24e2011-05-31 08:36:16156 bool load_interrupted_;
[email protected]e82f36672011-04-27 03:34:40157
[email protected]67a46b7f2009-06-16 21:41:02158 DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator);
159};
160
161#endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_