1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/dom_distiller/tab_utils.h"
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/message_loop/message_loop.h"
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/dom_distiller/content/distiller_page_web_contents.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/dom_distiller/core/distiller_page.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/dom_distiller/core/dom_distiller_service.h"
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/dom_distiller/core/task_tracker.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/dom_distiller/core/url_constants.h"
16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/dom_distiller/core/url_utils.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/browser_context.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/navigation_controller.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/web_contents.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/web_contents_observer.h"
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)namespace {
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using dom_distiller::ViewRequestDelegate;
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using dom_distiller::DistilledArticleProto;
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using dom_distiller::ArticleDistillationUpdate;
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using dom_distiller::ViewerHandle;
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using dom_distiller::SourcePageHandleWebContents;
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using dom_distiller::DomDistillerService;
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using dom_distiller::DomDistillerServiceFactory;
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using dom_distiller::DistillerPage;
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)using dom_distiller::SourcePageHandle;
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// An no-op ViewRequestDelegate which holds a ViewerHandle and deletes itself
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// after the WebContents navigates or goes away. This class is a band-aid to
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// keep a TaskTracker around until the distillation starts from the viewer.
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class SelfDeletingRequestDelegate : public ViewRequestDelegate,
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                    public content::WebContentsObserver {
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public:
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  explicit SelfDeletingRequestDelegate(content::WebContents* web_contents);
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual ~SelfDeletingRequestDelegate();
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // ViewRequestDelegate implementation.
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void OnArticleReady(
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const DistilledArticleProto* article_proto) OVERRIDE;
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void OnArticleUpdated(
47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      ArticleDistillationUpdate article_update) OVERRIDE;
48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // content::WebContentsObserver implementation.
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void DidNavigateMainFrame(
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const content::LoadCommittedDetails& details,
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      const content::FrameNavigateParams& params) OVERRIDE;
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void WebContentsDestroyed() OVERRIDE;
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Takes ownership of the ViewerHandle to keep distillation alive until |this|
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // is deleted.
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle);
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) private:
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The handle to the view request towards the DomDistillerService. It
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // needs to be kept around to ensure the distillation request finishes.
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<ViewerHandle> viewer_handle_;
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)};
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void SelfDeletingRequestDelegate::DidNavigateMainFrame(
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const content::LoadCommittedDetails& details,
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const content::FrameNavigateParams& params) {
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void SelfDeletingRequestDelegate::RenderProcessGone(
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    base::TerminationStatus status) {
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void SelfDeletingRequestDelegate::WebContentsDestroyed() {
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)SelfDeletingRequestDelegate::SelfDeletingRequestDelegate(
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    content::WebContents* web_contents)
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : WebContentsObserver(web_contents) {
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)SelfDeletingRequestDelegate::~SelfDeletingRequestDelegate() {
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void SelfDeletingRequestDelegate::OnArticleReady(
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const DistilledArticleProto* article_proto) {
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void SelfDeletingRequestDelegate::OnArticleUpdated(
94cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    ArticleDistillationUpdate article_update) {
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void SelfDeletingRequestDelegate::TakeViewerHandle(
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    scoped_ptr<ViewerHandle> viewer_handle) {
99cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  viewer_handle_ = viewer_handle.Pass();
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
102cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Start loading the viewer URL of the current page in |web_contents|.
103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void StartNavigationToDistillerViewer(content::WebContents* web_contents,
104cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                      const GURL& url) {
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  GURL viewer_url = dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
1065f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      dom_distiller::kDomDistillerScheme, url);
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  content::NavigationController::LoadURLParams params(viewer_url);
1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  params.transition_type = ui::PAGE_TRANSITION_AUTO_BOOKMARK;
109cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  web_contents->GetController().LoadURLWithParams(params);
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
112cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void StartDistillation(content::WebContents* web_contents) {
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Start distillation using |web_contents|, and ensure ViewerHandle stays
114cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // around until the viewer requests distillation.
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  SelfDeletingRequestDelegate* view_request_delegate =
116cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new SelfDeletingRequestDelegate(web_contents);
117cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<content::WebContents> old_web_contents_sptr(web_contents);
118cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<SourcePageHandleWebContents> source_page_handle(
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new SourcePageHandleWebContents(old_web_contents_sptr.Pass()));
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DomDistillerService* dom_distiller_service =
121cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      DomDistillerServiceFactory::GetForBrowserContext(
122cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          web_contents->GetBrowserContext());
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<DistillerPage> distiller_page =
124cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      dom_distiller_service->CreateDefaultDistillerPageWithHandle(
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                 source_page_handle.PassAs<SourcePageHandle>())
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          .Pass();
127cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const GURL& last_committed_url = web_contents->GetLastCommittedURL();
129cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  scoped_ptr<ViewerHandle> viewer_handle = dom_distiller_service->ViewUrl(
130cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      view_request_delegate, distiller_page.Pass(), last_committed_url);
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  view_request_delegate->TakeViewerHandle(viewer_handle.Pass());
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}  // namespace
135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void DistillCurrentPageAndView(content::WebContents* old_web_contents) {
137cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(old_web_contents);
138cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Create new WebContents.
139cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  content::WebContents::CreateParams create_params(
140cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      old_web_contents->GetBrowserContext());
141cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  content::WebContents* new_web_contents =
142cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      content::WebContents::Create(create_params);
143cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DCHECK(new_web_contents);
144cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Copy all navigation state from the old WebContents to the new one.
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  new_web_contents->GetController().CopyStateFrom(
147cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      old_web_contents->GetController());
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // StartNavigationToDistillerViewer must come before swapping the tab contents
1506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // to avoid triggering a reload of the page.  This reloadmakes it very
1516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // difficult to distinguish between the intermediate reload and a user hitting
1526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // the back button.
1536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  StartNavigationToDistillerViewer(new_web_contents,
1546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                   old_web_contents->GetLastCommittedURL());
1556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
156cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CoreTabHelper::FromWebContents(old_web_contents)->delegate()->SwapTabContents(
157cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      old_web_contents, new_web_contents, false, false);
158cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
159cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  StartDistillation(old_web_contents);
160cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
161