blob: c805a9b0b3247f17f1fe714227b2d149d76f0240 [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]6c2381d2011-10-19 02:52:5318#include "content/public/browser/notification_observer.h"
19#include "content/public/browser/notification_registrar.h"
[email protected]d8c660432011-12-22 20:51:2520#include "content/public/browser/web_contents_observer.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]67a46b7f2009-06-16 21:41:0226
[email protected]754e33822011-01-27 05:57:4527namespace history {
28class TopSites;
29}
30
[email protected]6c2381d2011-10-19 02:52:5331class ThumbnailGenerator : public content::NotificationObserver,
[email protected]d8c660432011-12-22 20:51:2532 public content::WebContentsObserver {
[email protected]67a46b7f2009-06-16 21:41:0233 public:
[email protected]bbdd2982011-10-08 18:14:2434 typedef base::Callback<void(const SkBitmap&)> ThumbnailReadyCallback;
[email protected]d54169e92011-01-21 09:19:5235 // The result of clipping. This can be used to determine if the
36 // generated thumbnail is good or not.
37 enum ClipResult {
38 // The source image is smaller.
39 kSourceIsSmaller,
40 // Wider than tall, clip horizontally.
41 kWiderThanTall,
42 // Taller than wide, clip vertically.
43 kTallerThanWide,
44 // The source and destination aspect ratios are identical.
45 kNotClipped,
46 };
47
48 // Bitmasks of options for generating a thumbnail.
49 enum ThumbnailOptions {
50 // No options.
51 kNoOptions = 0,
52 // Request a clipped thumbnail with the aspect ratio preserved.
53 kClippedThumbnail = 1 << 0,
54 };
55
[email protected]58dca552009-06-17 00:35:0256 // This class will do nothing until you call StartThumbnailing.
[email protected]67a46b7f2009-06-16 21:41:0257 ThumbnailGenerator();
[email protected]3690ebe02011-05-25 09:08:1958 virtual ~ThumbnailGenerator();
[email protected]67a46b7f2009-06-16 21:41:0259
[email protected]e82f36672011-04-27 03:34:4060 // Starts taking thumbnails of the given tab contents.
[email protected]fbc5e5f92012-01-02 06:08:3261 void StartThumbnailing(content::WebContents* web_contents);
[email protected]58dca552009-06-17 00:35:0262
[email protected]d65adb12010-04-28 17:26:4963 // This registers a callback that can receive the resulting SkBitmap
64 // from the renderer when it is done rendering it. This differs
[email protected]948f7ab72010-05-28 23:48:0865 // from GetThumbnailForRenderer in that it may be asynchronous, and
[email protected]d65adb12010-04-28 17:26:4966 // because it will also fetch the bitmap even if the tab is hidden.
67 // In addition, if the renderer has to be invoked, the scaling of
[email protected]948f7ab72010-05-28 23:48:0868 // the thumbnail happens on the rendering thread.
69 //
70 // Takes ownership of the callback object.
71 //
72 // If |prefer_backing_store| is set, then the function will try and
73 // use the backing store for the page if it exists. |page_size| is
74 // the size to render the page, and |desired_size| is the size to
75 // scale the resulting rendered page to (which is done efficiently
76 // if done in the rendering thread). If |prefer_backing_store| is
77 // set, and the backing store is used, then the resulting image will
78 // be less then twice the size of the |desired_size| in both
79 // dimensions, but might not be the exact size requested.
80 void AskForSnapshot(RenderWidgetHost* renderer,
81 bool prefer_backing_store,
[email protected]bbdd2982011-10-08 18:14:2482 const ThumbnailReadyCallback& callback,
[email protected]948f7ab72010-05-28 23:48:0883 gfx::Size page_size,
84 gfx::Size desired_size);
[email protected]d65adb12010-04-28 17:26:4985
[email protected]948f7ab72010-05-28 23:48:0886 // This returns a thumbnail of a fixed, small size for the given
87 // renderer.
[email protected]67a46b7f2009-06-16 21:41:0288 SkBitmap GetThumbnailForRenderer(RenderWidgetHost* renderer) const;
89
[email protected]d54169e92011-01-21 09:19:5290 // This returns a thumbnail of a fixed, small size for the given
91 // renderer. |options| is a bitmask of ThumbnailOptions. If
92 // |clip_result| is non-NULL, the result of clipping will be written.
93 SkBitmap GetThumbnailForRendererWithOptions(RenderWidgetHost* renderer,
94 int options,
95 ClipResult* clip_result) const;
96
[email protected]89d8a8f22011-01-12 21:12:1097 // Start or stop monitoring notifications for |renderer| based on the value
98 // of |monitor|.
99 void MonitorRenderer(RenderWidgetHost* renderer, bool monitor);
100
[email protected]d54169e92011-01-21 09:19:52101 // Calculates how "boring" a thumbnail is. The boring score is the
102 // 0,1 ranged percentage of pixels that are the most common
103 // luma. Higher boring scores indicate that a higher percentage of a
104 // bitmap are all the same brightness.
105 static double CalculateBoringScore(SkBitmap* bitmap);
106
107 // Gets the clipped bitmap from |bitmap| per the aspect ratio of the
108 // desired width and the desired height. For instance, if the input
109 // bitmap is vertically long (ex. 400x900) and the desired size is
110 // square (ex. 100x100), the clipped bitmap will be the top half of the
111 // input bitmap (400x400).
112 static SkBitmap GetClippedBitmap(const SkBitmap& bitmap,
113 int desired_width,
114 int desired_height,
115 ClipResult* clip_result);
116
[email protected]1718509f2011-04-12 07:04:34117 // Update the thumbnail of the given tab contents if necessary.
[email protected]768c5472011-12-26 19:06:17118 void UpdateThumbnailIfNecessary(content::WebContents* webb_contents);
[email protected]d54169e92011-01-21 09:19:52119
[email protected]754e33822011-01-27 05:57:45120 // Returns true if we should update the thumbnail of the given URL.
121 static bool ShouldUpdateThumbnail(Profile* profile,
122 history::TopSites* top_sites,
123 const GURL& url);
124
[email protected]d8c660432011-12-22 20:51:25125 // content::WebContentsObserver overrides.
[email protected]49fd7e22011-11-21 16:52:21126 virtual void DidStartLoading() OVERRIDE;
127 virtual void StopNavigation() OVERRIDE;
[email protected]eb0b24e2011-05-31 08:36:16128
[email protected]67a46b7f2009-06-16 21:41:02129 private:
[email protected]d65adb12010-04-28 17:26:49130 virtual void WidgetDidReceivePaintAtSizeAck(
131 RenderWidgetHost* widget,
[email protected]c88c9442010-07-19 18:55:09132 int tag,
[email protected]d65adb12010-04-28 17:26:49133 const gfx::Size& size);
134
[email protected]6c2381d2011-10-19 02:52:53135 // content::NotificationObserver interface.
[email protected]432115822011-07-10 15:52:27136 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53137 const content::NotificationSource& source,
[email protected]49fd7e22011-11-21 16:52:21138 const content::NotificationDetails& details) OVERRIDE;
[email protected]67a46b7f2009-06-16 21:41:02139
140 // Indicates that the given widget has changed is visibility.
[email protected]67a46b7f2009-06-16 21:41:02141 void WidgetHidden(RenderWidgetHost* widget);
142
[email protected]fbc5e5f92012-01-02 06:08:32143 // Called when the given web contents are disconnected (either
[email protected]d65adb12010-04-28 17:26:49144 // through being closed, or because the renderer is no longer there).
[email protected]fbc5e5f92012-01-02 06:08:32145 void WebContentsDisconnected(content::WebContents* contents);
[email protected]d65adb12010-04-28 17:26:49146
[email protected]6c2381d2011-10-19 02:52:53147 content::NotificationRegistrar registrar_;
[email protected]67a46b7f2009-06-16 21:41:02148
[email protected]c88c9442010-07-19 18:55:09149 // Map of callback objects by sequence number.
[email protected]d65adb12010-04-28 17:26:49150 struct AsyncRequestInfo;
[email protected]c88c9442010-07-19 18:55:09151 typedef std::map<int,
[email protected]d65adb12010-04-28 17:26:49152 linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap;
153 ThumbnailCallbackMap callback_map_;
154
[email protected]eb0b24e2011-05-31 08:36:16155 bool load_interrupted_;
[email protected]e82f36672011-04-27 03:34:40156
[email protected]67a46b7f2009-06-16 21:41:02157 DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator);
158};
159
160#endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_