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/android/window_android_helper.h" | |
nyquist
2014/05/06 03:52:27
Note to self: Remove.
nyquist
2014/05/15 10:15:35
Done.
| |
10 #include "chrome/browser/ui/tab_contents/core_tab_helper.h" | |
11 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" | |
12 #include "chrome/common/url_constants.h" | |
13 #include "components/dom_distiller/content/distiller_page_web_contents.h" | |
14 #include "components/dom_distiller/core/distiller_page.h" | |
15 #include "components/dom_distiller/core/dom_distiller_service.h" | |
16 #include "components/dom_distiller/core/task_tracker.h" | |
17 #include "components/dom_distiller/core/url_utils.h" | |
18 #include "content/public/browser/browser_context.h" | |
19 #include "content/public/browser/navigation_controller.h" | |
20 #include "content/public/browser/web_contents.h" | |
21 #include "content/public/browser/web_contents_observer.h" | |
22 | |
23 namespace dom_distiller { | |
24 | |
25 namespace tab_utils { | |
26 | |
27 // An no-op ViewRequestDelegate which holds a ViewerHandle and deletes itself | |
28 // after |delay_seconds|. This class is a band-aid to keep a TaskTracker around | |
29 // for a specified amount of time. | |
30 class DelayedSelfDeletingRequestDelegate : public ViewRequestDelegate { | |
31 public: | |
32 // Delays deletion of |this| for |delay_seconds| seconds. | |
33 explicit DelayedSelfDeletingRequestDelegate(int64 delay_seconds); | |
34 virtual ~DelayedSelfDeletingRequestDelegate(); | |
35 | |
36 // ViewRequestDelegate implementation. | |
37 virtual void OnArticleReady( | |
38 const DistilledArticleProto* article_proto) OVERRIDE; | |
39 virtual void OnArticleUpdated( | |
40 ArticleDistillationUpdate article_update) OVERRIDE; | |
41 | |
42 // Takes ownership of the ViewerHandle to keep distillation alive until |this| | |
43 // is deleted. | |
44 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | |
45 | |
46 private: | |
47 // Posts a task to delete |this| before the next run of the MessageLoop. | |
48 void DeleteSoon(); | |
49 | |
50 // The handle to the view request towards the DomDistillerService. It | |
51 // needs to be kept around to ensure the distillation request finishes. | |
52 scoped_ptr<ViewerHandle> viewer_handle_; | |
53 }; | |
54 | |
55 DelayedSelfDeletingRequestDelegate::DelayedSelfDeletingRequestDelegate( | |
56 int64 delay_seconds) { | |
57 base::MessageLoop::current()->PostDelayedTask( | |
58 FROM_HERE, | |
59 base::Bind(&DelayedSelfDeletingRequestDelegate::DeleteSoon, | |
60 base::Unretained(this)), | |
61 base::TimeDelta::FromSeconds(delay_seconds)); | |
62 } | |
63 | |
64 DelayedSelfDeletingRequestDelegate::~DelayedSelfDeletingRequestDelegate() { | |
65 } | |
66 | |
67 void DelayedSelfDeletingRequestDelegate::OnArticleReady( | |
68 const DistilledArticleProto* article_proto) { | |
69 } | |
70 | |
71 void DelayedSelfDeletingRequestDelegate::OnArticleUpdated( | |
72 ArticleDistillationUpdate article_update) { | |
73 } | |
74 | |
75 void DelayedSelfDeletingRequestDelegate::TakeViewerHandle( | |
76 scoped_ptr<ViewerHandle> viewer_handle) { | |
77 viewer_handle_ = viewer_handle.Pass(); | |
78 } | |
79 | |
80 void DelayedSelfDeletingRequestDelegate::DeleteSoon() { | |
81 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
82 } | |
83 | |
84 // Start loading the viewer URL of the current page in |web_contents|. | |
85 void StartNavigationToDistillerViewer(content::WebContents* web_contents, | |
86 const GURL& url) { | |
87 GURL viewer_url = | |
88 url_utils::GetDistillerViewUrlFromUrl(chrome::kDomDistillerScheme, url); | |
89 content::NavigationController::LoadURLParams params(viewer_url); | |
90 web_contents->GetController().LoadURLWithParams(params); | |
91 } | |
92 | |
93 void StartDistillation(content::WebContents* web_contents) { | |
94 // Start distillation using |web_contents|. | |
95 scoped_ptr<content::WebContents> old_web_contents_sptr(web_contents); | |
96 scoped_ptr<SourcePageHandleWebContents> source_page_handle( | |
97 new SourcePageHandleWebContents(old_web_contents_sptr.Pass())); | |
98 DomDistillerService* dom_distiller_service = | |
99 DomDistillerServiceFactory::GetForBrowserContext( | |
100 web_contents->GetBrowserContext()); | |
101 scoped_ptr<DistillerPage> distiller_page = | |
102 dom_distiller_service->CreateDefaultDistillerPageWithHandle( | |
103 source_page_handle.PassAs<SourcePageHandle>()) | |
104 .Pass(); | |
105 | |
106 DelayedSelfDeletingRequestDelegate* view_request_delegate = | |
107 new DelayedSelfDeletingRequestDelegate(5); | |
108 scoped_ptr<ViewerHandle> viewer_handle = | |
109 dom_distiller_service->ViewUrl(view_request_delegate, | |
110 distiller_page.Pass(), | |
111 web_contents->GetLastCommittedURL()); | |
112 view_request_delegate->TakeViewerHandle(viewer_handle.Pass()); | |
113 } | |
114 | |
115 void DistillCurrentPageAndView(content::WebContents* old_web_contents) { | |
116 DCHECK(old_web_contents); | |
117 // Create new WebContents. | |
118 content::WebContents::CreateParams create_params( | |
119 old_web_contents->GetBrowserContext()); | |
120 content::WebContents* new_web_contents = | |
121 content::WebContents::Create(create_params); | |
122 DCHECK(new_web_contents); | |
123 | |
124 // Copy all navigation state from the old WebContents to the new one. | |
125 new_web_contents->GetController().CopyStateFrom( | |
126 old_web_contents->GetController()); | |
127 | |
128 CoreTabHelper::FromWebContents(old_web_contents)->delegate()->SwapTabContents( | |
129 old_web_contents, new_web_contents, false, false); | |
130 | |
131 StartNavigationToDistillerViewer(new_web_contents, | |
132 old_web_contents->GetLastCommittedURL()); | |
133 StartDistillation(old_web_contents); | |
134 } | |
135 | |
136 } // namespace tab_utils | |
137 | |
138 } // namespace dom_distiller | |
OLD | NEW |