blob: bf905dd950fec9b4e0a06b8e81ce351b736d2336 [file] [log] [blame]
[email protected]4c03b2e92012-01-03 19:36:571// Copyright (c) 2012 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]0b07be452012-02-29 01:14:4214#include "base/callback_forward.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]6c2381d2011-10-19 02:52:5317#include "content/public/browser/notification_observer.h"
18#include "content/public/browser/notification_registrar.h"
[email protected]d8c660432011-12-22 20:51:2519#include "content/public/browser/web_contents_observer.h"
[email protected]67a46b7f2009-06-16 21:41:0220
[email protected]d54169e92011-01-21 09:19:5221class GURL;
[email protected]754e33822011-01-27 05:57:4522class Profile;
[email protected]67a46b7f2009-06-16 21:41:0223class SkBitmap;
24
[email protected]eaabba22012-03-07 15:02:1125namespace content {
26class RenderWidgetHost;
27}
28
[email protected]0b07be452012-02-29 01:14:4229namespace gfx {
30class Size;
31}
32
[email protected]754e33822011-01-27 05:57:4533namespace history {
34class TopSites;
35}
36
[email protected]6c2381d2011-10-19 02:52:5337class ThumbnailGenerator : public content::NotificationObserver,
[email protected]d8c660432011-12-22 20:51:2538 public content::WebContentsObserver {
[email protected]67a46b7f2009-06-16 21:41:0239 public:
[email protected]bbdd2982011-10-08 18:14:2440 typedef base::Callback<void(const SkBitmap&)> ThumbnailReadyCallback;
[email protected]d54169e92011-01-21 09:19:5241 // The result of clipping. This can be used to determine if the
42 // generated thumbnail is good or not.
43 enum ClipResult {
44 // The source image is smaller.
45 kSourceIsSmaller,
46 // Wider than tall, clip horizontally.
47 kWiderThanTall,
48 // Taller than wide, clip vertically.
49 kTallerThanWide,
50 // The source and destination aspect ratios are identical.
51 kNotClipped,
52 };
53
54 // Bitmasks of options for generating a thumbnail.
55 enum ThumbnailOptions {
56 // No options.
57 kNoOptions = 0,
58 // Request a clipped thumbnail with the aspect ratio preserved.
59 kClippedThumbnail = 1 << 0,
60 };
61
[email protected]58dca552009-06-17 00:35:0262 // This class will do nothing until you call StartThumbnailing.
[email protected]67a46b7f2009-06-16 21:41:0263 ThumbnailGenerator();
[email protected]3690ebe02011-05-25 09:08:1964 virtual ~ThumbnailGenerator();
[email protected]67a46b7f2009-06-16 21:41:0265
[email protected]e82f36672011-04-27 03:34:4066 // Starts taking thumbnails of the given tab contents.
[email protected]fbc5e5f92012-01-02 06:08:3267 void StartThumbnailing(content::WebContents* web_contents);
[email protected]58dca552009-06-17 00:35:0268
[email protected]d65adb12010-04-28 17:26:4969 // This registers a callback that can receive the resulting SkBitmap
70 // from the renderer when it is done rendering it. This differs
[email protected]948f7ab72010-05-28 23:48:0871 // from GetThumbnailForRenderer in that it may be asynchronous, and
[email protected]d65adb12010-04-28 17:26:4972 // because it will also fetch the bitmap even if the tab is hidden.
73 // In addition, if the renderer has to be invoked, the scaling of
[email protected]948f7ab72010-05-28 23:48:0874 // the thumbnail happens on the rendering thread.
75 //
76 // Takes ownership of the callback object.
77 //
78 // If |prefer_backing_store| is set, then the function will try and
79 // use the backing store for the page if it exists. |page_size| is
80 // the size to render the page, and |desired_size| is the size to
81 // scale the resulting rendered page to (which is done efficiently
82 // if done in the rendering thread). If |prefer_backing_store| is
83 // set, and the backing store is used, then the resulting image will
84 // be less then twice the size of the |desired_size| in both
85 // dimensions, but might not be the exact size requested.
[email protected]eaabba22012-03-07 15:02:1186 void AskForSnapshot(content::RenderWidgetHost* renderer,
[email protected]948f7ab72010-05-28 23:48:0887 bool prefer_backing_store,
[email protected]bbdd2982011-10-08 18:14:2488 const ThumbnailReadyCallback& callback,
[email protected]948f7ab72010-05-28 23:48:0889 gfx::Size page_size,
90 gfx::Size desired_size);
[email protected]d65adb12010-04-28 17:26:4991
[email protected]948f7ab72010-05-28 23:48:0892 // This returns a thumbnail of a fixed, small size for the given
93 // renderer.
[email protected]eaabba22012-03-07 15:02:1194 SkBitmap GetThumbnailForRenderer(content::RenderWidgetHost* renderer) const;
[email protected]67a46b7f2009-06-16 21:41:0295
[email protected]d54169e92011-01-21 09:19:5296 // This returns a thumbnail of a fixed, small size for the given
97 // renderer. |options| is a bitmask of ThumbnailOptions. If
98 // |clip_result| is non-NULL, the result of clipping will be written.
[email protected]eaabba22012-03-07 15:02:1199 SkBitmap GetThumbnailForRendererWithOptions(
100 content::RenderWidgetHost* renderer,
101 int options,
102 ClipResult* clip_result) const;
[email protected]d54169e92011-01-21 09:19:52103
[email protected]89d8a8f22011-01-12 21:12:10104 // Start or stop monitoring notifications for |renderer| based on the value
105 // of |monitor|.
[email protected]eaabba22012-03-07 15:02:11106 void MonitorRenderer(content::RenderWidgetHost* renderer, bool monitor);
[email protected]89d8a8f22011-01-12 21:12:10107
[email protected]d54169e92011-01-21 09:19:52108 // Calculates how "boring" a thumbnail is. The boring score is the
109 // 0,1 ranged percentage of pixels that are the most common
110 // luma. Higher boring scores indicate that a higher percentage of a
111 // bitmap are all the same brightness.
[email protected]29096e02012-03-12 22:57:35112 static double CalculateBoringScore(const SkBitmap& bitmap);
[email protected]d54169e92011-01-21 09:19:52113
114 // Gets the clipped bitmap from |bitmap| per the aspect ratio of the
115 // desired width and the desired height. For instance, if the input
116 // bitmap is vertically long (ex. 400x900) and the desired size is
117 // square (ex. 100x100), the clipped bitmap will be the top half of the
118 // input bitmap (400x400).
119 static SkBitmap GetClippedBitmap(const SkBitmap& bitmap,
120 int desired_width,
121 int desired_height,
122 ClipResult* clip_result);
123
[email protected]1718509f2011-04-12 07:04:34124 // Update the thumbnail of the given tab contents if necessary.
[email protected]768c5472011-12-26 19:06:17125 void UpdateThumbnailIfNecessary(content::WebContents* webb_contents);
[email protected]d54169e92011-01-21 09:19:52126
[email protected]a354c81f2012-02-23 18:24:59127 // Update the thumbnail of the given tab.
[email protected]29096e02012-03-12 22:57:35128 void UpdateThumbnail(content::WebContents* web_contents,
129 const SkBitmap& bitmap,
130 const ThumbnailGenerator::ClipResult& clip_result);
[email protected]a354c81f2012-02-23 18:24:59131
[email protected]754e33822011-01-27 05:57:45132 // Returns true if we should update the thumbnail of the given URL.
133 static bool ShouldUpdateThumbnail(Profile* profile,
134 history::TopSites* top_sites,
135 const GURL& url);
136
[email protected]d8c660432011-12-22 20:51:25137 // content::WebContentsObserver overrides.
[email protected]49fd7e22011-11-21 16:52:21138 virtual void DidStartLoading() OVERRIDE;
139 virtual void StopNavigation() OVERRIDE;
[email protected]eb0b24e2011-05-31 08:36:16140
[email protected]67a46b7f2009-06-16 21:41:02141 private:
[email protected]d65adb12010-04-28 17:26:49142 virtual void WidgetDidReceivePaintAtSizeAck(
[email protected]eaabba22012-03-07 15:02:11143 content::RenderWidgetHost* widget,
[email protected]c88c9442010-07-19 18:55:09144 int tag,
[email protected]d65adb12010-04-28 17:26:49145 const gfx::Size& size);
146
[email protected]6c2381d2011-10-19 02:52:53147 // content::NotificationObserver interface.
[email protected]432115822011-07-10 15:52:27148 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53149 const content::NotificationSource& source,
[email protected]49fd7e22011-11-21 16:52:21150 const content::NotificationDetails& details) OVERRIDE;
[email protected]67a46b7f2009-06-16 21:41:02151
152 // Indicates that the given widget has changed is visibility.
[email protected]eaabba22012-03-07 15:02:11153 void WidgetHidden(content::RenderWidgetHost* widget);
[email protected]67a46b7f2009-06-16 21:41:02154
[email protected]fbc5e5f92012-01-02 06:08:32155 // Called when the given web contents are disconnected (either
[email protected]d65adb12010-04-28 17:26:49156 // through being closed, or because the renderer is no longer there).
[email protected]fbc5e5f92012-01-02 06:08:32157 void WebContentsDisconnected(content::WebContents* contents);
[email protected]d65adb12010-04-28 17:26:49158
[email protected]6c2381d2011-10-19 02:52:53159 content::NotificationRegistrar registrar_;
[email protected]67a46b7f2009-06-16 21:41:02160
[email protected]c88c9442010-07-19 18:55:09161 // Map of callback objects by sequence number.
[email protected]d65adb12010-04-28 17:26:49162 struct AsyncRequestInfo;
[email protected]c88c9442010-07-19 18:55:09163 typedef std::map<int,
[email protected]d65adb12010-04-28 17:26:49164 linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap;
165 ThumbnailCallbackMap callback_map_;
166
[email protected]eb0b24e2011-05-31 08:36:16167 bool load_interrupted_;
[email protected]e82f36672011-04-27 03:34:40168
[email protected]67a46b7f2009-06-16 21:41:02169 DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator);
170};
171
172#endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_