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

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: Rebase 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 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 2492 matching lines...) Expand 10 before | Expand all | Expand 10 after
2839 FOR_EACH_OBSERVER( 2854 FOR_EACH_OBSERVER(
2840 WebContentsObserver, observers_, DidFinishLoad(rfh, validated_url)); 2855 WebContentsObserver, observers_, DidFinishLoad(rfh, validated_url));
2841 } 2856 }
2842 2857
2843 void WebContentsImpl::OnDidStartLoading(bool to_different_document) { 2858 void WebContentsImpl::OnDidStartLoading(bool to_different_document) {
2844 if (!HasValidFrameSource()) 2859 if (!HasValidFrameSource())
2845 return; 2860 return;
2846 2861
2847 RenderFrameHostImpl* rfh = 2862 RenderFrameHostImpl* rfh =
2848 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2863 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2849 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2850 2864
2851 // Any main frame load to a new document should reset the load progress, since 2865 // Any main frame load to a new document should reset the load progress, since
2852 // it will replace the current page and any frames. 2866 // it will replace the current page and any frames.
2853 if (to_different_document && !rfh->GetParent()) { 2867 if (to_different_document && !rfh->GetParent()) {
2854 ResetLoadProgressState(); 2868 ResetLoadProgressState();
2855 loading_frames_in_progress_ = 0; 2869 rfh->set_is_loading(false);
2856 rfh->frame_tree_node()->set_is_loading(false); 2870 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressNotStarted);
2857 } 2871 }
2858 2872
2859 // It is possible to get multiple calls to OnDidStartLoading that don't have 2873 // This method should never be called when the frame is loading.
2860 // corresponding calls to OnDidStopLoading: 2874 DCHECK(!rfh->is_loading());
2861 // - With "swappedout://" URLs, this happens when a RenderView gets swapped
2862 // out for a cross-process navigation, and it turns into a placeholder for
2863 // one being rendered in a different process.
2864 // - Also, there might be more than one RenderFrameHost sharing the same
2865 // FrameTreeNode (and thus sharing its ID) each sending a start.
2866 // - But in the future, once clamy@ moves navigation network requests to the
2867 // browser process, there's a good chance that callbacks about starting and
2868 // stopping will all be handled by the browser. When that happens, there
2869 // should no longer be a start/stop call imbalance. TODO(avi): When this
2870 // future arrives, update this code to not allow this case.
2871 if (rfh->frame_tree_node()->is_loading())
2872 return;
2873 2875
2874 DCHECK_GE(loading_frames_in_progress_, 0); 2876 if (!IsFrameTreeLoading())
2875 if (loading_frames_in_progress_ == 0)
2876 DidStartLoading(rfh, to_different_document); 2877 DidStartLoading(rfh, to_different_document);
2877 2878
2878 ++loading_frames_in_progress_; 2879 rfh->set_is_loading(true);
2879 rfh->frame_tree_node()->set_is_loading(true); 2880 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressMinimum);
2880 2881
2881 // Notify the RenderFrameHostManager of the event. 2882 // Notify the RenderFrameHostManager of the event.
2882 rfh->frame_tree_node()->render_manager()->OnDidStartLoading(); 2883 rfh->frame_tree_node()->render_manager()->OnDidStartLoading();
2883 2884
2884 loading_progresses_[render_frame_id] = kMinimumLoadingProgress;
2885 SendLoadProgressChanged(); 2885 SendLoadProgressChanged();
2886 } 2886 }
2887 2887
2888 void WebContentsImpl::OnDidStopLoading() { 2888 void WebContentsImpl::OnDidStopLoading() {
2889 if (!HasValidFrameSource()) 2889 if (!HasValidFrameSource())
2890 return; 2890 return;
2891 2891
2892 RenderFrameHostImpl* rfh = 2892 RenderFrameHostImpl* rfh =
2893 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2893 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2894 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2895 rfh->frame_tree_node()->set_is_loading(false);
2896 2894
2897 if (loading_progresses_.find(render_frame_id) != loading_progresses_.end()) { 2895 // This method should never be called when the frame is not loading.
2898 // Load stopped while we were still tracking load. Make sure we update 2896 DCHECK(rfh->is_loading());
2899 // progress based on this frame's completion. 2897 rfh->set_is_loading(false);
2900 loading_progresses_[render_frame_id] = 1.0; 2898 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressDone);
2901 SendLoadProgressChanged(); 2899
2902 // Then we clean-up our states. 2900 // Update progress based on this frame's completion.
2903 if (loading_total_progress_ == 1.0) 2901 SendLoadProgressChanged();
2904 ResetLoadProgressState(); 2902 // Then clean-up the states.
nasko 2015/02/26 23:30:33 nit: Empty line before the comment or combine with
Fabrice (no longer in Chrome) 2015/02/27 17:45:25 Acknowledged.
2905 } 2903 if (loading_total_progress_ == 1.0)
2904 ResetLoadProgressState();
2906 2905
2907 // Notify the RenderFrameHostManager of the event. 2906 // Notify the RenderFrameHostManager of the event.
2908 rfh->frame_tree_node()->render_manager()->OnDidStopLoading(); 2907 rfh->frame_tree_node()->render_manager()->OnDidStopLoading();
2909 2908
2910 // TODO(japhet): This should be a DCHECK, but the pdf plugin sometimes 2909 if (!IsFrameTreeLoading())
2911 // calls DidStopLoading() without a matching DidStartLoading().
2912 if (loading_frames_in_progress_ == 0)
2913 return;
2914 --loading_frames_in_progress_;
2915 if (loading_frames_in_progress_ == 0)
2916 DidStopLoading(rfh); 2910 DidStopLoading(rfh);
2917 } 2911 }
2918 2912
2919 void WebContentsImpl::OnDidChangeLoadProgress(double load_progress) { 2913 void WebContentsImpl::OnDidChangeLoadProgress(double load_progress) {
2920 if (!HasValidFrameSource()) 2914 if (!HasValidFrameSource())
2921 return; 2915 return;
2922 2916
2923 RenderFrameHostImpl* rfh = 2917 RenderFrameHostImpl* rfh =
2924 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2918 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2925 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2926 2919
2927 loading_progresses_[render_frame_id] = load_progress; 2920 rfh->set_loading_progress(load_progress);
2928 2921
2929 // We notify progress change immediately for the first and last updates. 2922 // We notify progress change immediately for the first and last updates.
2930 // Also, since the message loop may be pretty busy when a page is loaded, it 2923 // Also, since the message loop may be pretty busy when a page is loaded, it
2931 // might not execute a posted task in a timely manner so we make sure to 2924 // might not execute a posted task in a timely manner so we make sure to
2932 // immediately send progress report if enough time has passed. 2925 // immediately send progress report if enough time has passed.
2933 base::TimeDelta min_delay = 2926 base::TimeDelta min_delay =
2934 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS); 2927 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
2935 if (load_progress == 1.0 || loading_last_progress_update_.is_null() || 2928 if (load_progress == 1.0 || loading_last_progress_update_.is_null() ||
2936 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay) { 2929 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay) {
2937 // If there is a pending task to send progress, it is now obsolete. 2930 // If there is a pending task to send progress, it is now obsolete.
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
3395 Details<std::pair<NavigationEntry*, bool> >(&details)); 3388 Details<std::pair<NavigationEntry*, bool> >(&details));
3396 3389
3397 return true; 3390 return true;
3398 } 3391 }
3399 3392
3400 void WebContentsImpl::SendLoadProgressChanged() { 3393 void WebContentsImpl::SendLoadProgressChanged() {
3401 loading_last_progress_update_ = base::TimeTicks::Now(); 3394 loading_last_progress_update_ = base::TimeTicks::Now();
3402 double progress = 0.0; 3395 double progress = 0.0;
3403 int frame_count = 0; 3396 int frame_count = 0;
3404 3397
3405 for (LoadingProgressMap::iterator it = loading_progresses_.begin(); 3398 frame_tree_.ForEach(
3406 it != loading_progresses_.end(); 3399 base::Bind(&CollectLoadProgress, &progress, &frame_count));
3407 ++it) { 3400 DCHECK_GT(frame_count, 0);
3408 progress += it->second; 3401
3409 ++frame_count;
3410 }
3411 if (frame_count == 0)
3412 return;
3413 progress /= frame_count; 3402 progress /= frame_count;
3414 DCHECK(progress <= 1.0); 3403 DCHECK(progress <= 1.0);
3415 3404
3416 if (progress <= loading_total_progress_) 3405 if (progress <= loading_total_progress_)
3417 return; 3406 return;
3418 loading_total_progress_ = progress; 3407 loading_total_progress_ = progress;
3419 3408
3420 if (delegate_) 3409 if (delegate_)
3421 delegate_->LoadProgressChanged(this, progress); 3410 delegate_->LoadProgressChanged(this, progress);
3422 } 3411 }
3423 3412
3424 void WebContentsImpl::ResetLoadProgressState() { 3413 void WebContentsImpl::ResetLoadProgressState() {
3425 loading_progresses_.clear();
3426 loading_total_progress_ = 0.0; 3414 loading_total_progress_ = 0.0;
3427 loading_weak_factory_.InvalidateWeakPtrs(); 3415 loading_weak_factory_.InvalidateWeakPtrs();
3428 loading_last_progress_update_ = base::TimeTicks(); 3416 loading_last_progress_update_ = base::TimeTicks();
3429 } 3417 }
3430 3418
3419 bool WebContentsImpl::IsFrameTreeLoading() {
3420 bool is_loading = false;
3421 frame_tree_.ForEach(base::Bind(&IsNodeLoading, &is_loading));
3422 return is_loading;
3423 }
3424
3431 void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host, 3425 void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
3432 RenderViewHost* new_host) { 3426 RenderViewHost* new_host) {
3433 // After sending out a swap notification, we need to send a disconnect 3427 // After sending out a swap notification, we need to send a disconnect
3434 // notification so that clients that pick up a pointer to |this| can NULL the 3428 // notification so that clients that pick up a pointer to |this| can NULL the
3435 // pointer. See Bug 1230284. 3429 // pointer. See Bug 1230284.
3436 notify_disconnection_ = true; 3430 notify_disconnection_ = true;
3437 FOR_EACH_OBSERVER(WebContentsObserver, observers_, 3431 FOR_EACH_OBSERVER(WebContentsObserver, observers_,
3438 RenderViewHostChanged(old_host, new_host)); 3432 RenderViewHostChanged(old_host, new_host));
3439 3433
3440 // TODO(avi): Remove. http://crbug.com/170921 3434 // TODO(avi): Remove. http://crbug.com/170921
(...skipping 279 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