Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(638)

Side by Side Diff: chrome/browser/thumbnails/thumbnail_tab_helper.h

Issue 11985003: Refactored-out the code of thumbnaling algorithm from thumbnail_tab_helper. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added a unit test. Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_THUMBNAILS_THUMBNAIL_TAB_HELPER_H_ 5 #ifndef CHROME_BROWSER_THUMBNAILS_THUMBNAIL_TAB_HELPER_H_
6 #define CHROME_BROWSER_THUMBNAILS_THUMBNAIL_TAB_HELPER_H_ 6 #define CHROME_BROWSER_THUMBNAILS_THUMBNAIL_TAB_HELPER_H_
7 7
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "chrome/common/thumbnail_score.h" 9 #include "chrome/browser/thumbnails/thumbnailing_context.h"
10 #include "content/public/browser/notification_observer.h" 10 #include "content/public/browser/notification_observer.h"
11 #include "content/public/browser/notification_registrar.h" 11 #include "content/public/browser/notification_registrar.h"
12 #include "content/public/browser/web_contents_observer.h" 12 #include "content/public/browser/web_contents_observer.h"
13 #include "content/public/browser/web_contents_user_data.h" 13 #include "content/public/browser/web_contents_user_data.h"
14 14
15 class GURL; 15 class GURL;
mazda 2013/01/17 18:08:43 These three declarations are no longer necessary.
motek. 2013/01/17 18:39:42 Done.
16 class Profile; 16 class Profile;
17 class SkBitmap; 17 class SkBitmap;
18 18
19 namespace content { 19 namespace content {
20 class RenderViewHost; 20 class RenderViewHost;
21 class RenderWidgetHost; 21 class RenderWidgetHost;
22 } 22 }
23 23
24 namespace skia { 24 namespace skia {
25 class PlatformBitmap; 25 class PlatformBitmap;
mazda 2013/01/17 18:08:43 Ditto.
motek. 2013/01/17 18:39:42 Done.
26 } 26 }
27 27
28 class ThumbnailTabHelper 28 class ThumbnailTabHelper
29 : public content::NotificationObserver, 29 : public content::NotificationObserver,
30 public content::WebContentsObserver, 30 public content::WebContentsObserver,
31 public content::WebContentsUserData<ThumbnailTabHelper> { 31 public content::WebContentsUserData<ThumbnailTabHelper> {
32 public: 32 public:
33 // The result of clipping. This can be used to determine if the
34 // generated thumbnail is good or not.
35 enum ClipResult {
36 // Clipping is not done yet.
37 kUnprocessed,
38 // The source image is smaller.
39 kSourceIsSmaller,
40 // Wider than tall by twice or more, clip horizontally.
41 kTooWiderThanTall,
42 // Wider than tall, clip horizontally.
43 kWiderThanTall,
44 // Taller than wide, clip vertically.
45 kTallerThanWide,
46 // The source and destination aspect ratios are identical.
47 kNotClipped,
48 };
49
50 // Holds the information needed for processing a thumbnail.
51 struct ThumbnailingContext : base::RefCountedThreadSafe<ThumbnailingContext> {
52 ThumbnailingContext(content::WebContents* web_contents,
53 bool load_interrupted);
54
55 content::BrowserContext* browser_context;
56 GURL url;
57 ClipResult clip_result;
58 ThumbnailScore score;
59
60 private:
61 ~ThumbnailingContext();
62 friend class base::RefCountedThreadSafe<ThumbnailingContext>;
63 };
64
65 virtual ~ThumbnailTabHelper(); 33 virtual ~ThumbnailTabHelper();
66 34
67 // Enables or disables the function of taking thumbnails. 35 // Enables or disables the function of taking thumbnails.
68 // A disabled ThumbnailTabHelper generates no thumbnails although it still 36 // A disabled ThumbnailTabHelper generates no thumbnails although it still
69 // continues to receive the notifications from the web contents. 37 // continues to receive the notifications from the web contents.
70 void set_enabled(bool enabled) { enabled_ = enabled; } 38 void set_enabled(bool enabled) { enabled_ = enabled; }
71 39
72 // Calculates how "boring" a thumbnail is. The boring score is the
73 // 0,1 ranged percentage of pixels that are the most common
74 // luma. Higher boring scores indicate that a higher percentage of a
75 // bitmap are all the same brightness.
76 // Statically exposed for use by tests only.
77 static double CalculateBoringScore(const SkBitmap& bitmap);
78
79 // Gets the clipped bitmap from |bitmap| per the aspect ratio of the
80 // desired width and the desired height. For instance, if the input
81 // bitmap is vertically long (ex. 400x900) and the desired size is
82 // square (ex. 100x100), the clipped bitmap will be the top half of the
83 // input bitmap (400x400).
84 // Statically exposed for use by tests only.
85 static SkBitmap GetClippedBitmap(const SkBitmap& bitmap,
86 int desired_width,
87 int desired_height,
88 ClipResult* clip_result);
89
90 private: 40 private:
91 explicit ThumbnailTabHelper(content::WebContents* contents); 41 explicit ThumbnailTabHelper(content::WebContents* contents);
92 friend class content::WebContentsUserData<ThumbnailTabHelper>; 42 friend class content::WebContentsUserData<ThumbnailTabHelper>;
93 43
94 // content::NotificationObserver overrides. 44 // content::NotificationObserver overrides.
95 virtual void Observe(int type, 45 virtual void Observe(int type,
96 const content::NotificationSource& source, 46 const content::NotificationSource& source,
97 const content::NotificationDetails& details) OVERRIDE; 47 const content::NotificationDetails& details) OVERRIDE;
98 48
99 // content::WebContentsObserver overrides. 49 // content::WebContentsObserver overrides.
100 virtual void DidStartLoading( 50 virtual void DidStartLoading(
101 content::RenderViewHost* render_view_host) OVERRIDE; 51 content::RenderViewHost* render_view_host) OVERRIDE;
102 virtual void StopNavigation() OVERRIDE; 52 virtual void StopNavigation() OVERRIDE;
103 53
104 // Update the thumbnail of the given tab contents if necessary. 54 // Update the thumbnail of the given tab contents if necessary.
105 void UpdateThumbnailIfNecessary(content::WebContents* web_contents); 55 void UpdateThumbnailIfNecessary(content::WebContents* web_contents);
106 56
107 // Update the thumbnail of the given tab.
108 static void UpdateThumbnail(ThumbnailingContext* context,
109 const SkBitmap& bitmap);
110
111 // Asynchronously updates the thumbnail of the given tab. This must be called
112 // on the UI thread.
113 void AsyncUpdateThumbnail(content::WebContents* web_contents);
114
115 // Called when the bitmap for generating a thumbnail is ready after the
116 // AsyncUpdateThumbnail invocation. This runs on the UI thread.
117 static void UpdateThumbnailWithBitmap(ThumbnailingContext* context,
118 const SkBitmap& bitmap);
119
120 // Called when the canvas for generating a thumbnail is ready after the
121 // AsyncUpdateThumbnail invocation. This runs on the UI thread.
122 static void UpdateThumbnailWithCanvas(
123 ThumbnailingContext* context,
124 skia::PlatformBitmap* temp_bitmap,
125 bool result);
126
127 // Called when a render view host was created for a WebContents. 57 // Called when a render view host was created for a WebContents.
128 void RenderViewHostCreated(content::RenderViewHost* renderer); 58 void RenderViewHostCreated(content::RenderViewHost* renderer);
129 59
130 // Indicates that the given widget has changed is visibility. 60 // Indicates that the given widget has changed is visibility.
131 void WidgetHidden(content::RenderWidgetHost* widget); 61 void WidgetHidden(content::RenderWidgetHost* widget);
132 62
133 // Called when the given render view host was deleted. 63 // Called when the given render view host was deleted.
134 void RenderViewHostDeleted(content::RenderViewHost* renderer); 64 void RenderViewHostDeleted(content::RenderViewHost* renderer);
135 65
136 bool enabled_; 66 bool enabled_;
137 67
138 content::NotificationRegistrar registrar_; 68 content::NotificationRegistrar registrar_;
139 69
140 bool load_interrupted_; 70 bool load_interrupted_;
141 71
142 DISALLOW_COPY_AND_ASSIGN(ThumbnailTabHelper); 72 DISALLOW_COPY_AND_ASSIGN(ThumbnailTabHelper);
143 }; 73 };
144 74
145 #endif // CHROME_BROWSER_THUMBNAILS_THUMBNAIL_TAB_HELPER_H_ 75 #endif // CHROME_BROWSER_THUMBNAILS_THUMBNAIL_TAB_HELPER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698