[email protected] | 4c03b2e9 | 2012-01-03 19:36:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 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] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 8 | |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <utility> |
[email protected] | 038d52e1 | 2009-10-14 16:53:41 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 13 | #include "base/basictypes.h" |
[email protected] | 0b07be45 | 2012-02-29 01:14:42 | [diff] [blame] | 14 | #include "base/callback_forward.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 15 | #include "base/memory/linked_ptr.h" |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 16 | #include "base/timer.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 17 | #include "content/public/browser/notification_observer.h" |
| 18 | #include "content/public/browser/notification_registrar.h" |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 19 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 20 | |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 21 | class GURL; |
[email protected] | 754e3382 | 2011-01-27 05:57:45 | [diff] [blame] | 22 | class Profile; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 23 | class SkBitmap; |
| 24 | |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 25 | namespace content { |
| 26 | class RenderWidgetHost; |
| 27 | } |
| 28 | |
[email protected] | 0b07be45 | 2012-02-29 01:14:42 | [diff] [blame] | 29 | namespace gfx { |
| 30 | class Size; |
| 31 | } |
| 32 | |
[email protected] | 754e3382 | 2011-01-27 05:57:45 | [diff] [blame] | 33 | namespace history { |
| 34 | class TopSites; |
| 35 | } |
| 36 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 37 | class ThumbnailGenerator : public content::NotificationObserver, |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 38 | public content::WebContentsObserver { |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 39 | public: |
[email protected] | bbdd298 | 2011-10-08 18:14:24 | [diff] [blame] | 40 | typedef base::Callback<void(const SkBitmap&)> ThumbnailReadyCallback; |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 41 | // 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] | 58dca55 | 2009-06-17 00:35:02 | [diff] [blame] | 62 | // This class will do nothing until you call StartThumbnailing. |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 63 | ThumbnailGenerator(); |
[email protected] | 3690ebe0 | 2011-05-25 09:08:19 | [diff] [blame] | 64 | virtual ~ThumbnailGenerator(); |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 65 | |
[email protected] | e82f3667 | 2011-04-27 03:34:40 | [diff] [blame] | 66 | // Starts taking thumbnails of the given tab contents. |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame] | 67 | void StartThumbnailing(content::WebContents* web_contents); |
[email protected] | 58dca55 | 2009-06-17 00:35:02 | [diff] [blame] | 68 | |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 69 | // This registers a callback that can receive the resulting SkBitmap |
| 70 | // from the renderer when it is done rendering it. This differs |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 71 | // from GetThumbnailForRenderer in that it may be asynchronous, and |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 72 | // 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] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 74 | // 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] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 86 | void AskForSnapshot(content::RenderWidgetHost* renderer, |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 87 | bool prefer_backing_store, |
[email protected] | bbdd298 | 2011-10-08 18:14:24 | [diff] [blame] | 88 | const ThumbnailReadyCallback& callback, |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 89 | gfx::Size page_size, |
| 90 | gfx::Size desired_size); |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 91 | |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 92 | // This returns a thumbnail of a fixed, small size for the given |
| 93 | // renderer. |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 94 | SkBitmap GetThumbnailForRenderer(content::RenderWidgetHost* renderer) const; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 95 | |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 96 | // 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] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 99 | SkBitmap GetThumbnailForRendererWithOptions( |
| 100 | content::RenderWidgetHost* renderer, |
| 101 | int options, |
| 102 | ClipResult* clip_result) const; |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 103 | |
[email protected] | 89d8a8f2 | 2011-01-12 21:12:10 | [diff] [blame] | 104 | // Start or stop monitoring notifications for |renderer| based on the value |
| 105 | // of |monitor|. |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 106 | void MonitorRenderer(content::RenderWidgetHost* renderer, bool monitor); |
[email protected] | 89d8a8f2 | 2011-01-12 21:12:10 | [diff] [blame] | 107 | |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 108 | // 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] | 29096e0 | 2012-03-12 22:57:35 | [diff] [blame] | 112 | static double CalculateBoringScore(const SkBitmap& bitmap); |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 113 | |
| 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] | 1718509f | 2011-04-12 07:04:34 | [diff] [blame] | 124 | // Update the thumbnail of the given tab contents if necessary. |
[email protected] | 768c547 | 2011-12-26 19:06:17 | [diff] [blame] | 125 | void UpdateThumbnailIfNecessary(content::WebContents* webb_contents); |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 126 | |
[email protected] | a354c81f | 2012-02-23 18:24:59 | [diff] [blame] | 127 | // Update the thumbnail of the given tab. |
[email protected] | 29096e0 | 2012-03-12 22:57:35 | [diff] [blame] | 128 | void UpdateThumbnail(content::WebContents* web_contents, |
| 129 | const SkBitmap& bitmap, |
| 130 | const ThumbnailGenerator::ClipResult& clip_result); |
[email protected] | a354c81f | 2012-02-23 18:24:59 | [diff] [blame] | 131 | |
[email protected] | 754e3382 | 2011-01-27 05:57:45 | [diff] [blame] | 132 | // 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] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 137 | // content::WebContentsObserver overrides. |
[email protected] | 49fd7e2 | 2011-11-21 16:52:21 | [diff] [blame] | 138 | virtual void DidStartLoading() OVERRIDE; |
| 139 | virtual void StopNavigation() OVERRIDE; |
[email protected] | eb0b24e | 2011-05-31 08:36:16 | [diff] [blame] | 140 | |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 141 | private: |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 142 | virtual void WidgetDidReceivePaintAtSizeAck( |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 143 | content::RenderWidgetHost* widget, |
[email protected] | c88c944 | 2010-07-19 18:55:09 | [diff] [blame] | 144 | int tag, |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 145 | const gfx::Size& size); |
| 146 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 147 | // content::NotificationObserver interface. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 148 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 149 | const content::NotificationSource& source, |
[email protected] | 49fd7e2 | 2011-11-21 16:52:21 | [diff] [blame] | 150 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 151 | |
| 152 | // Indicates that the given widget has changed is visibility. |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 153 | void WidgetHidden(content::RenderWidgetHost* widget); |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 154 | |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame] | 155 | // Called when the given web contents are disconnected (either |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 156 | // through being closed, or because the renderer is no longer there). |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame] | 157 | void WebContentsDisconnected(content::WebContents* contents); |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 158 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 159 | content::NotificationRegistrar registrar_; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 160 | |
[email protected] | c88c944 | 2010-07-19 18:55:09 | [diff] [blame] | 161 | // Map of callback objects by sequence number. |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 162 | struct AsyncRequestInfo; |
[email protected] | c88c944 | 2010-07-19 18:55:09 | [diff] [blame] | 163 | typedef std::map<int, |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 164 | linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap; |
| 165 | ThumbnailCallbackMap callback_map_; |
| 166 | |
[email protected] | eb0b24e | 2011-05-31 08:36:16 | [diff] [blame] | 167 | bool load_interrupted_; |
[email protected] | e82f3667 | 2011-04-27 03:34:40 | [diff] [blame] | 168 | |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 169 | DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator); |
| 170 | }; |
| 171 | |
| 172 | #endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ |