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

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

Issue 925623002: Refactor the loading tracking logic in WebContentsImpl. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments + add test Created 5 years, 9 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
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/lazy_instance.h" 10 #include "base/lazy_instance.h"
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
113 #endif 113 #endif
114 114
115 #if defined(OS_MACOSX) 115 #if defined(OS_MACOSX)
116 #include "base/mac/foundation_util.h" 116 #include "base/mac/foundation_util.h"
117 #endif 117 #endif
118 118
119 namespace content { 119 namespace content {
120 namespace { 120 namespace {
121 121
122 const int kMinimumDelayBetweenLoadingUpdatesMS = 100; 122 const int kMinimumDelayBetweenLoadingUpdatesMS = 100;
123
124 // This matches what Blink's ProgressTracker has traditionally used for a
125 // minimum progress value.
126 const double kMinimumLoadingProgress = 0.1;
127
128 const char kDotGoogleDotCom[] = ".google.com"; 123 const char kDotGoogleDotCom[] = ".google.com";
129 124
130 #if defined(OS_ANDROID) 125 #if defined(OS_ANDROID)
131 const char kWebContentsAndroidKey[] = "web_contents_android"; 126 const char kWebContentsAndroidKey[] = "web_contents_android";
132 #endif // OS_ANDROID 127 #endif // OS_ANDROID
133 128
134 base::LazyInstance<std::vector<WebContentsImpl::CreatedCallback> > 129 base::LazyInstance<std::vector<WebContentsImpl::CreatedCallback> >
135 g_created_callbacks = LAZY_INSTANCE_INITIALIZER; 130 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
136 131
137 static int StartDownload(RenderFrameHost* rfh, 132 static int StartDownload(RenderFrameHost* rfh,
(...skipping 18 matching lines...) Expand all
156 } 151 }
157 152
158 // Helper function for retrieving all the sites in a frame tree. 153 // Helper function for retrieving all the sites in a frame tree.
159 bool CollectSites(BrowserContext* context, 154 bool CollectSites(BrowserContext* context,
160 std::set<GURL>* sites, 155 std::set<GURL>* sites,
161 FrameTreeNode* node) { 156 FrameTreeNode* node) {
162 sites->insert(SiteInstance::GetSiteForURL(context, node->current_url())); 157 sites->insert(SiteInstance::GetSiteForURL(context, node->current_url()));
163 return true; 158 return true;
164 } 159 }
165 160
161 // Helper function used with FrameTree::ForEach() for retrieving the total
162 // loading progress and number of frames in a frame tree.
163 bool CollectLoadProgress(double* progress,
164 int* frame_count,
165 FrameTreeNode* node) {
166 *progress += node->GetLoadingProgress();
167 (*frame_count)++;
168 return true;
169 }
170
171 // Helper function used with FrameTree::ForEach() to check if at least one of
172 // the nodes is loading.
173 bool IsNodeLoading(bool* is_loading, FrameTreeNode* node) {
174 if (node->IsLoading()) {
175 // There is at least one node loading, so abort traversal.
176 *is_loading = true;
177 return false;
178 }
179 return true;
180 }
181
166 bool ForEachFrameInternal( 182 bool ForEachFrameInternal(
167 const base::Callback<void(RenderFrameHost*)>& on_frame, 183 const base::Callback<void(RenderFrameHost*)>& on_frame,
168 FrameTreeNode* node) { 184 FrameTreeNode* node) {
169 on_frame.Run(node->current_frame_host()); 185 on_frame.Run(node->current_frame_host());
170 return true; 186 return true;
171 } 187 }
172 188
173 bool ForEachPendingFrameInternal( 189 bool ForEachPendingFrameInternal(
174 const base::Callback<void(RenderFrameHost*)>& on_frame, 190 const base::Callback<void(RenderFrameHost*)>& on_frame,
175 FrameTreeNode* node) { 191 FrameTreeNode* node) {
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 this, 342 this,
327 this, 343 this,
328 this), 344 this),
329 is_loading_(false), 345 is_loading_(false),
330 is_load_to_different_document_(false), 346 is_load_to_different_document_(false),
331 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING), 347 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
332 crashed_error_code_(0), 348 crashed_error_code_(0),
333 waiting_for_response_(false), 349 waiting_for_response_(false),
334 load_state_(net::LOAD_STATE_IDLE, base::string16()), 350 load_state_(net::LOAD_STATE_IDLE, base::string16()),
335 loading_total_progress_(0.0), 351 loading_total_progress_(0.0),
336 loading_frames_in_progress_(0),
337 upload_size_(0), 352 upload_size_(0),
338 upload_position_(0), 353 upload_position_(0),
339 displayed_insecure_content_(false), 354 displayed_insecure_content_(false),
340 has_accessed_initial_document_(false), 355 has_accessed_initial_document_(false),
341 capturer_count_(0), 356 capturer_count_(0),
342 should_normally_be_visible_(true), 357 should_normally_be_visible_(true),
343 is_being_destroyed_(false), 358 is_being_destroyed_(false),
344 notify_disconnection_(false), 359 notify_disconnection_(false),
345 dialog_manager_(NULL), 360 dialog_manager_(NULL),
346 is_showing_before_unload_dialog_(false), 361 is_showing_before_unload_dialog_(false),
(...skipping 2476 matching lines...) Expand 10 before | Expand all | Expand 10 after
2823 FOR_EACH_OBSERVER( 2838 FOR_EACH_OBSERVER(
2824 WebContentsObserver, observers_, DidFinishLoad(rfh, validated_url)); 2839 WebContentsObserver, observers_, DidFinishLoad(rfh, validated_url));
2825 } 2840 }
2826 2841
2827 void WebContentsImpl::OnDidStartLoading(bool to_different_document) { 2842 void WebContentsImpl::OnDidStartLoading(bool to_different_document) {
2828 if (!HasValidFrameSource()) 2843 if (!HasValidFrameSource())
2829 return; 2844 return;
2830 2845
2831 RenderFrameHostImpl* rfh = 2846 RenderFrameHostImpl* rfh =
2832 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2847 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2833 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2834 2848
2835 // Any main frame load to a new document should reset the load progress, since 2849 // Any main frame load to a new document should reset the load progress, since
2836 // it will replace the current page and any frames. 2850 // it will replace the current page and any frames.
2837 if (to_different_document && !rfh->GetParent()) { 2851 if (to_different_document && !rfh->GetParent()) {
2838 ResetLoadProgressState(); 2852 ResetLoadProgressState();
2839 loading_frames_in_progress_ = 0; 2853 rfh->set_is_loading(false);
2840 rfh->frame_tree_node()->set_is_loading(false); 2854 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressNotStarted);
2841 } 2855 }
2842 2856
2843 // It is possible to get multiple calls to OnDidStartLoading that don't have 2857 // This method should never be called when the frame is loading.
2844 // corresponding calls to OnDidStopLoading: 2858 DCHECK(!rfh->is_loading());
2845 // - With "swappedout://" URLs, this happens when a RenderView gets swapped
2846 // out for a cross-process navigation, and it turns into a placeholder for
2847 // one being rendered in a different process.
2848 // - Also, there might be more than one RenderFrameHost sharing the same
2849 // FrameTreeNode (and thus sharing its ID) each sending a start.
2850 // - But in the future, once clamy@ moves navigation network requests to the
2851 // browser process, there's a good chance that callbacks about starting and
2852 // stopping will all be handled by the browser. When that happens, there
2853 // should no longer be a start/stop call imbalance. TODO(avi): When this
2854 // future arrives, update this code to not allow this case.
2855 if (rfh->frame_tree_node()->is_loading())
2856 return;
2857 2859
2858 DCHECK_GE(loading_frames_in_progress_, 0); 2860 if (!IsFrameTreeLoading())
2859 if (loading_frames_in_progress_ == 0)
2860 DidStartLoading(rfh, to_different_document); 2861 DidStartLoading(rfh, to_different_document);
2861 2862
2862 ++loading_frames_in_progress_; 2863 rfh->set_is_loading(true);
2863 rfh->frame_tree_node()->set_is_loading(true); 2864 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressMinimum);
2864 2865
2865 // Notify the RenderFrameHostManager of the event. 2866 // Notify the RenderFrameHostManager of the event.
2866 rfh->frame_tree_node()->render_manager()->OnDidStartLoading(); 2867 rfh->frame_tree_node()->render_manager()->OnDidStartLoading();
2867 2868
2868 loading_progresses_[render_frame_id] = kMinimumLoadingProgress;
2869 SendLoadProgressChanged(); 2869 SendLoadProgressChanged();
2870 } 2870 }
2871 2871
2872 void WebContentsImpl::OnDidStopLoading() { 2872 void WebContentsImpl::OnDidStopLoading() {
2873 if (!HasValidFrameSource()) 2873 if (!HasValidFrameSource())
2874 return; 2874 return;
2875 2875
2876 RenderFrameHostImpl* rfh = 2876 RenderFrameHostImpl* rfh =
2877 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2877 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2878 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2879 rfh->frame_tree_node()->set_is_loading(false);
2880 2878
2881 if (loading_progresses_.find(render_frame_id) != loading_progresses_.end()) { 2879 // This method should never be called when the frame is not loading.
2882 // Load stopped while we were still tracking load. Make sure we update 2880 DCHECK(rfh->is_loading());
2883 // progress based on this frame's completion. 2881 rfh->set_is_loading(false);
2884 loading_progresses_[render_frame_id] = 1.0; 2882 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressDone);
2885 SendLoadProgressChanged(); 2883
2886 // Then we clean-up our states. 2884 // Update progress based on this frame's completion.
2887 if (loading_total_progress_ == 1.0) 2885 SendLoadProgressChanged();
2888 ResetLoadProgressState(); 2886 // Then clean-up the states.
2889 } 2887 if (loading_total_progress_ == 1.0)
2888 ResetLoadProgressState();
2890 2889
2891 // Notify the RenderFrameHostManager of the event. 2890 // Notify the RenderFrameHostManager of the event.
2892 rfh->frame_tree_node()->render_manager()->OnDidStopLoading(); 2891 rfh->frame_tree_node()->render_manager()->OnDidStopLoading();
2893 2892
2894 // TODO(japhet): This should be a DCHECK, but the pdf plugin sometimes 2893 if (!IsFrameTreeLoading())
2895 // calls DidStopLoading() without a matching DidStartLoading().
2896 if (loading_frames_in_progress_ == 0)
2897 return;
2898 --loading_frames_in_progress_;
2899 if (loading_frames_in_progress_ == 0)
2900 DidStopLoading(rfh); 2894 DidStopLoading(rfh);
2901 } 2895 }
2902 2896
2903 void WebContentsImpl::OnDidChangeLoadProgress(double load_progress) { 2897 void WebContentsImpl::OnDidChangeLoadProgress(double load_progress) {
2904 if (!HasValidFrameSource()) 2898 if (!HasValidFrameSource())
2905 return; 2899 return;
2906 2900
2907 RenderFrameHostImpl* rfh = 2901 RenderFrameHostImpl* rfh =
2908 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2902 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2909 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2910 2903
2911 loading_progresses_[render_frame_id] = load_progress; 2904 rfh->set_loading_progress(load_progress);
2912 2905
2913 // We notify progress change immediately for the first and last updates. 2906 // We notify progress change immediately for the first and last updates.
2914 // Also, since the message loop may be pretty busy when a page is loaded, it 2907 // Also, since the message loop may be pretty busy when a page is loaded, it
2915 // might not execute a posted task in a timely manner so we make sure to 2908 // might not execute a posted task in a timely manner so we make sure to
2916 // immediately send progress report if enough time has passed. 2909 // immediately send progress report if enough time has passed.
2917 base::TimeDelta min_delay = 2910 base::TimeDelta min_delay =
2918 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS); 2911 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
2919 if (load_progress == 1.0 || loading_last_progress_update_.is_null() || 2912 if (load_progress == 1.0 || loading_last_progress_update_.is_null() ||
2920 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay) { 2913 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay) {
2921 // If there is a pending task to send progress, it is now obsolete. 2914 // If there is a pending task to send progress, it is now obsolete.
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after
3383 Details<std::pair<NavigationEntry*, bool> >(&details)); 3376 Details<std::pair<NavigationEntry*, bool> >(&details));
3384 3377
3385 return true; 3378 return true;
3386 } 3379 }
3387 3380
3388 void WebContentsImpl::SendLoadProgressChanged() { 3381 void WebContentsImpl::SendLoadProgressChanged() {
3389 loading_last_progress_update_ = base::TimeTicks::Now(); 3382 loading_last_progress_update_ = base::TimeTicks::Now();
3390 double progress = 0.0; 3383 double progress = 0.0;
3391 int frame_count = 0; 3384 int frame_count = 0;
3392 3385
3393 for (LoadingProgressMap::iterator it = loading_progresses_.begin(); 3386 frame_tree_.ForEach(
3394 it != loading_progresses_.end(); 3387 base::Bind(&CollectLoadProgress, &progress, &frame_count));
3395 ++it) { 3388 DCHECK_GT(frame_count, 0);
3396 progress += it->second; 3389
3397 ++frame_count;
3398 }
3399 if (frame_count == 0)
3400 return;
3401 progress /= frame_count; 3390 progress /= frame_count;
3402 DCHECK(progress <= 1.0); 3391 DCHECK(progress <= 1.0);
3403 3392
3404 if (progress <= loading_total_progress_) 3393 if (progress <= loading_total_progress_)
3405 return; 3394 return;
3406 loading_total_progress_ = progress; 3395 loading_total_progress_ = progress;
3407 3396
3408 if (delegate_) 3397 if (delegate_)
3409 delegate_->LoadProgressChanged(this, progress); 3398 delegate_->LoadProgressChanged(this, progress);
3410 } 3399 }
3411 3400
3412 void WebContentsImpl::ResetLoadProgressState() { 3401 void WebContentsImpl::ResetLoadProgressState() {
3413 loading_progresses_.clear();
clamy 2015/02/26 16:05:00 As mentionned offline, we want to keep the current
Fabrice (no longer in Chrome) 2015/02/27 17:45:25 I like the first option because it fixes a whole b
3414 loading_total_progress_ = 0.0; 3402 loading_total_progress_ = 0.0;
3415 loading_weak_factory_.InvalidateWeakPtrs(); 3403 loading_weak_factory_.InvalidateWeakPtrs();
3416 loading_last_progress_update_ = base::TimeTicks(); 3404 loading_last_progress_update_ = base::TimeTicks();
3417 } 3405 }
3418 3406
3407 bool WebContentsImpl::IsFrameTreeLoading() {
3408 bool is_loading = false;
3409 frame_tree_.ForEach(base::Bind(&IsNodeLoading, &is_loading));
3410 return is_loading;
3411 }
3412
3419 void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host, 3413 void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
3420 RenderViewHost* new_host) { 3414 RenderViewHost* new_host) {
3421 // After sending out a swap notification, we need to send a disconnect 3415 // After sending out a swap notification, we need to send a disconnect
3422 // notification so that clients that pick up a pointer to |this| can NULL the 3416 // notification so that clients that pick up a pointer to |this| can NULL the
3423 // pointer. See Bug 1230284. 3417 // pointer. See Bug 1230284.
3424 notify_disconnection_ = true; 3418 notify_disconnection_ = true;
3425 FOR_EACH_OBSERVER(WebContentsObserver, observers_, 3419 FOR_EACH_OBSERVER(WebContentsObserver, observers_,
3426 RenderViewHostChanged(old_host, new_host)); 3420 RenderViewHostChanged(old_host, new_host));
3427 3421
3428 // TODO(avi): Remove. http://crbug.com/170921 3422 // TODO(avi): Remove. http://crbug.com/170921
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
3720 3714
3721 SetIsLoading(rvh, false, true, NULL); 3715 SetIsLoading(rvh, false, true, NULL);
3722 NotifyDisconnected(); 3716 NotifyDisconnected();
3723 SetIsCrashed(status, error_code); 3717 SetIsCrashed(status, error_code);
3724 3718
3725 // Reset the loading progress. TODO(avi): What does it mean to have a 3719 // Reset the loading progress. TODO(avi): What does it mean to have a
3726 // "renderer crash" when there is more than one renderer process serving a 3720 // "renderer crash" when there is more than one renderer process serving a
3727 // webpage? Once this function is called at a more granular frame level, we 3721 // webpage? Once this function is called at a more granular frame level, we
3728 // probably will need to more granularly reset the state here. 3722 // probably will need to more granularly reset the state here.
3729 ResetLoadProgressState(); 3723 ResetLoadProgressState();
3730 loading_frames_in_progress_ = 0;
3731 3724
3732 FOR_EACH_OBSERVER(WebContentsObserver, 3725 FOR_EACH_OBSERVER(WebContentsObserver,
3733 observers_, 3726 observers_,
3734 RenderProcessGone(GetCrashedStatus())); 3727 RenderProcessGone(GetCrashedStatus()));
3735 } 3728 }
3736 3729
3737 void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) { 3730 void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
3738 FOR_EACH_OBSERVER(WebContentsObserver, observers_, RenderViewDeleted(rvh)); 3731 FOR_EACH_OBSERVER(WebContentsObserver, observers_, RenderViewDeleted(rvh));
3739 } 3732 }
3740 3733
(...skipping 772 matching lines...) Expand 10 before | Expand all | Expand 10 after
4513 node->render_manager()->ResumeResponseDeferredAtStart(); 4506 node->render_manager()->ResumeResponseDeferredAtStart();
4514 } 4507 }
4515 4508
4516 void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) { 4509 void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
4517 force_disable_overscroll_content_ = force_disable; 4510 force_disable_overscroll_content_ = force_disable;
4518 if (view_) 4511 if (view_)
4519 view_->SetOverscrollControllerEnabled(CanOverscrollContent()); 4512 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
4520 } 4513 }
4521 4514
4522 } // namespace content 4515 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698