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

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

Powered by Google App Engine
This is Rietveld 408576698