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

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: Comments consistency. 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 20 matching lines...) Expand all
159 } 154 }
160 155
161 // Helper function for retrieving all the sites in a frame tree. 156 // Helper function for retrieving all the sites in a frame tree.
162 bool CollectSites(BrowserContext* context, 157 bool CollectSites(BrowserContext* context,
163 std::set<GURL>* sites, 158 std::set<GURL>* sites,
164 FrameTreeNode* node) { 159 FrameTreeNode* node) {
165 sites->insert(SiteInstance::GetSiteForURL(context, node->current_url())); 160 sites->insert(SiteInstance::GetSiteForURL(context, node->current_url()));
166 return true; 161 return true;
167 } 162 }
168 163
164 // Helper function used with FrameTree::ForEach() for retrieving the total
165 // loading progress and number of frames in a frame tree.
166 bool CollectLoadProgress(double* progress,
167 int* frame_count,
168 FrameTreeNode* node) {
169 // Ignore the current frame if it has not started loading.
170 double frame_progress = node->GetLoadingProgress();
171 if (frame_progress == RenderFrameHostImpl::kLoadingProgressNotStarted)
172 return true;
173
174 // Collect progress.
175 *progress += node->GetLoadingProgress();
176 (*frame_count)++;
177 return true;
178 }
179
180 // Helper function used with FrameTree::ForEach() to check if at least one of
181 // the nodes is loading.
182 bool IsNodeLoading(bool* is_loading, FrameTreeNode* node) {
183 if (node->IsLoading()) {
184 // There is at least one node loading, so abort traversal.
185 *is_loading = true;
186 return false;
187 }
188 return true;
189 }
190
191 // Helper function used with FrameTree::ForEach() to reset the load progress.
192 bool ResetLoadProgress(FrameTreeNode* node) {
193 node->current_frame_host()->set_loading_progress(
194 RenderFrameHostImpl::kLoadingProgressNotStarted);
195 return true;
196 }
197
169 bool ForEachFrameInternal( 198 bool ForEachFrameInternal(
170 const base::Callback<void(RenderFrameHost*)>& on_frame, 199 const base::Callback<void(RenderFrameHost*)>& on_frame,
171 FrameTreeNode* node) { 200 FrameTreeNode* node) {
172 on_frame.Run(node->current_frame_host()); 201 on_frame.Run(node->current_frame_host());
173 return true; 202 return true;
174 } 203 }
175 204
176 bool ForEachPendingFrameInternal( 205 bool ForEachPendingFrameInternal(
177 const base::Callback<void(RenderFrameHost*)>& on_frame, 206 const base::Callback<void(RenderFrameHost*)>& on_frame,
178 FrameTreeNode* node) { 207 FrameTreeNode* node) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 // termination is not allowed for the current RenderFrameHost of 249 // termination is not allowed for the current RenderFrameHost of
221 // |frame_tree_node|. Used with FrameTree::ForEach. 250 // |frame_tree_node|. Used with FrameTree::ForEach.
222 bool SuddenTerminationAllowed(bool* sudden_termination_allowed, 251 bool SuddenTerminationAllowed(bool* sudden_termination_allowed,
223 FrameTreeNode* frame_tree_node) { 252 FrameTreeNode* frame_tree_node) {
224 if (frame_tree_node->current_frame_host()->SuddenTerminationAllowed()) 253 if (frame_tree_node->current_frame_host()->SuddenTerminationAllowed())
225 return true; 254 return true;
226 *sudden_termination_allowed = false; 255 *sudden_termination_allowed = false;
227 return false; 256 return false;
228 } 257 }
229 258
259 // Returns true if at least one of the nodes in the |frame_tree| is loading.
260 bool IsFrameTreeLoading(FrameTree& frame_tree) {
261 bool is_loading = false;
262 frame_tree.ForEach(base::Bind(&IsNodeLoading, &is_loading));
263 return is_loading;
264 }
265
230 } // namespace 266 } // namespace
231 267
232 WebContents* WebContents::Create(const WebContents::CreateParams& params) { 268 WebContents* WebContents::Create(const WebContents::CreateParams& params) {
233 return WebContentsImpl::CreateWithOpener( 269 return WebContentsImpl::CreateWithOpener(
234 params, static_cast<WebContentsImpl*>(params.opener)); 270 params, static_cast<WebContentsImpl*>(params.opener));
235 } 271 }
236 272
237 WebContents* WebContents::CreateWithSessionStorage( 273 WebContents* WebContents::CreateWithSessionStorage(
238 const WebContents::CreateParams& params, 274 const WebContents::CreateParams& params,
239 const SessionStorageNamespaceMap& session_storage_namespace_map) { 275 const SessionStorageNamespaceMap& session_storage_namespace_map) {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
329 this, 365 this,
330 this, 366 this,
331 this), 367 this),
332 is_loading_(false), 368 is_loading_(false),
333 is_load_to_different_document_(false), 369 is_load_to_different_document_(false),
334 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING), 370 crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
335 crashed_error_code_(0), 371 crashed_error_code_(0),
336 waiting_for_response_(false), 372 waiting_for_response_(false),
337 load_state_(net::LOAD_STATE_IDLE, base::string16()), 373 load_state_(net::LOAD_STATE_IDLE, base::string16()),
338 loading_total_progress_(0.0), 374 loading_total_progress_(0.0),
339 loading_frames_in_progress_(0),
340 upload_size_(0), 375 upload_size_(0),
341 upload_position_(0), 376 upload_position_(0),
342 displayed_insecure_content_(false), 377 displayed_insecure_content_(false),
343 has_accessed_initial_document_(false), 378 has_accessed_initial_document_(false),
344 capturer_count_(0), 379 capturer_count_(0),
345 should_normally_be_visible_(true), 380 should_normally_be_visible_(true),
346 is_being_destroyed_(false), 381 is_being_destroyed_(false),
347 notify_disconnection_(false), 382 notify_disconnection_(false),
348 dialog_manager_(NULL), 383 dialog_manager_(NULL),
349 is_showing_before_unload_dialog_(false), 384 is_showing_before_unload_dialog_(false),
(...skipping 2507 matching lines...) Expand 10 before | Expand all | Expand 10 after
2857 FOR_EACH_OBSERVER( 2892 FOR_EACH_OBSERVER(
2858 WebContentsObserver, observers_, DidFinishLoad(rfh, validated_url)); 2893 WebContentsObserver, observers_, DidFinishLoad(rfh, validated_url));
2859 } 2894 }
2860 2895
2861 void WebContentsImpl::OnDidStartLoading(bool to_different_document) { 2896 void WebContentsImpl::OnDidStartLoading(bool to_different_document) {
2862 if (!HasValidFrameSource()) 2897 if (!HasValidFrameSource())
2863 return; 2898 return;
2864 2899
2865 RenderFrameHostImpl* rfh = 2900 RenderFrameHostImpl* rfh =
2866 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2901 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2867 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2868 2902
2869 // Any main frame load to a new document should reset the load progress, since 2903 // Any main frame load to a new document should reset the load progress, since
2870 // it will replace the current page and any frames. 2904 // it will replace the current page and any frames.
2871 if (to_different_document && !rfh->GetParent()) { 2905 if (to_different_document && !rfh->GetParent()) {
2872 ResetLoadProgressState(); 2906 ResetLoadProgressState();
2873 loading_frames_in_progress_ = 0; 2907 rfh->set_is_loading(false);
2874 rfh->frame_tree_node()->set_is_loading(false);
2875 } 2908 }
2876 2909
2877 // It is possible to get multiple calls to OnDidStartLoading that don't have 2910 // This method should never be called when the frame is loading.
2878 // corresponding calls to OnDidStopLoading: 2911 DCHECK(!rfh->is_loading());
2879 // - With "swappedout://" URLs, this happens when a RenderView gets swapped
2880 // out for a cross-process navigation, and it turns into a placeholder for
2881 // one being rendered in a different process.
2882 // - Also, there might be more than one RenderFrameHost sharing the same
2883 // FrameTreeNode (and thus sharing its ID) each sending a start.
2884 // - But in the future, once clamy@ moves navigation network requests to the
2885 // browser process, there's a good chance that callbacks about starting and
2886 // stopping will all be handled by the browser. When that happens, there
2887 // should no longer be a start/stop call imbalance. TODO(avi): When this
2888 // future arrives, update this code to not allow this case.
2889 if (rfh->frame_tree_node()->is_loading())
2890 return;
2891 2912
2892 DCHECK_GE(loading_frames_in_progress_, 0); 2913 if (!IsFrameTreeLoading(frame_tree_))
2893 if (loading_frames_in_progress_ == 0)
2894 DidStartLoading(rfh, to_different_document); 2914 DidStartLoading(rfh, to_different_document);
2895 2915
2896 ++loading_frames_in_progress_; 2916 rfh->set_is_loading(true);
2897 rfh->frame_tree_node()->set_is_loading(true); 2917 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressMinimum);
2898 2918
2899 // Notify the RenderFrameHostManager of the event. 2919 // Notify the RenderFrameHostManager of the event.
2900 rfh->frame_tree_node()->render_manager()->OnDidStartLoading(); 2920 rfh->frame_tree_node()->render_manager()->OnDidStartLoading();
2901 2921
2902 loading_progresses_[render_frame_id] = kMinimumLoadingProgress;
2903 SendLoadProgressChanged(); 2922 SendLoadProgressChanged();
2904 } 2923 }
2905 2924
2906 void WebContentsImpl::OnDidStopLoading() { 2925 void WebContentsImpl::OnDidStopLoading() {
2907 if (!HasValidFrameSource()) 2926 if (!HasValidFrameSource())
2908 return; 2927 return;
2909 2928
2910 RenderFrameHostImpl* rfh = 2929 RenderFrameHostImpl* rfh =
2911 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2930 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2912 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2913 rfh->frame_tree_node()->set_is_loading(false);
2914 2931
2915 if (loading_progresses_.find(render_frame_id) != loading_progresses_.end()) { 2932 // This method should never be called when the frame is not loading.
2916 // Load stopped while we were still tracking load. Make sure we update 2933 DCHECK(rfh->is_loading());
2917 // progress based on this frame's completion. 2934 rfh->set_is_loading(false);
2918 loading_progresses_[render_frame_id] = 1.0; 2935 rfh->set_loading_progress(RenderFrameHostImpl::kLoadingProgressDone);
2919 SendLoadProgressChanged(); 2936
2920 // Then we clean-up our states. 2937 // Update progress based on this frame's completion.
2921 if (loading_total_progress_ == 1.0) 2938 SendLoadProgressChanged();
2922 ResetLoadProgressState(); 2939
2923 } 2940 // Then clean-up the states.
2941 if (loading_total_progress_ == 1.0)
2942 ResetLoadProgressState();
2924 2943
2925 // Notify the RenderFrameHostManager of the event. 2944 // Notify the RenderFrameHostManager of the event.
2926 rfh->frame_tree_node()->render_manager()->OnDidStopLoading(); 2945 rfh->frame_tree_node()->render_manager()->OnDidStopLoading();
2927 2946
2928 // TODO(japhet): This should be a DCHECK, but the pdf plugin sometimes 2947 if (!IsFrameTreeLoading(frame_tree_))
2929 // calls DidStopLoading() without a matching DidStartLoading().
2930 if (loading_frames_in_progress_ == 0)
2931 return;
2932 --loading_frames_in_progress_;
2933 if (loading_frames_in_progress_ == 0)
2934 DidStopLoading(rfh); 2948 DidStopLoading(rfh);
2935 } 2949 }
2936 2950
2937 void WebContentsImpl::OnDidChangeLoadProgress(double load_progress) { 2951 void WebContentsImpl::OnDidChangeLoadProgress(double load_progress) {
2938 if (!HasValidFrameSource()) 2952 if (!HasValidFrameSource())
2939 return; 2953 return;
2940 2954
2941 RenderFrameHostImpl* rfh = 2955 RenderFrameHostImpl* rfh =
2942 static_cast<RenderFrameHostImpl*>(render_frame_message_source_); 2956 static_cast<RenderFrameHostImpl*>(render_frame_message_source_);
2943 int64 render_frame_id = rfh->frame_tree_node()->frame_tree_node_id();
2944 2957
2945 loading_progresses_[render_frame_id] = load_progress; 2958 rfh->set_loading_progress(load_progress);
2946 2959
2947 // We notify progress change immediately for the first and last updates. 2960 // We notify progress change immediately for the first and last updates.
2948 // Also, since the message loop may be pretty busy when a page is loaded, it 2961 // Also, since the message loop may be pretty busy when a page is loaded, it
2949 // might not execute a posted task in a timely manner so we make sure to 2962 // might not execute a posted task in a timely manner so we make sure to
2950 // immediately send progress report if enough time has passed. 2963 // immediately send progress report if enough time has passed.
2951 base::TimeDelta min_delay = 2964 base::TimeDelta min_delay =
2952 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS); 2965 base::TimeDelta::FromMilliseconds(kMinimumDelayBetweenLoadingUpdatesMS);
2953 if (load_progress == 1.0 || loading_last_progress_update_.is_null() || 2966 if (load_progress == 1.0 || loading_last_progress_update_.is_null() ||
2954 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay) { 2967 base::TimeTicks::Now() - loading_last_progress_update_ > min_delay) {
2955 // If there is a pending task to send progress, it is now obsolete. 2968 // 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
3413 Details<std::pair<NavigationEntry*, bool> >(&details)); 3426 Details<std::pair<NavigationEntry*, bool> >(&details));
3414 3427
3415 return true; 3428 return true;
3416 } 3429 }
3417 3430
3418 void WebContentsImpl::SendLoadProgressChanged() { 3431 void WebContentsImpl::SendLoadProgressChanged() {
3419 loading_last_progress_update_ = base::TimeTicks::Now(); 3432 loading_last_progress_update_ = base::TimeTicks::Now();
3420 double progress = 0.0; 3433 double progress = 0.0;
3421 int frame_count = 0; 3434 int frame_count = 0;
3422 3435
3423 for (LoadingProgressMap::iterator it = loading_progresses_.begin(); 3436 frame_tree_.ForEach(
3424 it != loading_progresses_.end(); 3437 base::Bind(&CollectLoadProgress, &progress, &frame_count));
3425 ++it) { 3438 if (frame_count != 0)
3426 progress += it->second; 3439 progress /= frame_count;
3427 ++frame_count; 3440 DCHECK_LE(progress, 1.0);
3428 }
3429 if (frame_count == 0)
3430 return;
3431 progress /= frame_count;
3432 DCHECK(progress <= 1.0);
3433 3441
3434 if (progress <= loading_total_progress_) 3442 if (progress <= loading_total_progress_)
3435 return; 3443 return;
3436 loading_total_progress_ = progress; 3444 loading_total_progress_ = progress;
3437 3445
3438 if (delegate_) 3446 if (delegate_)
3439 delegate_->LoadProgressChanged(this, progress); 3447 delegate_->LoadProgressChanged(this, progress);
3440 } 3448 }
3441 3449
3442 void WebContentsImpl::ResetLoadProgressState() { 3450 void WebContentsImpl::ResetLoadProgressState() {
3443 loading_progresses_.clear(); 3451 frame_tree_.ForEach(base::Bind(&ResetLoadProgress));
3444 loading_total_progress_ = 0.0; 3452 loading_total_progress_ = 0.0;
3445 loading_weak_factory_.InvalidateWeakPtrs(); 3453 loading_weak_factory_.InvalidateWeakPtrs();
3446 loading_last_progress_update_ = base::TimeTicks(); 3454 loading_last_progress_update_ = base::TimeTicks();
3447 } 3455 }
3448 3456
3449 void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host, 3457 void WebContentsImpl::NotifyViewSwapped(RenderViewHost* old_host,
3450 RenderViewHost* new_host) { 3458 RenderViewHost* new_host) {
3451 // After sending out a swap notification, we need to send a disconnect 3459 // After sending out a swap notification, we need to send a disconnect
3452 // notification so that clients that pick up a pointer to |this| can NULL the 3460 // notification so that clients that pick up a pointer to |this| can NULL the
3453 // pointer. See Bug 1230284. 3461 // pointer. See Bug 1230284.
(...skipping 284 matching lines...) Expand 10 before | Expand all | Expand 10 after
3738 3746
3739 SetIsLoading(rvh, false, true, NULL); 3747 SetIsLoading(rvh, false, true, NULL);
3740 NotifyDisconnected(); 3748 NotifyDisconnected();
3741 SetIsCrashed(status, error_code); 3749 SetIsCrashed(status, error_code);
3742 3750
3743 // Reset the loading progress. TODO(avi): What does it mean to have a 3751 // Reset the loading progress. TODO(avi): What does it mean to have a
3744 // "renderer crash" when there is more than one renderer process serving a 3752 // "renderer crash" when there is more than one renderer process serving a
3745 // webpage? Once this function is called at a more granular frame level, we 3753 // webpage? Once this function is called at a more granular frame level, we
3746 // probably will need to more granularly reset the state here. 3754 // probably will need to more granularly reset the state here.
3747 ResetLoadProgressState(); 3755 ResetLoadProgressState();
3748 loading_frames_in_progress_ = 0;
3749 3756
3750 FOR_EACH_OBSERVER(WebContentsObserver, 3757 FOR_EACH_OBSERVER(WebContentsObserver,
3751 observers_, 3758 observers_,
3752 RenderProcessGone(GetCrashedStatus())); 3759 RenderProcessGone(GetCrashedStatus()));
3753 } 3760 }
3754 3761
3755 void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) { 3762 void WebContentsImpl::RenderViewDeleted(RenderViewHost* rvh) {
3756 FOR_EACH_OBSERVER(WebContentsObserver, observers_, RenderViewDeleted(rvh)); 3763 FOR_EACH_OBSERVER(WebContentsObserver, observers_, RenderViewDeleted(rvh));
3757 } 3764 }
3758 3765
(...skipping 778 matching lines...) Expand 10 before | Expand all | Expand 10 after
4537 node->render_manager()->ResumeResponseDeferredAtStart(); 4544 node->render_manager()->ResumeResponseDeferredAtStart();
4538 } 4545 }
4539 4546
4540 void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) { 4547 void WebContentsImpl::SetForceDisableOverscrollContent(bool force_disable) {
4541 force_disable_overscroll_content_ = force_disable; 4548 force_disable_overscroll_content_ = force_disable;
4542 if (view_) 4549 if (view_)
4543 view_->SetOverscrollControllerEnabled(CanOverscrollContent()); 4550 view_->SetOverscrollControllerEnabled(CanOverscrollContent());
4544 } 4551 }
4545 4552
4546 } // namespace content 4553 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698