OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/dom_distiller/tab_utils.h" | 5 #include "chrome/browser/dom_distiller/tab_utils.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" | 8 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" |
9 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" | 9 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" |
10 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" | 10 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" |
11 #include "components/dom_distiller/content/distiller_page_web_contents.h" | 11 #include "components/dom_distiller/content/distiller_page_web_contents.h" |
12 #include "components/dom_distiller/core/distiller_page.h" | 12 #include "components/dom_distiller/core/distiller_page.h" |
13 #include "components/dom_distiller/core/dom_distiller_service.h" | 13 #include "components/dom_distiller/core/dom_distiller_service.h" |
14 #include "components/dom_distiller/core/task_tracker.h" | 14 #include "components/dom_distiller/core/task_tracker.h" |
15 #include "components/dom_distiller/core/url_constants.h" | 15 #include "components/dom_distiller/core/url_constants.h" |
16 #include "components/dom_distiller/core/url_utils.h" | 16 #include "components/dom_distiller/core/url_utils.h" |
17 #include "content/public/browser/browser_context.h" | 17 #include "content/public/browser/browser_context.h" |
18 #include "content/public/browser/navigation_controller.h" | 18 #include "content/public/browser/navigation_controller.h" |
19 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
20 #include "content/public/browser/web_contents_observer.h" | 20 #include "content/public/browser/web_contents_observer.h" |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 using dom_distiller::ViewRequestDelegate; | 24 using dom_distiller::ViewRequestDelegate; |
25 using dom_distiller::DistilledArticleProto; | 25 using dom_distiller::DistilledArticleProto; |
26 using dom_distiller::ArticleDistillationUpdate; | 26 using dom_distiller::ArticleDistillationUpdate; |
27 using dom_distiller::ViewerHandle; | 27 using dom_distiller::ViewerHandle; |
28 using dom_distiller::SourcePageHandleWebContents; | 28 using dom_distiller::SourcePageHandleWebContentsWeakPtr; |
29 using dom_distiller::DomDistillerService; | 29 using dom_distiller::DomDistillerService; |
30 using dom_distiller::DomDistillerServiceFactory; | 30 using dom_distiller::DomDistillerServiceFactory; |
31 using dom_distiller::DistillerPage; | 31 using dom_distiller::DistillerPage; |
32 using dom_distiller::SourcePageHandle; | |
33 | 32 |
34 // An no-op ViewRequestDelegate which holds a ViewerHandle and deletes itself | 33 // An no-op ViewRequestDelegate which holds a ViewerHandle and deletes itself |
35 // after the WebContents navigates or goes away. This class is a band-aid to | 34 // after the WebContents navigates or goes away. This class is a band-aid to |
36 // keep a TaskTracker around until the distillation starts from the viewer. | 35 // keep a TaskTracker around until the distillation starts from the viewer. |
37 class SelfDeletingRequestDelegate : public ViewRequestDelegate, | 36 class SelfDeletingRequestDelegate : public ViewRequestDelegate, |
38 public content::WebContentsObserver { | 37 public content::WebContentsObserver { |
39 public: | 38 public: |
40 explicit SelfDeletingRequestDelegate(content::WebContents* web_contents); | 39 explicit SelfDeletingRequestDelegate(content::WebContents* web_contents); |
41 ~SelfDeletingRequestDelegate() override; | 40 ~SelfDeletingRequestDelegate() override; |
42 | 41 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 // Start loading the viewer URL of the current page in |web_contents|. | 102 // Start loading the viewer URL of the current page in |web_contents|. |
104 void StartNavigationToDistillerViewer(content::WebContents* web_contents, | 103 void StartNavigationToDistillerViewer(content::WebContents* web_contents, |
105 const GURL& url) { | 104 const GURL& url) { |
106 GURL viewer_url = dom_distiller::url_utils::GetDistillerViewUrlFromUrl( | 105 GURL viewer_url = dom_distiller::url_utils::GetDistillerViewUrlFromUrl( |
107 dom_distiller::kDomDistillerScheme, url); | 106 dom_distiller::kDomDistillerScheme, url); |
108 content::NavigationController::LoadURLParams params(viewer_url); | 107 content::NavigationController::LoadURLParams params(viewer_url); |
109 params.transition_type = ui::PAGE_TRANSITION_AUTO_BOOKMARK; | 108 params.transition_type = ui::PAGE_TRANSITION_AUTO_BOOKMARK; |
110 web_contents->GetController().LoadURLWithParams(params); | 109 web_contents->GetController().LoadURLWithParams(params); |
111 } | 110 } |
112 | 111 |
| 112 } // namespace |
| 113 |
113 void MaybeStartDistillation(content::WebContents* web_contents) { | 114 void MaybeStartDistillation(content::WebContents* web_contents) { |
114 const GURL& last_committed_url = web_contents->GetLastCommittedURL(); | 115 const GURL& last_committed_url = web_contents->GetLastCommittedURL(); |
115 if (!dom_distiller::url_utils::IsUrlDistillable(last_committed_url)) { | 116 if (!dom_distiller::url_utils::IsUrlDistillable(last_committed_url)) |
116 delete web_contents; | |
117 return; | 117 return; |
118 } | |
119 | 118 |
120 // Start distillation using |web_contents|, and ensure ViewerHandle stays | 119 // Start distillation using |web_contents|, and ensure ViewerHandle stays |
121 // around until the viewer requests distillation. | 120 // around until the viewer requests distillation. |
122 SelfDeletingRequestDelegate* view_request_delegate = | 121 SelfDeletingRequestDelegate* view_request_delegate = |
123 new SelfDeletingRequestDelegate(web_contents); | 122 new SelfDeletingRequestDelegate(web_contents); |
124 scoped_ptr<content::WebContents> old_web_contents_sptr(web_contents); | 123 scoped_ptr<SourcePageHandleWebContentsWeakPtr> source_page_handle( |
125 scoped_ptr<SourcePageHandleWebContents> source_page_handle( | 124 new SourcePageHandleWebContentsWeakPtr(web_contents)); |
126 new SourcePageHandleWebContents(old_web_contents_sptr.Pass())); | |
127 DomDistillerService* dom_distiller_service = | 125 DomDistillerService* dom_distiller_service = |
128 DomDistillerServiceFactory::GetForBrowserContext( | 126 DomDistillerServiceFactory::GetForBrowserContext( |
129 web_contents->GetBrowserContext()); | 127 web_contents->GetBrowserContext()); |
130 scoped_ptr<DistillerPage> distiller_page = | 128 scoped_ptr<DistillerPage> distiller_page = |
131 dom_distiller_service->CreateDefaultDistillerPageWithHandle( | 129 dom_distiller_service->CreateDefaultDistillerPageWithHandle( |
132 source_page_handle.Pass()).Pass(); | 130 source_page_handle.Pass()).Pass(); |
133 | 131 |
134 scoped_ptr<ViewerHandle> viewer_handle = dom_distiller_service->ViewUrl( | 132 scoped_ptr<ViewerHandle> viewer_handle = dom_distiller_service->ViewUrl( |
135 view_request_delegate, distiller_page.Pass(), last_committed_url); | 133 view_request_delegate, distiller_page.Pass(), last_committed_url); |
136 view_request_delegate->TakeViewerHandle(viewer_handle.Pass()); | 134 view_request_delegate->TakeViewerHandle(viewer_handle.Pass()); |
137 } | 135 } |
138 | 136 |
139 } // namespace | 137 void DistillCurrentPageAndView(content::WebContents* web_contents) { |
140 | 138 DCHECK(web_contents); |
141 void DistillCurrentPageAndView(content::WebContents* old_web_contents) { | |
142 DCHECK(old_web_contents); | |
143 // Create new WebContents. | 139 // Create new WebContents. |
144 content::WebContents::CreateParams create_params( | 140 content::WebContents::CreateParams create_params( |
145 old_web_contents->GetBrowserContext()); | 141 web_contents->GetBrowserContext()); |
146 content::WebContents* new_web_contents = | |
147 content::WebContents::Create(create_params); | |
148 DCHECK(new_web_contents); | |
149 | 142 |
150 // Copy all navigation state from the old WebContents to the new one. | 143 MaybeStartDistillation(web_contents); |
151 new_web_contents->GetController().CopyStateFrom( | |
152 old_web_contents->GetController()); | |
153 | 144 |
154 // StartNavigationToDistillerViewer must come before swapping the tab contents | 145 StartNavigationToDistillerViewer(web_contents, |
155 // to avoid triggering a reload of the page. This reloadmakes it very | 146 web_contents->GetLastCommittedURL()); |
156 // difficult to distinguish between the intermediate reload and a user hitting | |
157 // the back button. | |
158 StartNavigationToDistillerViewer(new_web_contents, | |
159 old_web_contents->GetLastCommittedURL()); | |
160 | |
161 CoreTabHelper::FromWebContents(old_web_contents)->delegate()->SwapTabContents( | |
162 old_web_contents, new_web_contents, false, false); | |
163 | |
164 MaybeStartDistillation(old_web_contents); | |
165 } | 147 } |
OLD | NEW |