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

Side by Side Diff: chrome/browser/dom_distiller/tab_utils.cc

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

Powered by Google App Engine
This is Rietveld 408576698