blob: c44a95279a63ca69a5ec87526ce2cc482c92db68 [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]ebbbb9f2011-03-09 13:16:1418#include "content/common/notification_observer.h"
19#include "content/common/notification_registrar.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 RenderWidgetHost;
24class SkBitmap;
[email protected]d65adb12010-04-28 17:26:4925class TabContents;
[email protected]67a46b7f2009-06-16 21:41:0226
[email protected]754e33822011-01-27 05:57:4527namespace history {
28class TopSites;
29}
30
[email protected]89d8a8f22011-01-12 21:12:1031class ThumbnailGenerator : NotificationObserver {
[email protected]67a46b7f2009-06-16 21:41:0232 public:
[email protected]d65adb12010-04-28 17:26:4933 typedef Callback1<const SkBitmap&>::Type ThumbnailReadyCallback;
[email protected]d54169e92011-01-21 09:19:5234 // The result of clipping. This can be used to determine if the
35 // generated thumbnail is good or not.
36 enum ClipResult {
37 // The source image is smaller.
38 kSourceIsSmaller,
39 // Wider than tall, clip horizontally.
40 kWiderThanTall,
41 // Taller than wide, clip vertically.
42 kTallerThanWide,
43 // The source and destination aspect ratios are identical.
44 kNotClipped,
45 };
46
47 // Bitmasks of options for generating a thumbnail.
48 enum ThumbnailOptions {
49 // No options.
50 kNoOptions = 0,
51 // Request a clipped thumbnail with the aspect ratio preserved.
52 kClippedThumbnail = 1 << 0,
53 };
54
[email protected]58dca552009-06-17 00:35:0255 // This class will do nothing until you call StartThumbnailing.
[email protected]67a46b7f2009-06-16 21:41:0256 ThumbnailGenerator();
[email protected]3690ebe02011-05-25 09:08:1957 virtual ~ThumbnailGenerator();
[email protected]67a46b7f2009-06-16 21:41:0258
[email protected]e82f36672011-04-27 03:34:4059 // Starts taking thumbnails of the given tab contents.
60 void StartThumbnailing(TabContents* tab_contents);
[email protected]58dca552009-06-17 00:35:0261
[email protected]d65adb12010-04-28 17:26:4962 // This registers a callback that can receive the resulting SkBitmap
63 // from the renderer when it is done rendering it. This differs
[email protected]948f7ab72010-05-28 23:48:0864 // from GetThumbnailForRenderer in that it may be asynchronous, and
[email protected]d65adb12010-04-28 17:26:4965 // because it will also fetch the bitmap even if the tab is hidden.
66 // In addition, if the renderer has to be invoked, the scaling of
[email protected]948f7ab72010-05-28 23:48:0867 // the thumbnail happens on the rendering thread.
68 //
69 // Takes ownership of the callback object.
70 //
71 // If |prefer_backing_store| is set, then the function will try and
72 // use the backing store for the page if it exists. |page_size| is
73 // the size to render the page, and |desired_size| is the size to
74 // scale the resulting rendered page to (which is done efficiently
75 // if done in the rendering thread). If |prefer_backing_store| is
76 // set, and the backing store is used, then the resulting image will
77 // be less then twice the size of the |desired_size| in both
78 // dimensions, but might not be the exact size requested.
79 void AskForSnapshot(RenderWidgetHost* renderer,
80 bool prefer_backing_store,
81 ThumbnailReadyCallback* callback,
82 gfx::Size page_size,
83 gfx::Size desired_size);
[email protected]d65adb12010-04-28 17:26:4984
[email protected]948f7ab72010-05-28 23:48:0885 // This returns a thumbnail of a fixed, small size for the given
86 // renderer.
[email protected]67a46b7f2009-06-16 21:41:0287 SkBitmap GetThumbnailForRenderer(RenderWidgetHost* renderer) const;
88
[email protected]d54169e92011-01-21 09:19:5289 // This returns a thumbnail of a fixed, small size for the given
90 // renderer. |options| is a bitmask of ThumbnailOptions. If
91 // |clip_result| is non-NULL, the result of clipping will be written.
92 SkBitmap GetThumbnailForRendererWithOptions(RenderWidgetHost* renderer,
93 int options,
94 ClipResult* clip_result) const;
95
[email protected]89d8a8f22011-01-12 21:12:1096 // Start or stop monitoring notifications for |renderer| based on the value
97 // of |monitor|.
98 void MonitorRenderer(RenderWidgetHost* renderer, bool monitor);
99
[email protected]d54169e92011-01-21 09:19:52100 // Calculates how "boring" a thumbnail is. The boring score is the
101 // 0,1 ranged percentage of pixels that are the most common
102 // luma. Higher boring scores indicate that a higher percentage of a
103 // bitmap are all the same brightness.
104 static double CalculateBoringScore(SkBitmap* bitmap);
105
106 // Gets the clipped bitmap from |bitmap| per the aspect ratio of the
107 // desired width and the desired height. For instance, if the input
108 // bitmap is vertically long (ex. 400x900) and the desired size is
109 // square (ex. 100x100), the clipped bitmap will be the top half of the
110 // input bitmap (400x400).
111 static SkBitmap GetClippedBitmap(const SkBitmap& bitmap,
112 int desired_width,
113 int desired_height,
114 ClipResult* clip_result);
115
[email protected]1718509f2011-04-12 07:04:34116 // Update the thumbnail of the given tab contents if necessary.
117 void UpdateThumbnailIfNecessary(TabContents* tab_contents);
[email protected]d54169e92011-01-21 09:19:52118
[email protected]754e33822011-01-27 05:57:45119 // Returns true if we should update the thumbnail of the given URL.
120 static bool ShouldUpdateThumbnail(Profile* profile,
121 history::TopSites* top_sites,
122 const GURL& url);
123
[email protected]67a46b7f2009-06-16 21:41:02124 private:
[email protected]d65adb12010-04-28 17:26:49125 virtual void WidgetDidReceivePaintAtSizeAck(
126 RenderWidgetHost* widget,
[email protected]c88c9442010-07-19 18:55:09127 int tag,
[email protected]d65adb12010-04-28 17:26:49128 const gfx::Size& size);
129
[email protected]67a46b7f2009-06-16 21:41:02130 // NotificationObserver interface.
131 virtual void Observe(NotificationType type,
132 const NotificationSource& source,
133 const NotificationDetails& details);
134
135 // Indicates that the given widget has changed is visibility.
[email protected]67a46b7f2009-06-16 21:41:02136 void WidgetHidden(RenderWidgetHost* widget);
137
[email protected]d65adb12010-04-28 17:26:49138 // Called when the given tab contents are disconnected (either
139 // through being closed, or because the renderer is no longer there).
140 void TabContentsDisconnected(TabContents* contents);
141
[email protected]67a46b7f2009-06-16 21:41:02142 NotificationRegistrar registrar_;
143
[email protected]c88c9442010-07-19 18:55:09144 // Map of callback objects by sequence number.
[email protected]d65adb12010-04-28 17:26:49145 struct AsyncRequestInfo;
[email protected]c88c9442010-07-19 18:55:09146 typedef std::map<int,
[email protected]d65adb12010-04-28 17:26:49147 linked_ptr<AsyncRequestInfo> > ThumbnailCallbackMap;
148 ThumbnailCallbackMap callback_map_;
149
[email protected]e82f36672011-04-27 03:34:40150 TabContents* tab_contents_;
151
[email protected]67a46b7f2009-06-16 21:41:02152 DISALLOW_COPY_AND_ASSIGN(ThumbnailGenerator);
153};
154
155#endif // CHROME_BROWSER_TAB_CONTENTS_THUMBNAIL_GENERATOR_H_