[email protected] | ebbbb9f | 2011-03-09 13:16:14 | [diff] [blame] | 1 | // Copyright (c) 2011 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] | 8b43b06 | 2011-05-10 03:49:43 | [diff] [blame] | 14 | #include "base/callback_old.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] | 5de63471 | 2011-03-02 00:20:19 | [diff] [blame] | 17 | #include "content/browser/renderer_host/backing_store.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 18 | #include "content/public/browser/notification_observer.h" |
| 19 | #include "content/public/browser/notification_registrar.h" |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 20 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 21 | |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 22 | class GURL; |
[email protected] | 754e3382 | 2011-01-27 05:57:45 | [diff] [blame] | 23 | class Profile; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 24 | class RenderWidgetHost; |
| 25 | class SkBitmap; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 26 | |
[email protected] | 754e3382 | 2011-01-27 05:57:45 | [diff] [blame] | 27 | namespace history { |
| 28 | class TopSites; |
| 29 | } |
| 30 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 31 | class ThumbnailGenerator : public content::NotificationObserver, |
[email protected] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 32 | public content::WebContentsObserver { |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 33 | public: |
[email protected] | bbdd298 | 2011-10-08 18:14:24 | [diff] [blame] | 34 | typedef base::Callback<void(const SkBitmap&)> ThumbnailReadyCallback; |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 35 | // 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] | 58dca55 | 2009-06-17 00:35:02 | [diff] [blame] | 56 | // This class will do nothing until you call StartThumbnailing. |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 57 | ThumbnailGenerator(); |
[email protected] | 3690ebe0 | 2011-05-25 09:08:19 | [diff] [blame] | 58 | virtual ~ThumbnailGenerator(); |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 59 | |
[email protected] | e82f3667 | 2011-04-27 03:34:40 | [diff] [blame] | 60 | // Starts taking thumbnails of the given tab contents. |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame^] | 61 | void StartThumbnailing(content::WebContents* web_contents); |
[email protected] | 58dca55 | 2009-06-17 00:35:02 | [diff] [blame] | 62 | |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 63 | // This registers a callback that can receive the resulting SkBitmap |
| 64 | // from the renderer when it is done rendering it. This differs |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 65 | // from GetThumbnailForRenderer in that it may be asynchronous, and |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 66 | // 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] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 68 | // 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] | bbdd298 | 2011-10-08 18:14:24 | [diff] [blame] | 82 | const ThumbnailReadyCallback& callback, |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 83 | gfx::Size page_size, |
| 84 | gfx::Size desired_size); |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 85 | |
[email protected] | 948f7ab7 | 2010-05-28 23:48:08 | [diff] [blame] | 86 | // This returns a thumbnail of a fixed, small size for the given |
| 87 | // renderer. |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 88 | SkBitmap GetThumbnailForRenderer(RenderWidgetHost* renderer) const; |
| 89 | |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 90 | // 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] | 89d8a8f2 | 2011-01-12 21:12:10 | [diff] [blame] | 97 | // Start or stop monitoring notifications for |renderer| based on the value |
| 98 | // of |monitor|. |
| 99 | void MonitorRenderer(RenderWidgetHost* renderer, bool monitor); |
| 100 | |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 101 | // 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] | 1718509f | 2011-04-12 07:04:34 | [diff] [blame] | 117 | // Update the thumbnail of the given tab contents if necessary. |
[email protected] | 768c547 | 2011-12-26 19:06:17 | [diff] [blame] | 118 | void UpdateThumbnailIfNecessary(content::WebContents* webb_contents); |
[email protected] | d54169e9 | 2011-01-21 09:19:52 | [diff] [blame] | 119 | |
[email protected] | 754e3382 | 2011-01-27 05:57:45 | [diff] [blame] | 120 | // 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] | d8c66043 | 2011-12-22 20:51:25 | [diff] [blame] | 125 | // content::WebContentsObserver overrides. |
[email protected] | 49fd7e2 | 2011-11-21 16:52:21 | [diff] [blame] | 126 | virtual void DidStartLoading() OVERRIDE; |
| 127 | virtual void StopNavigation() OVERRIDE; |
[email protected] | eb0b24e | 2011-05-31 08:36:16 | [diff] [blame] | 128 | |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 129 | private: |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 130 | virtual void WidgetDidReceivePaintAtSizeAck( |
| 131 | RenderWidgetHost* widget, |
[email protected] | c88c944 | 2010-07-19 18:55:09 | [diff] [blame] | 132 | int tag, |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 133 | const gfx::Size& size); |
| 134 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 135 | // content::NotificationObserver interface. |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 136 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 137 | const content::NotificationSource& source, |
[email protected] | 49fd7e2 | 2011-11-21 16:52:21 | [diff] [blame] | 138 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 139 | |
| 140 | // Indicates that the given widget has changed is visibility. |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 141 | void WidgetHidden(RenderWidgetHost* widget); |
| 142 | |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame^] | 143 | // Called when the given web contents are disconnected (either |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 144 | // through being closed, or because the renderer is no longer there). |
[email protected] | fbc5e5f9 | 2012-01-02 06:08:32 | [diff] [blame^] | 145 | void WebContentsDisconnected(content::WebContents* contents); |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 146 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 147 | content::NotificationRegistrar registrar_; |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 148 | |
[email protected] | c88c944 | 2010-07-19 18:55:09 | [diff] [blame] | 149 | // Map of callback objects by sequence number. |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 150 | struct AsyncRequestInfo; |
[email protected] | c88c944 | 2010-07-19 18:55:09 | [diff] [blame] | 151 | typedef std::map<int, |
[email protected] | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 152 | linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap; |
| 153 | ThumbnailCallbackMap callback_map_; |
| 154 | |
[email protected] | eb0b24e | 2011-05-31 08:36:16 | [diff] [blame] | 155 | bool load_interrupted_; |
[email protected] | e82f3667 | 2011-04-27 03:34:40 | [diff] [blame] | 156 | |
[email protected] | 67a46b7f | 2009-06-16 21:41:02 | [diff] [blame] | 157 | DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator); |
| 158 | }; |
| 159 | |
| 160 | #endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_ |