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

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 + rebase 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 #endif 114 #endif
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
125 // This matches what Blink's ProgressTracker has traditionally used for a
126 // minimum progress value.
127 const double kMinimumLoadingProgress = 0.1;
128
129 const char kDotGoogleDotCom[] = ".google.com"; 124 const char kDotGoogleDotCom[] = ".google.com";
130 125
131 #if defined(OS_ANDROID) 126 #if defined(OS_ANDROID)
132 const char kWebContentsAndroidKey[] = "web_contents_android"; 127 const char kWebContentsAndroidKey[] = "web_contents_android";
133 #endif // OS_ANDROID 128 #endif // OS_ANDROID
134 129
135 base::LazyInstance<std::vector<WebContentsImpl::CreatedCallback> > 130 base::LazyInstance<std::vector<WebContentsImpl::CreatedCallback> >
136 g_created_callbacks = LAZY_INSTANCE_INITIALIZER; 131 g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
137 132
138 static int StartDownload(RenderFrameHost* rfh, 133 static int StartDownload(RenderFrameHost* rfh,
(...skipping 18 matching lines...) Expand all
157 } 152 }
158 153
159 // Helper function for retrieving all the sites in a frame tree. 154 // Helper function for retrieving all the sites in a frame tree.
160 bool CollectSites(BrowserContext* context, 155 bool CollectSites(BrowserContext* context,
161 std::set<GURL>* sites, 156 std::set<GURL>* sites,
162 FrameTreeNode* node) { 157 FrameTreeNode* node) {
163 sites->insert(SiteInstance::GetSiteForURL(context, node->current_url())); 158 sites->insert(SiteInstance::GetSiteForURL(context, node->current_url()));
164 return true; 159 return true;
165 } 160 }
166 161
162 // Helper function used with FrameTree::ForEach() for retrieving the total
163 // loading progress and number of frames in a frame tree.
164 bool CollectLoadProgress(double* progress,
165 int* frame_count,
166 FrameTreeNode* node) {
167 // Ignore the current frame if it has not started loading.
168 double frame_progress = node->GetLoadingProgress();
169 if (frame_progress == RenderFrameHostImpl::kLoadingProgressNotStarted)
170 return true;
171
172 // Collect progress.
173 *progress += node->GetLoadingProgress();
174 (*frame_count)++;
175 return true;
176 }
177
178 // Helper function used with FrameTree::ForEach() to check if at least one of
179 // the nodes is loading.
180 bool IsNodeLoading(bool* is_loading, FrameTreeNode* node) {
181 if (node->IsLoading()) {
182 // There is at least one node loading, so abort traversal.
183 *is_loading = true;
184 return false;
185 }
186 return true;
187 }
188
189 // Helper function used with FrameTree::ForEach() to reset the load progress.
190 bool ResetLoadProgress(FrameTreeNode* node) {
191 node->current_frame_host()->set_loading_progress(
192 RenderFrameHostImpl::kLoadingProgressNotStarted);
193 return true;
194 }
195
167 bool ForEachFrameInternal( 196 bool ForEachFrameInternal(
168 const base::Callback<void(RenderFrameHost*)>& on_frame, 197 const base::Callback<void(RenderFrameHost*)>& on_frame,
169 FrameTreeNode* node) { 198 FrameTreeNode* node) {
170 on_frame.Run(node->current_frame_host()); 199 on_frame.Run(node->current_frame_host());
171 return true; 200 return true;
172 } 201 }
173 202
174 bool ForEachPendingFrameInternal( 203 bool ForEachPendingFrameInternal(
175 const base::Callback<void(RenderFrameHost*)>& on_frame, 204 const base::Callback<void(RenderFrameHost*)>& on_frame,
176 FrameTreeNode* node) { 205 FrameTreeNode* node) {
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
327 this, 356 this,
328 this, 357 this,
329 this), 358 this),
330 is_loading_(false), 359 is_loading_(false),
331 is_load_to_different_document_(false), 360 is_load_to_different_document_(false),
332 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING), 361 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
333 crashed_error_code_(0), 362 crashed_error_code_(0),
334 waiting_for_response_(false), 363 waiting_for_response_(false),
335 load_state_(net::LOAD_STATE_IDLE, base::string16()), 364 load_state_(net::LOAD_STATE_IDLE, base::string16()),
336 loading_total_progress_(0.0), 365 loading_total_progress_(0.0),
337 loading_frames_in_progress_(0),
338 upload_size_(0), 366 upload_size_(0),
339 upload_position_(0), 367 upload_position_(0),
340 displayed_insecure_content_(false), 368 displayed_insecure_content_(false),
341 has_accessed_initial_document_(false), 369 has_accessed_initial_document_(false),
342 capturer_count_(0), 370 capturer_count_(0),
343 should_normally_be_visible_(true), 371 should_normally_be_visible_(true),
344 is_being_destroyed_(false), 372 is_being_destroyed_(false),
345 notify_disconnection_(false), 373 notify_disconnection_(false),
346 dialog_manager_(NULL), 374 dialog_manager_(NULL),
347 is_showing_before_unload_dialog_(false), 375 is_showing_before_unload_dialog_(false),
(...skipping 2507 matching lines...) Expand 10 before | Expand all | Expand 10 after
2855 FOR_EACH_OBSERVER( 2883 FOR_EACH_OBSERVER(
2856 WebContentsObserver, observers_, DidFinishLoad(rfh, validated_url)); 2884 WebContentsObserver, observers_, DidFinishLoad(rfh, validated_url));
2857 } 2885 }
2858 2886
2859 void WebContentsImpl::OnDidStartLoading(bool to_different_document) { 2887 void WebContentsImpl::OnDidStartLoading(bool to_different_document) {
2860 if (!HasValidFrameSource()) 2888 if (!HasValidFrameSource())
2861 return; 2889 return;
2862 2890
2863 RenderFrameHostImpl* rfh = 2891 RenderFrameHostImpl* rfh =
2864 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2892 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2865 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2866 2893
2867 // Any main frame load to a new document should reset the load progress, since 2894 // Any main frame load to a new document should reset the load progress, since
2868 // it will replace the current page and any frames. 2895 // it will replace the current page and any frames.
2869 if (to_different_document && !rfh->GetParent()) { 2896 if (to_different_document && !rfh->GetParent()) {
2870 ResetLoadProgressState(); 2897 ResetLoadProgressState();
2871 loading_frames_in_progress_ = 0; 2898 rfh->set_is_loading(false);
2872 rfh->frame_tree_node()->set_is_loading(false);
2873 } 2899 }
2874 2900
2875 // It is possible to get multiple calls to OnDidStartLoading that don't have 2901 // This method should never be called when the frame is loading.
2876 // corresponding calls to OnDidStopLoading: 2902 DCHECK(!rfh->is_loading());
2877 // - With "swappedout://" URLs, this happens when a RenderView gets swapped
2878 // out for a cross-process navigation, and it turns into a placeholder for
2879 // one being rendered in a different process.
2880 // - Also, there might be more than one RenderFrameHost sharing the same
2881 // FrameTreeNode (and thus sharing its ID) each sending a start.
2882 // - But in the future, once clamy@ moves navigation network requests to the
2883 // browser process, there's a good chance that callbacks about starting and
2884 // stopping will all be handled by the browser. When that happens, there
2885 // should no longer be a start/stop call imbalance. TODO(avi): When this
2886 // future arrives, update this code to not allow this case.
2887 if (rfh->frame_tree_node()->is_loading())
2888 return;
2889 2903
2890 DCHECK_GE(loading_frames_in_progress_, 0); 2904 if (!IsFrameTreeLoading())
2891 if (loading_frames_in_progress_ == 0)
2892 DidStartLoading(rfh, to_different_document); 2905 DidStartLoading(rfh, to_different_document);
2893 2906
2894 ++loading_frames_in_progress_; 2907 rfh->set_is_loading(true);
2895 rfh->frame_tree_node()->set_is_loading(true); 2908 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressMinimum);
2896 2909
2897 // Notify the RenderFrameHostManager of the event. 2910 // Notify the RenderFrameHostManager of the event.
2898 rfh->frame_tree_node()->render_manager()->OnDidStartLoading(); 2911 rfh->frame_tree_node()->render_manager()->OnDidStartLoading();
2899 2912
2900 loading_progresses_[render_frame_id] = kMinimumLoadingProgress;
2901 SendLoadProgressChanged(); 2913 SendLoadProgressChanged();
2902 } 2914 }
2903 2915
2904 void WebContentsImpl::OnDidStopLoading() { 2916 void WebContentsImpl::OnDidStopLoading() {
2905 if (!HasValidFrameSource()) 2917 if (!HasValidFrameSource())
2906 return; 2918 return;
2907 2919
2908 RenderFrameHostImpl* rfh = 2920 RenderFrameHostImpl* rfh =
2909 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2921 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2910 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2911 rfh->frame_tree_node()->set_is_loading(false);
2912 2922
2913 if (loading_progresses_.find(render_frame_id) != loading_progresses_.end()) { 2923 // This method should never be called when the frame is not loading.
2914 // Load stopped while we were still tracking load. Make sure we update 2924 DCHECK(rfh->is_loading());
2915 // progress based on this frame's completion. 2925 rfh->set_is_loading(false);
2916 loading_progresses_[render_frame_id] = 1.0; 2926 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressDone);
2917 SendLoadProgressChanged(); 2927
2918 // Then we clean-up our states. 2928 // Update progress based on this frame's completion.
2919 if (loading_total_progress_ == 1.0) 2929 SendLoadProgressChanged();
2920 ResetLoadProgressState(); 2930
2921 } 2931 // Then clean-up the states.
2932 if (loading_total_progress_ == 1.0)
2933 ResetLoadProgressState();
2922 2934
2923 // Notify the RenderFrameHostManager of the event. 2935 // Notify the RenderFrameHostManager of the event.
2924 rfh->frame_tree_node()->render_manager()->OnDidStopLoading(); 2936 rfh->frame_tree_node()->render_manager()->OnDidStopLoading();
2925 2937
2926 // TODO(japhet): This should be a DCHECK, but the pdf plugin sometimes 2938 if (!IsFrameTreeLoading())
2927 // calls DidStopLoading() without a matching DidStartLoading().
2928 if (loading_frames_in_progress_ == 0)
2929 return;
2930 --loading_frames_in_progress_;
2931 if (loading_frames_in_progress_ == 0)
2932 DidStopLoading(rfh); 2939 DidStopLoading(rfh);
2933 } 2940 }
2934 2941
2935 void WebContentsImpl::OnDidChangeLoadProgress(double load_progress) { 2942 void WebContentsImpl::OnDidChangeLoadProgress(double load_progress) {
2936 if (!HasValidFrameSource()) 2943 if (!HasValidFrameSource())
2937 return; 2944 return;
2938 2945
2939 RenderFrameHostImpl* rfh = 2946 RenderFrameHostImpl* rfh =
2940 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2947 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2941 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2942 2948
2943 loading_progresses_[render_frame_id] = load_progress; 2949 rfh->set_loading_progress(load_progress);
2944 2950
2945 // We notify progress change immediately for the first and last updates. 2951 // We notify progress change immediately for the first and last updates.
2946 // Also, since the message loop may be pretty busy when a page is loaded, it 2952 // Also, since the message loop may be pretty busy when a page is loaded, it
2947 // might not execute a posted task in a timely manner so we make sure to 2953 // might not execute a posted task in a timely manner so we make sure to
2948 // immediately send progress report if enough time has passed. 2954 // immediately send progress report if enough time has passed.
2949 base::TimeDelta min_delay = 2955 base::TimeDelta min_delay =
2950 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS); 2956 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
2951 if (load_progress == 1.0 || loading_last_progress_update_.is_null() || 2957 if (load_progress == 1.0 || loading_last_progress_update_.is_null() ||
2952 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay) { 2958 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay) {
2953 // If there is a pending task to send progress, it is now obsolete. 2959 // 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
3411 Details<std::pair<NavigationEntry*, bool> >(&details)); 3417 Details<std::pair<NavigationEntry*, bool> >(&details));
3412 3418
3413 return true; 3419 return true;
3414 } 3420 }
3415 3421
3416 void WebContentsImpl::SendLoadProgressChanged() { 3422 void WebContentsImpl::SendLoadProgressChanged() {
3417 loading_last_progress_update_ = base::TimeTicks::Now(); 3423 loading_last_progress_update_ = base::TimeTicks::Now();
3418 double progress = 0.0; 3424 double progress = 0.0;
3419 int frame_count = 0; 3425 int frame_count = 0;
3420 3426
3421 for (LoadingProgressMap::iterator it = loading_progresses_.begin(); 3427 frame_tree_.ForEach(
3422 it != loading_progresses_.end(); 3428 base::Bind(&CollectLoadProgress, &progress, &frame_count));
3423 ++it) { 3429 if (frame_count != 0)
3424 progress += it->second; 3430 progress /= frame_count;
3425 ++frame_count; 3431 DCHECK_LE(progress, 1.0);
3426 }
3427 if (frame_count == 0)
3428 return;
3429 progress /= frame_count;
3430 DCHECK(progress <= 1.0);
3431 3432
3432 if (progress <= loading_total_progress_) 3433 if (progress <= loading_total_progress_)
3433 return; 3434 return;
3434 loading_total_progress_ = progress; 3435 loading_total_progress_ = progress;
3435 3436
3436 if (delegate_) 3437 if (delegate_)
3437 delegate_->LoadProgressChanged(this, progress); 3438 delegate_->LoadProgressChanged(this, progress);
3438 } 3439 }
3439 3440
3440 void WebContentsImpl::ResetLoadProgressState() { 3441 void WebContentsImpl::ResetLoadProgressState() {
3441 loading_progresses_.clear(); 3442 frame_tree_.ForEach(base::Bind(&ResetLoadProgress));
3442 loading_total_progress_ = 0.0; 3443 loading_total_progress_ = 0.0;
3443 loading_weak_factory_.InvalidateWeakPtrs(); 3444 loading_weak_factory_.InvalidateWeakPtrs();
3444 loading_last_progress_update_ = base::TimeTicks(); 3445 loading_last_progress_update_ = base::TimeTicks();
3445 } 3446 }
3446 3447
3448 bool WebContentsImpl::IsFrameTreeLoading() {
clamy 2015/03/03 17:01:09 Suggestion: replace this private member function w
Fabrice (no longer in Chrome) 2015/03/04 18:17:58 Done.
3449 bool is_loading = false;
3450 frame_tree_.ForEach(base::Bind(&IsNodeLoading, &is_loading));
3451 return is_loading;
3452 }
3453
3447 void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host, 3454 void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
3448 RenderViewHost* new_host) { 3455 RenderViewHost* new_host) {
3449 // After sending out a swap notification, we need to send a disconnect 3456 // After sending out a swap notification, we need to send a disconnect
3450 // notification so that clients that pick up a pointer to |this| can NULL the 3457 // notification so that clients that pick up a pointer to |this| can NULL the
3451 // pointer. See Bug 1230284. 3458 // pointer. See Bug 1230284.
3452 notify_disconnection_ = true; 3459 notify_disconnection_ = true;
3453 FOR_EACH_OBSERVER(WebContentsObserver, observers_, 3460 FOR_EACH_OBSERVER(WebContentsObserver, observers_,
3454 RenderViewHostChanged(old_host, new_host)); 3461 RenderViewHostChanged(old_host, new_host));
3455 3462
3456 // TODO(avi): Remove. http://crbug.com/170921 3463 // TODO(avi): Remove. http://crbug.com/170921
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
3736 3743
3737 SetIsLoading(rvh, false, true, NULL); 3744 SetIsLoading(rvh, false, true, NULL);
3738 NotifyDisconnected(); 3745 NotifyDisconnected();
3739 SetIsCrashed(status, error_code); 3746 SetIsCrashed(status, error_code);
3740 3747
3741 // Reset the loading progress. TODO(avi): What does it mean to have a 3748 // Reset the loading progress. TODO(avi): What does it mean to have a
3742 // "renderer crash" when there is more than one renderer process serving a 3749 // "renderer crash" when there is more than one renderer process serving a
3743 // webpage? Once this function is called at a more granular frame level, we 3750 // webpage? Once this function is called at a more granular frame level, we
3744 // probably will need to more granularly reset the state here. 3751 // probably will need to more granularly reset the state here.
3745 ResetLoadProgressState(); 3752 ResetLoadProgressState();
3746 loading_frames_in_progress_ = 0;
3747 3753
3748 FOR_EACH_OBSERVER(WebContentsObserver, 3754 FOR_EACH_OBSERVER(WebContentsObserver,
3749 observers_, 3755 observers_,
3750 RenderProcessGone(GetCrashedStatus())); 3756 RenderProcessGone(GetCrashedStatus()));
3751 } 3757 }
3752 3758
3753 void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) { 3759 void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
3754 FOR_EACH_OBSERVER(WebContentsObserver, observers_, RenderViewDeleted(rvh)); 3760 FOR_EACH_OBSERVER(WebContentsObserver, observers_, RenderViewDeleted(rvh));
3755 } 3761 }
3756 3762
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after
4535 node->render_manager()->ResumeResponseDeferredAtStart(); 4541 node->render_manager()->ResumeResponseDeferredAtStart();
4536 } 4542 }
4537 4543
4538 void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) { 4544 void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
4539 force_disable_overscroll_content_ = force_disable; 4545 force_disable_overscroll_content_ = force_disable;
4540 if (view_) 4546 if (view_)
4541 view_->SetOverscrollControllerEnabled(CanOverscrollContent()); 4547 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
4542 } 4548 }
4543 4549
4544 } // namespace content 4550 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698