OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/dom_distiller/tab_utils.h" | |
6 | |
7 #include "base/message_loop/message_loop.h" | |
8 #include "chrome/browser/dom_distiller/dom_distiller_service_factory.h" | |
9 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" | |
10 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" | |
11 #include "chrome/common/url_constants.h" | |
12 #include "components/dom_distiller/content/distiller_page_web_contents.h" | |
13 #include "components/dom_distiller/core/distiller_page.h" | |
14 #include "components/dom_distiller/core/dom_distiller_service.h" | |
15 #include "components/dom_distiller/core/task_tracker.h" | |
16 #include "components/dom_distiller/core/url_utils.h" | |
17 #include "content/public/browser/browser_context.h" | |
18 #include "content/public/browser/navigation_controller.h" | |
19 #include "content/public/browser/web_contents.h" | |
20 #include "content/public/browser/web_contents_observer.h" | |
21 | |
22 namespace { | |
23 | |
24 using dom_distiller::ViewRequestDelegate; | |
25 using dom_distiller::DistilledArticleProto; | |
26 using dom_distiller::ArticleDistillationUpdate; | |
27 using dom_distiller::ViewerHandle; | |
28 using dom_distiller::SourcePageHandleWebContents; | |
29 using dom_distiller::DomDistillerService; | |
30 using dom_distiller::DomDistillerServiceFactory; | |
31 using dom_distiller::DistillerPage; | |
32 using dom_distiller::SourcePageHandle; | |
33 | |
34 // 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 | |
36 // keep a TaskTracker around until the distillation starts from the viewer. | |
37 class SelfDeletingRequestDelegate : public ViewRequestDelegate, | |
38 public content::WebContentsObserver { | |
39 public: | |
40 explicit SelfDeletingRequestDelegate(content::WebContents* web_contents); | |
41 virtual ~SelfDeletingRequestDelegate(); | |
42 | |
43 // ViewRequestDelegate implementation. | |
44 virtual void OnArticleReady( | |
45 const DistilledArticleProto* article_proto) OVERRIDE; | |
46 virtual void OnArticleUpdated( | |
47 ArticleDistillationUpdate article_update) OVERRIDE; | |
48 | |
49 // content::WebContentsObserver implementation. | |
50 virtual void DidNavigateMainFrame( | |
51 const content::LoadCommittedDetails& details, | |
52 const content::FrameNavigateParams& params) OVERRIDE; | |
53 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | |
54 virtual void WebContentsDestroyed() OVERRIDE; | |
55 | |
56 // Takes ownership of the ViewerHandle to keep distillation alive until |this| | |
57 // is deleted. | |
58 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | |
59 | |
60 private: | |
61 // The handle to the view request towards the DomDistillerService. It | |
62 // needs to be kept around to ensure the distillation request finishes. | |
63 scoped_ptr<ViewerHandle> viewer_handle_; | |
64 | |
65 // The WebContents this class is tracking. | |
66 content::WebContents* web_contents_; | |
67 }; | |
68 | |
69 void SelfDeletingRequestDelegate::DidNavigateMainFrame( | |
70 const content::LoadCommittedDetails& details, | |
71 const content::FrameNavigateParams& params) { | |
72 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
73 } | |
74 | |
75 void SelfDeletingRequestDelegate::RenderProcessGone( | |
76 base::TerminationStatus status) { | |
77 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
78 } | |
79 | |
80 void SelfDeletingRequestDelegate::WebContentsDestroyed() { | |
81 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
82 } | |
83 | |
84 SelfDeletingRequestDelegate::SelfDeletingRequestDelegate( | |
85 content::WebContents* web_contents) | |
86 : web_contents_(web_contents) { | |
87 content::WebContentsObserver::Observe(web_contents_); | |
88 } | |
89 | |
90 SelfDeletingRequestDelegate::~SelfDeletingRequestDelegate() { | |
91 content::WebContentsObserver::Observe(NULL); | |
92 } | |
93 | |
94 void SelfDeletingRequestDelegate::OnArticleReady( | |
95 const DistilledArticleProto* article_proto) { | |
96 } | |
97 | |
98 void SelfDeletingRequestDelegate::OnArticleUpdated( | |
99 ArticleDistillationUpdate article_update) { | |
100 } | |
101 | |
102 void SelfDeletingRequestDelegate::TakeViewerHandle( | |
103 scoped_ptr<ViewerHandle> viewer_handle) { | |
104 viewer_handle_ = viewer_handle.Pass(); | |
105 } | |
106 | |
107 // Start loading the viewer URL of the current page in |web_contents|. | |
108 void StartNavigationToDistillerViewer(content::WebContents* web_contents, | |
109 const GURL& url) { | |
110 GURL viewer_url = dom_distiller::url_utils::GetDistillerViewUrlFromUrl( | |
111 chrome::kDomDistillerScheme, url); | |
112 content::NavigationController::LoadURLParams params(viewer_url); | |
113 params.transition_type = content::PAGE_TRANSITION_AUTO_BOOKMARK; | |
114 web_contents->GetController().LoadURLWithParams(params); | |
115 } | |
116 | |
117 void StartDistillation(content::WebContents* web_contents) { | |
118 // Start distillation using |web_contents|, and ensure ViewerHandle stays | |
119 // around until the viewer requests distillation. | |
120 SelfDeletingRequestDelegate* view_request_delegate = | |
121 new SelfDeletingRequestDelegate(web_contents); | |
122 scoped_ptr<content::WebContents> old_web_contents_sptr(web_contents); | |
123 scoped_ptr<SourcePageHandleWebContents> source_page_handle( | |
124 new SourcePageHandleWebContents(old_web_contents_sptr.Pass())); | |
125 DomDistillerService* dom_distiller_service = | |
126 DomDistillerServiceFactory::GetForBrowserContext( | |
127 web_contents->GetBrowserContext()); | |
128 scoped_ptr<DistillerPage> distiller_page = | |
129 dom_distiller_service->CreateDefaultDistillerPageWithHandle( | |
130 source_page_handle.PassAs<SourcePageHandle>()) | |
131 .Pass(); | |
132 | |
133 const GURL& last_committed_url = web_contents->GetLastCommittedURL(); | |
134 scoped_ptr<ViewerHandle> viewer_handle = dom_distiller_service->ViewUrl( | |
135 view_request_delegate, distiller_page.Pass(), last_committed_url); | |
136 view_request_delegate->TakeViewerHandle(viewer_handle.Pass()); | |
137 } | |
138 | |
139 } // namespace | |
140 | |
141 void DistillCurrentPageAndView(content::WebContents* old_web_contents) { | |
142 DCHECK(old_web_contents); | |
143 // Create new WebContents. | |
144 content::WebContents::CreateParams create_params( | |
145 old_web_contents->GetBrowserContext()); | |
146 content::WebContents* new_web_contents = | |
147 content::WebContents::Create(create_params); | |
148 DCHECK(new_web_contents); | |
149 | |
150 // Copy all navigation state from the old WebContents to the new one. | |
151 new_web_contents->GetController().CopyStateFrom( | |
152 old_web_contents->GetController()); | |
153 | |
154 CoreTabHelper::FromWebContents(old_web_contents)->delegate()->SwapTabContents( | |
155 old_web_contents, new_web_contents, false, false); | |
156 | |
157 StartNavigationToDistillerViewer(new_web_contents, | |
158 old_web_contents->GetLastCommittedURL()); | |
159 StartDistillation(old_web_contents); | |
160 } | |
OLD | NEW |