| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_frame_observer.h" | 5 #include "chrome/renderer/chrome_render_frame_observer.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 | 42 |
| 43 using blink::WebDataSource; | 43 using blink::WebDataSource; |
| 44 using blink::WebElement; | 44 using blink::WebElement; |
| 45 using blink::WebLocalFrame; | 45 using blink::WebLocalFrame; |
| 46 using blink::WebNode; | 46 using blink::WebNode; |
| 47 using blink::WebNodeList; | 47 using blink::WebNodeList; |
| 48 using blink::WebString; | 48 using blink::WebString; |
| 49 using content::SSLStatus; | 49 using content::SSLStatus; |
| 50 using content::RenderFrame; | 50 using content::RenderFrame; |
| 51 | 51 |
| 52 // Delay in milliseconds that we'll wait before capturing the page contents. | 52 // // Delay in milliseconds that we'll wait before capturing the page contents. |
| 53 static const int kDelayForCaptureMs = 500; | 53 // static const int kDelayForCaptureMs = 500; |
| 54 | 54 |
| 55 // Typically, we capture the page data once the page is loaded. | 55 // Typically, we capture the page data once the page is loaded. |
| 56 // Sometimes, the page never finishes to load, preventing the page capture | 56 // Sometimes, the page never finishes to load, preventing the page capture |
| 57 // To workaround this problem, we always perform a capture after the following | 57 // To workaround this problem, we always perform a capture after the following |
| 58 // delay. | 58 // delay. |
| 59 static const int kDelayForForcedCaptureMs = 6000; | 59 // static const int kDelayForForcedCaptureMs = 6000; |
| 60 | 60 |
| 61 // Maximum number of characters in the document to index. | 61 // Maximum number of characters in the document to index. |
| 62 // Any text beyond this point will be clipped. | 62 // Any text beyond this point will be clipped. |
| 63 static const size_t kMaxIndexChars = 65535; | 63 static const size_t kMaxIndexChars = 65535; |
| 64 | 64 |
| 65 // Constants for UMA statistic collection. | 65 // Constants for UMA statistic collection. |
| 66 static const char kTranslateCaptureText[] = "Translate.CaptureText"; | 66 static const char kTranslateCaptureText[] = "Translate.CaptureText"; |
| 67 | 67 |
| 68 namespace { | 68 namespace { |
| 69 | 69 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 static_cast<int>(scaled_size.height())); | 103 static_cast<int>(scaled_size.height())); |
| 104 } | 104 } |
| 105 | 105 |
| 106 } // namespace | 106 } // namespace |
| 107 | 107 |
| 108 PageInfo::PageInfo(PageInfoReceiver* context) | 108 PageInfo::PageInfo(PageInfoReceiver* context) |
| 109 : context_(context), capture_timer_(false, false) { | 109 : context_(context), capture_timer_(false, false) { |
| 110 DCHECK(context_); | 110 DCHECK(context_); |
| 111 } | 111 } |
| 112 | 112 |
| 113 // TODO(dglazkov): Refactor to remove the RenderFrame* argument. | 113 // // TODO(dglazkov): Refactor to remove the RenderFrame* argument. |
| 114 void PageInfo::CapturePageInfoLater(CaptureType capture_type, | 114 // void PageInfo::CapturePageInfoLater(CaptureType capture_type, |
| 115 RenderFrame* render_frame, | 115 // RenderFrame* render_frame, |
| 116 base::TimeDelta delay) { | 116 // base::TimeDelta delay) { |
| 117 capture_timer_.Start( | 117 // capture_timer_.Start( |
| 118 FROM_HERE, delay, | 118 // FROM_HERE, delay, |
| 119 base::Bind(&PageInfo::CapturePageInfo, base::Unretained(this), | 119 // base::Bind(&PageInfo::CapturePageInfo, base::Unretained(this), |
| 120 render_frame, capture_type)); | 120 // render_frame, capture_type)); |
| 121 } | 121 // } |
| 122 | 122 |
| 123 bool PageInfo::IsErrorPage(WebLocalFrame* frame) { | 123 bool PageInfo::IsErrorPage(WebLocalFrame* frame) { |
| 124 WebDataSource* ds = frame->dataSource(); | 124 WebDataSource* ds = frame->dataSource(); |
| 125 return ds && ds->hasUnreachableURL(); | 125 return ds && ds->hasUnreachableURL(); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void PageInfo::CapturePageInfo(RenderFrame* render_frame, | 128 void PageInfo::CapturePageInfo(RenderFrame* render_frame, |
| 129 CaptureType capture_type) { | 129 CaptureType capture_type) { |
| 130 if (!render_frame) | 130 if (!render_frame) |
| 131 return; | 131 return; |
| 132 | 132 |
| 133 WebLocalFrame* frame = render_frame->GetWebFrame(); | 133 WebLocalFrame* frame = render_frame->GetWebFrame(); |
| 134 if (!frame) | 134 if (!frame) |
| 135 return; | 135 return; |
| 136 | 136 |
| 137 // Don't capture pages that have pending redirect or location change. |
| 138 if (frame->isNavigationScheduled()) |
| 139 return; |
| 140 |
| 137 // Don't index/capture pages that are in view source mode. | 141 // Don't index/capture pages that are in view source mode. |
| 138 if (frame->isViewSourceModeEnabled()) | 142 if (frame->isViewSourceModeEnabled()) |
| 139 return; | 143 return; |
| 140 | 144 |
| 141 if (IsErrorPage(frame)) | 145 if (IsErrorPage(frame)) |
| 142 return; | 146 return; |
| 143 | 147 |
| 144 // Don't index/capture pages that are being prerendered. | 148 // Don't index/capture pages that are being prerendered. |
| 145 if (prerender::PrerenderHelper::IsPrerendering(render_frame)) { | 149 if (prerender::PrerenderHelper::IsPrerendering(render_frame)) { |
| 146 return; | 150 return; |
| (...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 if (frame->parent()) | 370 if (frame->parent()) |
| 367 return; | 371 return; |
| 368 | 372 |
| 369 GURL osdd_url = frame->document().openSearchDescriptionURL(); | 373 GURL osdd_url = frame->document().openSearchDescriptionURL(); |
| 370 if (!osdd_url.is_empty()) { | 374 if (!osdd_url.is_empty()) { |
| 371 Send(new ChromeViewHostMsg_PageHasOSDD( | 375 Send(new ChromeViewHostMsg_PageHasOSDD( |
| 372 routing_id(), frame->document().url(), osdd_url, | 376 routing_id(), frame->document().url(), osdd_url, |
| 373 search_provider::AUTODETECTED_PROVIDER)); | 377 search_provider::AUTODETECTED_PROVIDER)); |
| 374 } | 378 } |
| 375 | 379 |
| 376 // Don't capture pages that have pending redirect or location change. | 380 // // TODO(dglazkov): This is only necessary for testing and indicates that |
| 377 if (frame->isNavigationScheduled()) | 381 // // the tests that rely on this specific timing need to be rewritten to |
| 378 return; | 382 // // avoid this kludge. |
| 379 | 383 // if (render_frame()->GetRenderView()->GetContentStateImmediately()) { |
| 380 page_info_.CapturePageInfoLater( | 384 // page_info_.CapturePageInfo(render_frame(), FINAL_CAPTURE); |
| 381 FINAL_CAPTURE, render_frame(), | |
| 382 base::TimeDelta::FromMilliseconds( | |
| 383 render_frame()->GetRenderView()->GetContentStateImmediately() | |
| 384 ? 0 | |
| 385 : kDelayForCaptureMs)); | |
| 386 } | 385 } |
| 387 | 386 |
| 388 void ChromeRenderFrameObserver::DidStartProvisionalLoad() { | 387 void ChromeRenderFrameObserver::DidStartProvisionalLoad() { |
| 389 // Let translate_helper do any preparatory work for loading a URL. | 388 // Let translate_helper do any preparatory work for loading a URL. |
| 390 if (!translate_helper_) | 389 if (!translate_helper_) |
| 391 return; | 390 return; |
| 392 | 391 |
| 393 translate_helper_->PrepareForUrl( | 392 translate_helper_->PrepareForUrl( |
| 394 render_frame()->GetWebFrame()->document().url()); | 393 render_frame()->GetWebFrame()->document().url()); |
| 395 } | 394 } |
| 396 | 395 |
| 397 void ChromeRenderFrameObserver::DidCommitProvisionalLoad( | 396 void ChromeRenderFrameObserver::DidCommitProvisionalLoad( |
| 398 bool is_new_navigation, | 397 bool is_new_navigation, |
| 399 bool is_same_page_navigation) { | 398 bool is_same_page_navigation) { |
| 400 WebLocalFrame* frame = render_frame()->GetWebFrame(); | 399 WebLocalFrame* frame = render_frame()->GetWebFrame(); |
| 401 | 400 |
| 402 // Don't do anything for subframes. | 401 // Don't do anything for subframes. |
| 403 if (frame->parent()) | 402 if (frame->parent()) |
| 404 return; | 403 return; |
| 405 | 404 |
| 406 // Don't capture pages being not new, with pending redirect, or location | 405 // // Don't capture pages being not new, with pending redirect, or location |
| 407 // change. | 406 // // change. |
| 408 if (!is_new_navigation || frame->isNavigationScheduled()) | 407 // if (!is_new_navigation || frame->isNavigationScheduled()) |
| 409 return; | 408 // return; |
| 410 | 409 |
| 411 base::debug::SetCrashKeyValue( | 410 base::debug::SetCrashKeyValue( |
| 412 crash_keys::kViewCount, | 411 crash_keys::kViewCount, |
| 413 base::SizeTToString(content::RenderView::GetRenderViewCount())); | 412 base::SizeTToString(content::RenderView::GetRenderViewCount())); |
| 413 } |
| 414 | 414 |
| 415 page_info_.CapturePageInfoLater( | 415 void ChromeRenderFrameObserver::DidMeaningfulLayout( |
| 416 PRELIMINARY_CAPTURE, render_frame(), | 416 blink::WebMeaningfulLayout layout_type) { |
| 417 base::TimeDelta::FromMilliseconds(kDelayForForcedCaptureMs)); | 417 switch (layout_type) { |
| 418 case blink::WebMeaningfulLayout::FinishedParsing: |
| 419 page_info_.CapturePageInfo(render_frame(), PRELIMINARY_CAPTURE); |
| 420 break; |
| 421 case blink::WebMeaningfulLayout::FinishedLoading: |
| 422 page_info_.CapturePageInfo(render_frame(), FINAL_CAPTURE); |
| 423 break; |
| 424 default: |
| 425 break; |
| 426 } |
| 418 } | 427 } |
| 419 | 428 |
| 420 void ChromeRenderFrameObserver::PageCaptured(base::string16* content, | 429 void ChromeRenderFrameObserver::PageCaptured(base::string16* content, |
| 421 CaptureType capture_type) { | 430 CaptureType capture_type) { |
| 422 if (translate_helper_) | 431 bool is_preliminary_capture = capture_type == PRELIMINARY_CAPTURE; |
| 432 if (translate_helper_ && is_preliminary_capture) |
| 423 translate_helper_->PageCaptured(*content); | 433 translate_helper_->PageCaptured(*content); |
| 424 | 434 |
| 425 TRACE_EVENT0("renderer", "ChromeRenderViewObserver::CapturePageInfo"); | 435 TRACE_EVENT0("renderer", "ChromeRenderFrameObserver::CapturePageInfo"); |
| 426 | 436 |
| 427 #if defined(FULL_SAFE_BROWSING) | 437 #if defined(FULL_SAFE_BROWSING) |
| 428 // Will swap out the string. | 438 // Will swap out the string. |
| 429 if (phishing_classifier_) | 439 if (phishing_classifier_) |
| 430 phishing_classifier_->PageCaptured(content, | 440 phishing_classifier_->PageCaptured(content, is_preliminary_capture); |
| 431 capture_type == PRELIMINARY_CAPTURE); | |
| 432 #endif | 441 #endif |
| 433 } | 442 } |
| OLD | NEW |