| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "blimp/engine/session/page_load_tracker.h" | 5 #include "blimp/engine/session/page_load_tracker.h" |
| 6 | 6 |
| 7 #include "content/public/browser/navigation_handle.h" |
| 7 #include "content/public/browser/render_widget_host_view.h" | 8 #include "content/public/browser/render_widget_host_view.h" |
| 8 | 9 |
| 9 namespace blimp { | 10 namespace blimp { |
| 10 namespace engine { | 11 namespace engine { |
| 11 | 12 |
| 12 namespace { | 13 namespace { |
| 13 | 14 |
| 14 content::RenderWidgetHost* GetRenderWidgetHostIfMainFrame( | 15 bool ShouldIgnoreNavigation(content::NavigationHandle* navigation_handle) { |
| 15 content::RenderFrameHost* render_frame_host) { | 16 // We change the progress bar for main frame navigations only. |
| 16 if (render_frame_host->GetParent() != nullptr) | 17 if (!navigation_handle->IsInMainFrame()) |
| 17 return nullptr; | 18 return true; |
| 18 | 19 |
| 19 return render_frame_host->GetView()->GetRenderWidgetHost(); | 20 // Same page navigations don't need to trigger a progress bar update. |
| 21 if (navigation_handle->IsSamePage()) |
| 22 return true; |
| 23 |
| 24 return false; |
| 20 } | 25 } |
| 21 | 26 |
| 22 } // namespace | 27 } // namespace |
| 23 | 28 |
| 24 PageLoadTracker::PageLoadTracker(content::WebContents* web_contents, | 29 PageLoadTracker::PageLoadTracker(content::WebContents* web_contents, |
| 25 PageLoadTrackerClient* client) | 30 PageLoadTrackerClient* client) |
| 26 : client_(client) { | 31 : client_(client) { |
| 27 DCHECK(web_contents); | 32 DCHECK(web_contents); |
| 28 Observe(web_contents); | 33 Observe(web_contents); |
| 29 } | 34 } |
| 30 | 35 |
| 31 PageLoadTracker::~PageLoadTracker() {} | 36 PageLoadTracker::~PageLoadTracker() {} |
| 32 | 37 |
| 33 void PageLoadTracker::DidStartProvisionalLoadForFrame( | 38 void PageLoadTracker::DidStartNavigation( |
| 34 content::RenderFrameHost* render_frame_host, | 39 content::NavigationHandle* navigation_handle) { |
| 35 const GURL& validated_url, | 40 if (ShouldIgnoreNavigation(navigation_handle)) |
| 36 bool is_error_page, | |
| 37 bool is_iframe_srcdoc) { | |
| 38 content::RenderWidgetHost* render_widget_host = | |
| 39 GetRenderWidgetHostIfMainFrame(render_frame_host); | |
| 40 if (!render_widget_host) | |
| 41 return; | 41 return; |
| 42 | 42 |
| 43 render_widget_load_status_[render_widget_host] = LoadStatus(); | 43 // Cancel any pending callbacks for the previous navigation. We will send an |
| 44 | 44 // update based on the progress of this navigation. |
| 45 // Notify the client that a navigation was initiated. | 45 did_paint_after_navigation_callback_.Cancel(); |
| 46 client_->SendPageLoadStatusUpdate(PageLoadStatus::LOADING); | 46 client_->SendPageLoadStatusUpdate(PageLoadStatus::LOADING); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void PageLoadTracker::DidFinishLoad(content::RenderFrameHost* render_frame_host, | 49 void PageLoadTracker::DidFinishNavigation( |
| 50 const GURL& validated_url) { | 50 content::NavigationHandle* navigation_handle) { |
| 51 content::RenderWidgetHost* render_widget_host = | 51 if (ShouldIgnoreNavigation(navigation_handle)) |
| 52 GetRenderWidgetHostIfMainFrame(render_frame_host); | |
| 53 if (!render_widget_host) | |
| 54 return; | 52 return; |
| 55 | 53 |
| 56 RenderWidgetLoadStatusMap::iterator it = | 54 if (navigation_handle->HasCommitted()) { |
| 57 render_widget_load_status_.find(render_widget_host); | 55 // Make sure that at least one compositor content update after the |
| 58 DCHECK(it != render_widget_load_status_.end()); | 56 // navigation commits is sent to the client. |
| 59 | 57 // Note that a visual state update in our case implies that this callback |
| 60 it->second.page_loaded = true; | 58 // will be invoked after the update is queued to be sent to the client. |
| 61 if (it->second.Loaded()) { | 59 did_paint_after_navigation_callback_.Reset( |
| 60 base::Bind(&PageLoadTracker::DidPaintAfterNavigationCommitted, |
| 61 base::Unretained(this))); |
| 62 navigation_handle->GetRenderFrameHost()->InsertVisualStateCallback( |
| 63 did_paint_after_navigation_callback_.callback()); |
| 64 } else { |
| 65 // Inform the client to update the progress bar right away. |
| 62 client_->SendPageLoadStatusUpdate(PageLoadStatus::LOADED); | 66 client_->SendPageLoadStatusUpdate(PageLoadStatus::LOADED); |
| 63 render_widget_load_status_.erase(it); | |
| 64 } | 67 } |
| 65 } | 68 } |
| 66 | 69 |
| 67 void PageLoadTracker::DidFailLoad(content::RenderFrameHost* render_frame_host, | 70 void PageLoadTracker::DidPaintAfterNavigationCommitted(bool result) { |
| 68 const GURL& validated_url, | |
| 69 int error_code, | |
| 70 const base::string16& error_description, | |
| 71 bool was_ignored_by_handler) { | |
| 72 content::RenderWidgetHost* render_widget_host = | |
| 73 GetRenderWidgetHostIfMainFrame(render_frame_host); | |
| 74 if (!render_widget_host) | |
| 75 return; | |
| 76 | |
| 77 RenderWidgetLoadStatusMap::iterator it = | |
| 78 render_widget_load_status_.find(render_widget_host); | |
| 79 DCHECK(it != render_widget_load_status_.end()); | |
| 80 | |
| 81 // If the navigation failed, the client should dismiss the load indicator. | |
| 82 client_->SendPageLoadStatusUpdate(PageLoadStatus::LOADED); | 71 client_->SendPageLoadStatusUpdate(PageLoadStatus::LOADED); |
| 83 render_widget_load_status_.erase(it); | |
| 84 } | |
| 85 | |
| 86 void PageLoadTracker::DidFirstPaintAfterLoad( | |
| 87 content::RenderWidgetHost* render_widget_host) { | |
| 88 RenderWidgetLoadStatusMap::iterator it = | |
| 89 render_widget_load_status_.find(render_widget_host); | |
| 90 DCHECK(it != render_widget_load_status_.end()); | |
| 91 | |
| 92 it->second.did_first_paint = true; | |
| 93 if (it->second.Loaded()) { | |
| 94 client_->SendPageLoadStatusUpdate(PageLoadStatus::LOADED); | |
| 95 render_widget_load_status_.erase(it); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 bool PageLoadTracker::LoadStatus::Loaded() const { | |
| 100 return page_loaded && did_first_paint; | |
| 101 } | 72 } |
| 102 | 73 |
| 103 } // namespace engine | 74 } // namespace engine |
| 104 } // namespace blimp | 75 } // namespace blimp |
| OLD | NEW |