| OLD | NEW |
| 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; | |
| 16 class Profile; | |
| 17 class SkBitmap; | |
| 18 | |
| 19 namespace content { | 15 namespace content { |
| 20 class RenderViewHost; | 16 class RenderViewHost; |
| 21 class RenderWidgetHost; | 17 class RenderWidgetHost; |
| 22 } | 18 } |
| 23 | 19 |
| 24 namespace skia { | |
| 25 class PlatformBitmap; | |
| 26 } | |
| 27 | |
| 28 class ThumbnailTabHelper | 20 class ThumbnailTabHelper |
| 29 : public content::NotificationObserver, | 21 : public content::NotificationObserver, |
| 30 public content::WebContentsObserver, | 22 public content::WebContentsObserver, |
| 31 public content::WebContentsUserData<ThumbnailTabHelper> { | 23 public content::WebContentsUserData<ThumbnailTabHelper> { |
| 32 public: | 24 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(); | 25 virtual ~ThumbnailTabHelper(); |
| 66 | 26 |
| 67 // Enables or disables the function of taking thumbnails. | 27 // Enables or disables the function of taking thumbnails. |
| 68 // A disabled ThumbnailTabHelper generates no thumbnails although it still | 28 // A disabled ThumbnailTabHelper generates no thumbnails although it still |
| 69 // continues to receive the notifications from the web contents. | 29 // continues to receive the notifications from the web contents. |
| 70 void set_enabled(bool enabled) { enabled_ = enabled; } | 30 void set_enabled(bool enabled) { enabled_ = enabled; } |
| 71 | 31 |
| 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: | 32 private: |
| 91 explicit ThumbnailTabHelper(content::WebContents* contents); | 33 explicit ThumbnailTabHelper(content::WebContents* contents); |
| 92 friend class content::WebContentsUserData<ThumbnailTabHelper>; | 34 friend class content::WebContentsUserData<ThumbnailTabHelper>; |
| 93 | 35 |
| 94 // content::NotificationObserver overrides. | 36 // content::NotificationObserver overrides. |
| 95 virtual void Observe(int type, | 37 virtual void Observe(int type, |
| 96 const content::NotificationSource& source, | 38 const content::NotificationSource& source, |
| 97 const content::NotificationDetails& details) OVERRIDE; | 39 const content::NotificationDetails& details) OVERRIDE; |
| 98 | 40 |
| 99 // content::WebContentsObserver overrides. | 41 // content::WebContentsObserver overrides. |
| 100 virtual void DidStartLoading( | 42 virtual void DidStartLoading( |
| 101 content::RenderViewHost* render_view_host) OVERRIDE; | 43 content::RenderViewHost* render_view_host) OVERRIDE; |
| 102 virtual void StopNavigation() OVERRIDE; | 44 virtual void StopNavigation() OVERRIDE; |
| 103 | 45 |
| 104 // Update the thumbnail of the given tab contents if necessary. | 46 // Update the thumbnail of the given tab contents if necessary. |
| 105 void UpdateThumbnailIfNecessary(content::WebContents* web_contents); | 47 void UpdateThumbnailIfNecessary(content::WebContents* web_contents); |
| 106 | 48 |
| 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. | 49 // Called when a render view host was created for a WebContents. |
| 128 void RenderViewHostCreated(content::RenderViewHost* renderer); | 50 void RenderViewHostCreated(content::RenderViewHost* renderer); |
| 129 | 51 |
| 130 // Indicates that the given widget has changed is visibility. | 52 // Indicates that the given widget has changed is visibility. |
| 131 void WidgetHidden(content::RenderWidgetHost* widget); | 53 void WidgetHidden(content::RenderWidgetHost* widget); |
| 132 | 54 |
| 133 // Called when the given render view host was deleted. | 55 // Called when the given render view host was deleted. |
| 134 void RenderViewHostDeleted(content::RenderViewHost* renderer); | 56 void RenderViewHostDeleted(content::RenderViewHost* renderer); |
| 135 | 57 |
| 136 bool enabled_; | 58 bool enabled_; |
| 137 | 59 |
| 138 content::NotificationRegistrar registrar_; | 60 content::NotificationRegistrar registrar_; |
| 139 | 61 |
| 140 bool load_interrupted_; | 62 bool load_interrupted_; |
| 141 | 63 |
| 142 DISALLOW_COPY_AND_ASSIGN(ThumbnailTabHelper); | 64 DISALLOW_COPY_AND_ASSIGN(ThumbnailTabHelper); |
| 143 }; | 65 }; |
| 144 | 66 |
| 145 #endif // CHROME_BROWSER_THUMBNAILS_THUMBNAIL_TAB_HELPER_H_ | 67 #endif // CHROME_BROWSER_THUMBNAILS_THUMBNAIL_TAB_HELPER_H_ |
| OLD | NEW |