| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/renderer/chrome_render_observer.h" | 5 #include "chrome/renderer/chrome_render_view_observer.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "chrome/common/chrome_switches.h" | 10 #include "chrome/common/chrome_switches.h" |
| 11 #include "chrome/common/render_messages.h" | 11 #include "chrome/common/render_messages.h" |
| 12 #include "chrome/common/thumbnail_score.h" | 12 #include "chrome/common/thumbnail_score.h" |
| 13 #include "chrome/renderer/about_handler.h" |
| 13 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" | 14 #include "chrome/renderer/safe_browsing/phishing_classifier_delegate.h" |
| 14 #include "chrome/renderer/translate_helper.h" | 15 #include "chrome/renderer/translate_helper.h" |
| 16 #include "content/common/view_messages.h" |
| 15 #include "content/renderer/content_renderer_client.h" | 17 #include "content/renderer/content_renderer_client.h" |
| 16 #include "content/renderer/render_view.h" | 18 #include "content/renderer/render_view.h" |
| 17 #include "skia/ext/bitmap_platform_device.h" | 19 #include "skia/ext/bitmap_platform_device.h" |
| 18 #include "skia/ext/image_operations.h" | 20 #include "skia/ext/image_operations.h" |
| 19 #include "skia/ext/platform_canvas.h" | 21 #include "skia/ext/platform_canvas.h" |
| 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" | 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" |
| 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 22 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" | 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebRect.h" |
| 23 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSize.h" |
| 24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 // bitmap are all the same brightness. | 78 // bitmap are all the same brightness. |
| 77 static double CalculateBoringScore(SkBitmap* bitmap) { | 79 static double CalculateBoringScore(SkBitmap* bitmap) { |
| 78 int histogram[256] = {0}; | 80 int histogram[256] = {0}; |
| 79 color_utils::BuildLumaHistogram(bitmap, histogram); | 81 color_utils::BuildLumaHistogram(bitmap, histogram); |
| 80 | 82 |
| 81 int color_count = *std::max_element(histogram, histogram + 256); | 83 int color_count = *std::max_element(histogram, histogram + 256); |
| 82 int pixel_count = bitmap->width() * bitmap->height(); | 84 int pixel_count = bitmap->width() * bitmap->height(); |
| 83 return static_cast<double>(color_count) / pixel_count; | 85 return static_cast<double>(color_count) / pixel_count; |
| 84 } | 86 } |
| 85 | 87 |
| 86 ChromeRenderObserver::ChromeRenderObserver( | 88 ChromeRenderViewObserver::ChromeRenderViewObserver( |
| 87 RenderView* render_view, | 89 RenderView* render_view, |
| 88 TranslateHelper* translate_helper, | 90 TranslateHelper* translate_helper, |
| 89 safe_browsing::PhishingClassifierDelegate* phishing_classifier) | 91 safe_browsing::PhishingClassifierDelegate* phishing_classifier) |
| 90 : RenderViewObserver(render_view), | 92 : RenderViewObserver(render_view), |
| 91 translate_helper_(translate_helper), | 93 translate_helper_(translate_helper), |
| 92 phishing_classifier_(phishing_classifier), | 94 phishing_classifier_(phishing_classifier), |
| 93 last_indexed_page_id_(-1), | 95 last_indexed_page_id_(-1), |
| 94 ALLOW_THIS_IN_INITIALIZER_LIST(page_info_method_factory_(this)) { | 96 ALLOW_THIS_IN_INITIALIZER_LIST(page_info_method_factory_(this)) { |
| 95 } | 97 } |
| 96 | 98 |
| 97 ChromeRenderObserver::~ChromeRenderObserver() { | 99 ChromeRenderViewObserver::~ChromeRenderViewObserver() { |
| 98 } | 100 } |
| 99 | 101 |
| 100 bool ChromeRenderObserver::OnMessageReceived(const IPC::Message& message) { | 102 bool ChromeRenderViewObserver::OnMessageReceived(const IPC::Message& message) { |
| 101 bool handled = true; | 103 bool handled = true; |
| 102 IPC_BEGIN_MESSAGE_MAP(ChromeRenderObserver, message) | 104 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message) |
| 103 IPC_MESSAGE_HANDLER(ViewMsg_CaptureSnapshot, OnCaptureSnapshot) | 105 IPC_MESSAGE_HANDLER(ViewMsg_CaptureSnapshot, OnCaptureSnapshot) |
| 104 IPC_MESSAGE_UNHANDLED(handled = false) | 106 IPC_MESSAGE_UNHANDLED(handled = false) |
| 105 IPC_END_MESSAGE_MAP() | 107 IPC_END_MESSAGE_MAP() |
| 108 |
| 109 // Filter only. |
| 110 IPC_BEGIN_MESSAGE_MAP(ChromeRenderViewObserver, message) |
| 111 IPC_MESSAGE_HANDLER(ViewMsg_Navigate, OnNavigate) |
| 112 IPC_END_MESSAGE_MAP() |
| 113 |
| 106 return handled; | 114 return handled; |
| 107 } | 115 } |
| 108 | 116 |
| 109 void ChromeRenderObserver::OnCaptureSnapshot() { | 117 void ChromeRenderViewObserver::OnCaptureSnapshot() { |
| 110 SkBitmap snapshot; | 118 SkBitmap snapshot; |
| 111 bool error = false; | 119 bool error = false; |
| 112 | 120 |
| 113 WebFrame* main_frame = render_view()->webview()->mainFrame(); | 121 WebFrame* main_frame = render_view()->webview()->mainFrame(); |
| 114 if (!main_frame) | 122 if (!main_frame) |
| 115 error = true; | 123 error = true; |
| 116 | 124 |
| 117 if (!error && !CaptureSnapshot(render_view()->webview(), &snapshot)) | 125 if (!error && !CaptureSnapshot(render_view()->webview(), &snapshot)) |
| 118 error = true; | 126 error = true; |
| 119 | 127 |
| 120 DCHECK(error == snapshot.empty()) << | 128 DCHECK(error == snapshot.empty()) << |
| 121 "Snapshot should be empty on error, non-empty otherwise."; | 129 "Snapshot should be empty on error, non-empty otherwise."; |
| 122 | 130 |
| 123 // Send the snapshot to the browser process. | 131 // Send the snapshot to the browser process. |
| 124 Send(new ViewHostMsg_Snapshot(routing_id(), snapshot)); | 132 Send(new ViewHostMsg_Snapshot(routing_id(), snapshot)); |
| 125 } | 133 } |
| 126 | 134 |
| 127 void ChromeRenderObserver::DidStopLoading() { | 135 void ChromeRenderViewObserver::OnNavigate( |
| 136 const ViewMsg_Navigate_Params& params) { |
| 137 AboutHandler::MaybeHandle(params.url); |
| 138 } |
| 139 |
| 140 void ChromeRenderViewObserver::DidStopLoading() { |
| 128 MessageLoop::current()->PostDelayedTask( | 141 MessageLoop::current()->PostDelayedTask( |
| 129 FROM_HERE, | 142 FROM_HERE, |
| 130 page_info_method_factory_.NewRunnableMethod( | 143 page_info_method_factory_.NewRunnableMethod( |
| 131 &ChromeRenderObserver::CapturePageInfo, render_view()->page_id(), | 144 &ChromeRenderViewObserver::CapturePageInfo, render_view()->page_id(), |
| 132 false), | 145 false), |
| 133 render_view()->content_state_immediately() ? 0 : kDelayForCaptureMs); | 146 render_view()->content_state_immediately() ? 0 : kDelayForCaptureMs); |
| 134 } | 147 } |
| 135 | 148 |
| 136 void ChromeRenderObserver::DidCommitProvisionalLoad(WebFrame* frame, | 149 void ChromeRenderViewObserver::DidCommitProvisionalLoad( |
| 137 bool is_new_navigation) { | 150 WebFrame* frame, bool is_new_navigation) { |
| 138 if (!is_new_navigation) | 151 if (!is_new_navigation) |
| 139 return; | 152 return; |
| 140 | 153 |
| 141 MessageLoop::current()->PostDelayedTask( | 154 MessageLoop::current()->PostDelayedTask( |
| 142 FROM_HERE, | 155 FROM_HERE, |
| 143 page_info_method_factory_.NewRunnableMethod( | 156 page_info_method_factory_.NewRunnableMethod( |
| 144 &ChromeRenderObserver::CapturePageInfo, render_view()->page_id(), | 157 &ChromeRenderViewObserver::CapturePageInfo, render_view()->page_id(), |
| 145 true), | 158 true), |
| 146 kDelayForForcedCaptureMs); | 159 kDelayForForcedCaptureMs); |
| 147 } | 160 } |
| 148 | 161 |
| 149 void ChromeRenderObserver::CapturePageInfo(int load_id, | 162 void ChromeRenderViewObserver::CapturePageInfo(int load_id, |
| 150 bool preliminary_capture) { | 163 bool preliminary_capture) { |
| 151 if (load_id != render_view()->page_id()) | 164 if (load_id != render_view()->page_id()) |
| 152 return; // This capture call is no longer relevant due to navigation. | 165 return; // This capture call is no longer relevant due to navigation. |
| 153 | 166 |
| 154 if (load_id == last_indexed_page_id_) | 167 if (load_id == last_indexed_page_id_) |
| 155 return; // we already indexed this page | 168 return; // we already indexed this page |
| 156 | 169 |
| 157 if (!render_view()->webview()) | 170 if (!render_view()->webview()) |
| 158 return; | 171 return; |
| 159 | 172 |
| 160 WebFrame* main_frame = render_view()->webview()->mainFrame(); | 173 WebFrame* main_frame = render_view()->webview()->mainFrame(); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 if (!CommandLine::ForCurrentProcess()->HasSwitch( | 209 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 197 switches::kEnableInBrowserThumbnailing)) { | 210 switches::kEnableInBrowserThumbnailing)) { |
| 198 CaptureThumbnail(); | 211 CaptureThumbnail(); |
| 199 } | 212 } |
| 200 | 213 |
| 201 // Will swap out the string. | 214 // Will swap out the string. |
| 202 if (phishing_classifier_) | 215 if (phishing_classifier_) |
| 203 phishing_classifier_->PageCaptured(&contents, preliminary_capture); | 216 phishing_classifier_->PageCaptured(&contents, preliminary_capture); |
| 204 } | 217 } |
| 205 | 218 |
| 206 void ChromeRenderObserver::CaptureText(WebFrame* frame, string16* contents) { | 219 void ChromeRenderViewObserver::CaptureText(WebFrame* frame, |
| 220 string16* contents) { |
| 207 contents->clear(); | 221 contents->clear(); |
| 208 if (!frame) | 222 if (!frame) |
| 209 return; | 223 return; |
| 210 | 224 |
| 211 #ifdef TIME_TEXT_RETRIEVAL | 225 #ifdef TIME_TEXT_RETRIEVAL |
| 212 double begin = time_util::GetHighResolutionTimeNow(); | 226 double begin = time_util::GetHighResolutionTimeNow(); |
| 213 #endif | 227 #endif |
| 214 | 228 |
| 215 // get the contents of the frame | 229 // get the contents of the frame |
| 216 *contents = frame->contentAsText(kMaxIndexChars); | 230 *contents = frame->contentAsText(kMaxIndexChars); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 227 // partial word indexed at the end that might have been clipped. Therefore, | 241 // partial word indexed at the end that might have been clipped. Therefore, |
| 228 // terminate the string at the last space to ensure no words are clipped. | 242 // terminate the string at the last space to ensure no words are clipped. |
| 229 if (contents->size() == kMaxIndexChars) { | 243 if (contents->size() == kMaxIndexChars) { |
| 230 size_t last_space_index = contents->find_last_of(kWhitespaceUTF16); | 244 size_t last_space_index = contents->find_last_of(kWhitespaceUTF16); |
| 231 if (last_space_index == std::wstring::npos) | 245 if (last_space_index == std::wstring::npos) |
| 232 return; // don't index if we got a huge block of text with no spaces | 246 return; // don't index if we got a huge block of text with no spaces |
| 233 contents->resize(last_space_index); | 247 contents->resize(last_space_index); |
| 234 } | 248 } |
| 235 } | 249 } |
| 236 | 250 |
| 237 void ChromeRenderObserver::CaptureThumbnail() { | 251 void ChromeRenderViewObserver::CaptureThumbnail() { |
| 238 WebFrame* main_frame = render_view()->webview()->mainFrame(); | 252 WebFrame* main_frame = render_view()->webview()->mainFrame(); |
| 239 if (!main_frame) | 253 if (!main_frame) |
| 240 return; | 254 return; |
| 241 | 255 |
| 242 // get the URL for this page | 256 // get the URL for this page |
| 243 GURL url(main_frame->url()); | 257 GURL url(main_frame->url()); |
| 244 if (url.is_empty()) | 258 if (url.is_empty()) |
| 245 return; | 259 return; |
| 246 | 260 |
| 247 if (render_view()->size().IsEmpty()) | 261 if (render_view()->size().IsEmpty()) |
| 248 return; // Don't create an empty thumbnail! | 262 return; // Don't create an empty thumbnail! |
| 249 | 263 |
| 250 ThumbnailScore score; | 264 ThumbnailScore score; |
| 251 SkBitmap thumbnail; | 265 SkBitmap thumbnail; |
| 252 if (!CaptureFrameThumbnail(render_view()->webview(), kThumbnailWidth, | 266 if (!CaptureFrameThumbnail(render_view()->webview(), kThumbnailWidth, |
| 253 kThumbnailHeight, &thumbnail, &score)) | 267 kThumbnailHeight, &thumbnail, &score)) |
| 254 return; | 268 return; |
| 255 | 269 |
| 256 // send the thumbnail message to the browser process | 270 // send the thumbnail message to the browser process |
| 257 Send(new ViewHostMsg_Thumbnail(routing_id(), url, score, thumbnail)); | 271 Send(new ViewHostMsg_Thumbnail(routing_id(), url, score, thumbnail)); |
| 258 } | 272 } |
| 259 | 273 |
| 260 bool ChromeRenderObserver::CaptureFrameThumbnail(WebView* view, | 274 bool ChromeRenderViewObserver::CaptureFrameThumbnail(WebView* view, |
| 261 int w, | 275 int w, |
| 262 int h, | 276 int h, |
| 263 SkBitmap* thumbnail, | 277 SkBitmap* thumbnail, |
| 264 ThumbnailScore* score) { | 278 ThumbnailScore* score) { |
| 265 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 279 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
| 266 | 280 |
| 267 skia::PlatformCanvas canvas; | 281 skia::PlatformCanvas canvas; |
| 268 | 282 |
| 269 // Paint |view| into |canvas|. | 283 // Paint |view| into |canvas|. |
| 270 if (!PaintViewIntoCanvas(view, canvas)) | 284 if (!PaintViewIntoCanvas(view, canvas)) |
| 271 return false; | 285 return false; |
| 272 | 286 |
| 273 skia::BitmapPlatformDevice& device = | 287 skia::BitmapPlatformDevice& device = |
| 274 static_cast<skia::BitmapPlatformDevice&>(canvas.getTopPlatformDevice()); | 288 static_cast<skia::BitmapPlatformDevice&>(canvas.getTopPlatformDevice()); |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 319 downsampled_subset, skia::ImageOperations::RESIZE_LANCZOS3, w, h); | 333 downsampled_subset, skia::ImageOperations::RESIZE_LANCZOS3, w, h); |
| 320 | 334 |
| 321 score->boring_score = CalculateBoringScore(thumbnail); | 335 score->boring_score = CalculateBoringScore(thumbnail); |
| 322 | 336 |
| 323 HISTOGRAM_TIMES("Renderer4.Thumbnail", | 337 HISTOGRAM_TIMES("Renderer4.Thumbnail", |
| 324 base::TimeTicks::Now() - beginning_time); | 338 base::TimeTicks::Now() - beginning_time); |
| 325 | 339 |
| 326 return true; | 340 return true; |
| 327 } | 341 } |
| 328 | 342 |
| 329 bool ChromeRenderObserver::CaptureSnapshot(WebView* view, SkBitmap* snapshot) { | 343 bool ChromeRenderViewObserver::CaptureSnapshot(WebView* view, |
| 344 SkBitmap* snapshot) { |
| 330 base::TimeTicks beginning_time = base::TimeTicks::Now(); | 345 base::TimeTicks beginning_time = base::TimeTicks::Now(); |
| 331 | 346 |
| 332 skia::PlatformCanvas canvas; | 347 skia::PlatformCanvas canvas; |
| 333 if (!PaintViewIntoCanvas(view, canvas)) | 348 if (!PaintViewIntoCanvas(view, canvas)) |
| 334 return false; | 349 return false; |
| 335 | 350 |
| 336 skia::BitmapPlatformDevice& device = | 351 skia::BitmapPlatformDevice& device = |
| 337 static_cast<skia::BitmapPlatformDevice&>(canvas.getTopPlatformDevice()); | 352 static_cast<skia::BitmapPlatformDevice&>(canvas.getTopPlatformDevice()); |
| 338 | 353 |
| 339 const SkBitmap& bitmap = device.accessBitmap(false); | 354 const SkBitmap& bitmap = device.accessBitmap(false); |
| 340 if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config)) | 355 if (!bitmap.copyTo(snapshot, SkBitmap::kARGB_8888_Config)) |
| 341 return false; | 356 return false; |
| 342 | 357 |
| 343 HISTOGRAM_TIMES("Renderer4.Snapshot", | 358 HISTOGRAM_TIMES("Renderer4.Snapshot", |
| 344 base::TimeTicks::Now() - beginning_time); | 359 base::TimeTicks::Now() - beginning_time); |
| 345 return true; | 360 return true; |
| 346 } | 361 } |
| OLD | NEW |