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

Side by Side Diff: content/browser/web_contents/web_contents_impl.cc

Issue 263973003: Move LoadProgressTracker to the browser process. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: final comment Created 6 years, 7 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 #include "content/browser/web_contents/web_contents_impl.h" 5 #include "content/browser/web_contents/web_contents_impl.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/command_line.h" 9 #include "base/command_line.h"
10 #include "base/debug/trace_event.h" 10 #include "base/debug/trace_event.h"
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 // - The pending renderer sends a FrameNavigate message that invokes the 153 // - The pending renderer sends a FrameNavigate message that invokes the
154 // DidNavigate method. This replaces the current RVH with the 154 // DidNavigate method. This replaces the current RVH with the
155 // pending RVH. 155 // pending RVH.
156 // - The previous renderer is kept swapped out in RenderFrameHostManager in case 156 // - The previous renderer is kept swapped out in RenderFrameHostManager in case
157 // the user goes back. The process only stays live if another tab is using 157 // the user goes back. The process only stays live if another tab is using
158 // it, but if so, the existing frame relationships will be maintained. 158 // it, but if so, the existing frame relationships will be maintained.
159 159
160 namespace content { 160 namespace content {
161 namespace { 161 namespace {
162 162
163 const int kMinimumDelayBetweenLoadingUpdatesMS = 100;
164
165 // This matches what Blink's ProgressTracker has traditionally used for a
166 // minimum progress value.
167 const double kMinimumLoadingProgress = 0.1;
168
163 const char kDotGoogleDotCom[] = ".google.com"; 169 const char kDotGoogleDotCom[] = ".google.com";
164 170
165 #if defined(OS_ANDROID) 171 #if defined(OS_ANDROID)
166 const char kWebContentsAndroidKey[] = "web_contents_android"; 172 const char kWebContentsAndroidKey[] = "web_contents_android";
167 #endif // OS_ANDROID 173 #endif // OS_ANDROID
168 174
169 base::LazyInstance<std::vector<WebContentsImpl::CreatedCallback> > 175 base::LazyInstance<std::vector<WebContentsImpl::CreatedCallback> >
170 g_created_callbacks = LAZY_INSTANCE_INITIALIZER; 176 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
171 177
172 static int StartDownload(content::RenderFrameHost* rfh, 178 static int StartDownload(content::RenderFrameHost* rfh,
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 #if defined(OS_WIN) 331 #if defined(OS_WIN)
326 accessible_parent_(NULL), 332 accessible_parent_(NULL),
327 #endif 333 #endif
328 frame_tree_(new NavigatorImpl(&controller_, this), 334 frame_tree_(new NavigatorImpl(&controller_, this),
329 this, this, this, this), 335 this, this, this, this),
330 is_loading_(false), 336 is_loading_(false),
331 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING), 337 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
332 crashed_error_code_(0), 338 crashed_error_code_(0),
333 waiting_for_response_(false), 339 waiting_for_response_(false),
334 load_state_(net::LOAD_STATE_IDLE, base::string16()), 340 load_state_(net::LOAD_STATE_IDLE, base::string16()),
341 loading_total_progress_(0.0),
342 loading_weak_factory_(this),
343 loading_frames_in_progress_(0),
335 upload_size_(0), 344 upload_size_(0),
336 upload_position_(0), 345 upload_position_(0),
337 displayed_insecure_content_(false), 346 displayed_insecure_content_(false),
338 has_accessed_initial_document_(false), 347 has_accessed_initial_document_(false),
339 capturer_count_(0), 348 capturer_count_(0),
340 should_normally_be_visible_(true), 349 should_normally_be_visible_(true),
341 is_being_destroyed_(false), 350 is_being_destroyed_(false),
342 notify_disconnection_(false), 351 notify_disconnection_(false),
343 dialog_manager_(NULL), 352 dialog_manager_(NULL),
344 is_showing_before_unload_dialog_(false), 353 is_showing_before_unload_dialog_(false),
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 503
495 bool handled = true; 504 bool handled = true;
496 IPC_BEGIN_MESSAGE_MAP(WebContentsImpl, message) 505 IPC_BEGIN_MESSAGE_MAP(WebContentsImpl, message)
497 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung) 506 IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung)
498 IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed) 507 IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed)
499 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse, 508 IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
500 OnDomOperationResponse) 509 OnDomOperationResponse)
501 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishDocumentLoad, 510 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishDocumentLoad,
502 OnDocumentLoadedInFrame) 511 OnDocumentLoadedInFrame)
503 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishLoad, OnDidFinishLoad) 512 IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishLoad, OnDidFinishLoad)
513 IPC_MESSAGE_HANDLER(FrameHostMsg_DidStartLoading, OnDidStartLoading)
514 IPC_MESSAGE_HANDLER(FrameHostMsg_DidStopLoading, OnDidStopLoading)
515 IPC_MESSAGE_HANDLER(FrameHostMsg_DidChangeLoadProgress,
516 OnDidChangeLoadProgress)
504 IPC_MESSAGE_HANDLER(FrameHostMsg_OpenColorChooser, OnOpenColorChooser) 517 IPC_MESSAGE_HANDLER(FrameHostMsg_OpenColorChooser, OnOpenColorChooser)
505 IPC_MESSAGE_HANDLER(FrameHostMsg_EndColorChooser, OnEndColorChooser) 518 IPC_MESSAGE_HANDLER(FrameHostMsg_EndColorChooser, OnEndColorChooser)
506 IPC_MESSAGE_HANDLER(FrameHostMsg_SetSelectedColorInColorChooser, 519 IPC_MESSAGE_HANDLER(FrameHostMsg_SetSelectedColorInColorChooser,
507 OnSetSelectedColorInColorChooser) 520 OnSetSelectedColorInColorChooser)
508 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPlayingNotification, 521 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPlayingNotification,
509 OnMediaPlayingNotification) 522 OnMediaPlayingNotification)
510 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPausedNotification, 523 IPC_MESSAGE_HANDLER(FrameHostMsg_MediaPausedNotification,
511 OnMediaPausedNotification) 524 OnMediaPausedNotification)
512 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache, 525 IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
513 OnDidLoadResourceFromMemoryCache) 526 OnDidLoadResourceFromMemoryCache)
(...skipping 1790 matching lines...) Expand 10 before | Expand all | Expand 10 after
2304 delegate_->SetFocusToLocationBar(select_all); 2317 delegate_->SetFocusToLocationBar(select_all);
2305 } 2318 }
2306 2319
2307 void WebContentsImpl::DidStartProvisionalLoad( 2320 void WebContentsImpl::DidStartProvisionalLoad(
2308 RenderFrameHostImpl* render_frame_host, 2321 RenderFrameHostImpl* render_frame_host,
2309 int parent_routing_id, 2322 int parent_routing_id,
2310 const GURL& validated_url, 2323 const GURL& validated_url,
2311 bool is_error_page, 2324 bool is_error_page,
2312 bool is_iframe_srcdoc) { 2325 bool is_iframe_srcdoc) {
2313 bool is_main_frame = render_frame_host->frame_tree_node()->IsMainFrame(); 2326 bool is_main_frame = render_frame_host->frame_tree_node()->IsMainFrame();
2314 if (is_main_frame)
2315 DidChangeLoadProgress(0);
2316 2327
2317 // Notify observers about the start of the provisional load. 2328 // Notify observers about the start of the provisional load.
2318 int render_frame_id = render_frame_host->GetRoutingID(); 2329 int render_frame_id = render_frame_host->GetRoutingID();
2319 RenderViewHost* render_view_host = render_frame_host->render_view_host(); 2330 RenderViewHost* render_view_host = render_frame_host->render_view_host();
2320 FOR_EACH_OBSERVER(WebContentsObserver, observers_, 2331 FOR_EACH_OBSERVER(WebContentsObserver, observers_,
2321 DidStartProvisionalLoadForFrame( 2332 DidStartProvisionalLoadForFrame(
2322 render_frame_id, parent_routing_id, is_main_frame, 2333 render_frame_id, parent_routing_id, is_main_frame,
2323 validated_url, is_error_page, is_iframe_srcdoc, 2334 validated_url, is_error_page, is_iframe_srcdoc,
2324 render_view_host)); 2335 render_view_host));
2325 2336
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
2610 RenderFrameHostImpl* rfh = 2621 RenderFrameHostImpl* rfh =
2611 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2622 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2612 int render_frame_id = rfh->GetRoutingID(); 2623 int render_frame_id = rfh->GetRoutingID();
2613 RenderViewHost* render_view_host = rfh->render_view_host(); 2624 RenderViewHost* render_view_host = rfh->render_view_host();
2614 bool is_main_frame = rfh->frame_tree_node()->IsMainFrame(); 2625 bool is_main_frame = rfh->frame_tree_node()->IsMainFrame();
2615 FOR_EACH_OBSERVER(WebContentsObserver, observers_, 2626 FOR_EACH_OBSERVER(WebContentsObserver, observers_,
2616 DidFinishLoad(render_frame_id, validated_url, 2627 DidFinishLoad(render_frame_id, validated_url,
2617 is_main_frame, render_view_host)); 2628 is_main_frame, render_view_host));
2618 } 2629 }
2619 2630
2631 void WebContentsImpl::OnDidStartLoading(bool to_different_document) {
2632 RenderFrameHostImpl* rfh =
2633 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2634 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2635
2636 // It is possible to get multiple calls to OnDidStartLoading that don't have
2637 // corresponding calls to OnDidStopLoading:
2638 // - With "swappedout://" URLs, this happens when a RenderView gets swapped
2639 // out for a cross-process navigation, and it turns into a placeholder for
2640 // one being rendered in a different process.
2641 // - Also, there might be more than one RenderFrameHost sharing the same
2642 // FrameTreeNode (and thus sharing its ID) each sending a start.
2643 // - But in the future, once clamy@ moves navigation network requests to the
2644 // browser process, there's a good chance that callbacks about starting and
2645 // stopping will all be handled by the browser. When that happens, there
2646 // should no longer be a start/stop call imbalance. TODO(avi): When this
2647 // future arrives, update this code to not allow this case.
2648 DCHECK_GE(loading_frames_in_progress_, 0);
2649 if (loading_progresses_.find(render_frame_id) == loading_progresses_.end()) {
2650 if (loading_frames_in_progress_ == 0)
2651 DidStartLoading(rfh, to_different_document);
2652 ++loading_frames_in_progress_;
2653 }
2654
2655 loading_progresses_[render_frame_id] = kMinimumLoadingProgress;
2656 SendLoadProgressChanged();
2657 }
2658
2659 void WebContentsImpl::OnDidStopLoading() {
2660 RenderFrameHostImpl* rfh =
2661 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2662 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2663
2664 if (loading_progresses_.find(render_frame_id) != loading_progresses_.end()) {
2665 // Load stopped while we were still tracking load. Make sure we update
2666 // progress based on this frame's completion.
2667 loading_progresses_[render_frame_id] = 1.0;
2668 SendLoadProgressChanged();
2669 // Then we clean-up our states.
2670 if (loading_total_progress_ == 1.0)
2671 ResetLoadProgressState();
2672 }
2673
2674 // TODO(japhet): This should be a DCHECK, but the pdf plugin sometimes
2675 // calls DidStopLoading() without a matching DidStartLoading().
2676 if (loading_frames_in_progress_ == 0)
2677 return;
2678 --loading_frames_in_progress_;
2679 if (loading_frames_in_progress_ == 0)
2680 DidStopLoading(rfh);
2681 }
2682
2683 void WebContentsImpl::OnDidChangeLoadProgress(double load_progress) {
2684 RenderFrameHostImpl* rfh =
2685 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2686 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2687
2688 loading_progresses_[render_frame_id] = load_progress;
2689
2690 // We notify progress change immediately for the first and last updates.
2691 // Also, since the message loop may be pretty busy when a page is loaded, it
2692 // might not execute a posted task in a timely manner so we make sure to
2693 // immediately send progress report if enough time has passed.
2694 base::TimeDelta min_delay =
2695 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
2696 if (load_progress == 1.0 || loading_last_progress_update_.is_null() ||
2697 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay) {
2698 // If there is a pending task to send progress, it is now obsolete.
2699 loading_weak_factory_.InvalidateWeakPtrs();
2700 SendLoadProgressChanged();
2701 if (loading_total_progress_ == 1.0)
2702 ResetLoadProgressState();
2703 return;
2704 }
2705
2706 if (loading_weak_factory_.HasWeakPtrs())
2707 return;
2708
2709 base::MessageLoop::current()->PostDelayedTask(
2710 FROM_HERE,
2711 base::Bind(&WebContentsImpl::SendLoadProgressChanged,
2712 loading_weak_factory_.GetWeakPtr()),
2713 min_delay);
2714 }
2715
2620 void WebContentsImpl::OnGoToEntryAtOffset(int offset) { 2716 void WebContentsImpl::OnGoToEntryAtOffset(int offset) {
2621 if (!delegate_ || delegate_->OnGoToEntryOffset(offset)) 2717 if (!delegate_ || delegate_->OnGoToEntryOffset(offset))
2622 controller_.GoToOffset(offset); 2718 controller_.GoToOffset(offset);
2623 } 2719 }
2624 2720
2625 void WebContentsImpl::OnUpdateZoomLimits(int minimum_percent, 2721 void WebContentsImpl::OnUpdateZoomLimits(int minimum_percent,
2626 int maximum_percent, 2722 int maximum_percent,
2627 bool remember) { 2723 bool remember) {
2628 minimum_zoom_percent_ = minimum_percent; 2724 minimum_zoom_percent_ = minimum_percent;
2629 maximum_zoom_percent_ = maximum_percent; 2725 maximum_zoom_percent_ = maximum_percent;
(...skipping 365 matching lines...) Expand 10 before | Expand all | Expand 10 after
2995 std::pair<NavigationEntry*, bool> details = 3091 std::pair<NavigationEntry*, bool> details =
2996 std::make_pair(entry, explicit_set); 3092 std::make_pair(entry, explicit_set);
2997 NotificationService::current()->Notify( 3093 NotificationService::current()->Notify(
2998 NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED, 3094 NOTIFICATION_WEB_CONTENTS_TITLE_UPDATED,
2999 Source<WebContents>(this), 3095 Source<WebContents>(this),
3000 Details<std::pair<NavigationEntry*, bool> >(&details)); 3096 Details<std::pair<NavigationEntry*, bool> >(&details));
3001 3097
3002 return true; 3098 return true;
3003 } 3099 }
3004 3100
3101 void WebContentsImpl::SendLoadProgressChanged() {
3102 loading_last_progress_update_ = base::TimeTicks::Now();
3103 double progress = 0.0;
3104 int frame_count = 0;
3105
3106 for (LoadingProgressMap::iterator it = loading_progresses_.begin();
3107 it != loading_progresses_.end();
3108 ++it) {
3109 progress += it->second;
3110 ++frame_count;
3111 }
3112 if (frame_count == 0)
3113 return;
3114 progress /= frame_count;
3115 DCHECK(progress <= 1.0);
3116
3117 if (progress <= loading_total_progress_)
3118 return;
3119 loading_total_progress_ = progress;
3120
3121 if (delegate_)
3122 delegate_->LoadProgressChanged(this, progress);
3123 }
3124
3125 void WebContentsImpl::ResetLoadProgressState() {
3126 loading_progresses_.clear();
3127 loading_total_progress_ = 0.0;
3128 loading_weak_factory_.InvalidateWeakPtrs();
3129 loading_last_progress_update_ = base::TimeTicks();
3130 }
3131
3005 void WebContentsImpl::NotifySwapped(RenderViewHost* old_host, 3132 void WebContentsImpl::NotifySwapped(RenderViewHost* old_host,
3006 RenderViewHost* new_host) { 3133 RenderViewHost* new_host) {
3007 // After sending out a swap notification, we need to send a disconnect 3134 // After sending out a swap notification, we need to send a disconnect
3008 // notification so that clients that pick up a pointer to |this| can NULL the 3135 // notification so that clients that pick up a pointer to |this| can NULL the
3009 // pointer. See Bug 1230284. 3136 // pointer. See Bug 1230284.
3010 notify_disconnection_ = true; 3137 notify_disconnection_ = true;
3011 FOR_EACH_OBSERVER(WebContentsObserver, observers_, 3138 FOR_EACH_OBSERVER(WebContentsObserver, observers_,
3012 RenderViewHostChanged(old_host, new_host)); 3139 RenderViewHostChanged(old_host, new_host));
3013 3140
3014 // TODO(avi): Remove. http://crbug.com/170921 3141 // TODO(avi): Remove. http://crbug.com/170921
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
3275 ToggleFullscreenMode(false); 3402 ToggleFullscreenMode(false);
3276 3403
3277 // Cancel any visible dialogs so they are not left dangling over the sad tab. 3404 // Cancel any visible dialogs so they are not left dangling over the sad tab.
3278 if (dialog_manager_) 3405 if (dialog_manager_)
3279 dialog_manager_->CancelActiveAndPendingDialogs(this); 3406 dialog_manager_->CancelActiveAndPendingDialogs(this);
3280 3407
3281 SetIsLoading(rvh, false, true, NULL); 3408 SetIsLoading(rvh, false, true, NULL);
3282 NotifyDisconnected(); 3409 NotifyDisconnected();
3283 SetIsCrashed(status, error_code); 3410 SetIsCrashed(status, error_code);
3284 3411
3412 // Reset the loading progress. TODO(avi): What does it mean to have a
3413 // "renderer crash" when there is more than one renderer process serving a
3414 // webpage? Once this function is called at a more granular frame level, we
3415 // probably will need to more granularly reset the state here.
3416 ResetLoadProgressState();
3417 loading_frames_in_progress_ = 0;
3418
3285 #if defined(OS_ANDROID) 3419 #if defined(OS_ANDROID)
3286 if (GetRenderViewHostImpl()->media_player_manager()) 3420 if (GetRenderViewHostImpl()->media_player_manager())
3287 GetRenderViewHostImpl()->media_player_manager()->DestroyAllMediaPlayers(); 3421 GetRenderViewHostImpl()->media_player_manager()->DestroyAllMediaPlayers();
3288 #endif 3422 #endif
3289 3423
3290 FOR_EACH_OBSERVER(WebContentsObserver, 3424 FOR_EACH_OBSERVER(WebContentsObserver,
3291 observers_, 3425 observers_,
3292 RenderProcessGone(GetCrashedStatus())); 3426 RenderProcessGone(GetCrashedStatus()));
3293 } 3427 }
3294 3428
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
3394 details.get()); 3528 details.get());
3395 } 3529 }
3396 3530
3397 void WebContentsImpl::DidCancelLoading() { 3531 void WebContentsImpl::DidCancelLoading() {
3398 controller_.DiscardNonCommittedEntries(); 3532 controller_.DiscardNonCommittedEntries();
3399 3533
3400 // Update the URL display. 3534 // Update the URL display.
3401 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL); 3535 NotifyNavigationStateChanged(INVALIDATE_TYPE_URL);
3402 } 3536 }
3403 3537
3404 void WebContentsImpl::DidChangeLoadProgress(double progress) {
3405 if (delegate_)
3406 delegate_->LoadProgressChanged(this, progress);
3407 }
3408
3409 void WebContentsImpl::DidAccessInitialDocument() { 3538 void WebContentsImpl::DidAccessInitialDocument() {
3410 has_accessed_initial_document_ = true; 3539 has_accessed_initial_document_ = true;
3411 3540
3412 // We may have left a failed browser-initiated navigation in the address bar 3541 // We may have left a failed browser-initiated navigation in the address bar
3413 // to let the user edit it and try again. Clear it now that content might 3542 // to let the user edit it and try again. Clear it now that content might
3414 // show up underneath it. 3543 // show up underneath it.
3415 if (!IsLoading() && controller_.GetPendingEntry()) 3544 if (!IsLoading() && controller_.GetPendingEntry())
3416 controller_.DiscardPendingEntry(); 3545 controller_.DiscardPendingEntry();
3417 3546
3418 // Update the URL display. 3547 // Update the URL display.
(...skipping 542 matching lines...) Expand 10 before | Expand all | Expand 10 after
3961 4090
3962 void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) { 4091 void WebContentsImpl::OnPreferredSizeChanged(const gfx::Size& old_size) {
3963 if (!delegate_) 4092 if (!delegate_)
3964 return; 4093 return;
3965 const gfx::Size new_size = GetPreferredSize(); 4094 const gfx::Size new_size = GetPreferredSize();
3966 if (new_size != old_size) 4095 if (new_size != old_size)
3967 delegate_->UpdatePreferredSize(this, new_size); 4096 delegate_->UpdatePreferredSize(this, new_size);
3968 } 4097 }
3969 4098
3970 } // namespace content 4099 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/web_contents/web_contents_impl.h ('k') | content/browser/web_contents/web_contents_impl_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698