Chromium Code Reviews| 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 dom_distiller { | |
| 23 | |
| 24 namespace tab_utils { | |
| 25 | |
| 26 // An no-op ViewRequestDelegate which holds a ViewerHandle and deletes itself | |
| 27 // after the WebContents navigates or goes away. This class is a band-aid to | |
| 28 // keep a TaskTracker around until the distillation starts from the viewer. | |
| 29 class SelfDeletingRequestDelegate : public ViewRequestDelegate, | |
| 30 public content::WebContentsObserver { | |
| 31 public: | |
| 32 explicit SelfDeletingRequestDelegate(content::WebContents* web_contents); | |
| 33 virtual ~SelfDeletingRequestDelegate(); | |
| 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 // content::WebContentsObserver implementation. | |
| 42 virtual void DidNavigateMainFrame( | |
| 43 const content::LoadCommittedDetails& details, | |
| 44 const content::FrameNavigateParams& params) OVERRIDE; | |
| 45 virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE; | |
| 46 virtual void WebContentsDestroyed() OVERRIDE; | |
| 47 | |
| 48 // Takes ownership of the ViewerHandle to keep distillation alive until |this| | |
| 49 // is deleted. | |
| 50 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle); | |
| 51 | |
| 52 private: | |
| 53 // The handle to the view request towards the DomDistillerService. It | |
| 54 // needs to be kept around to ensure the distillation request finishes. | |
| 55 scoped_ptr<ViewerHandle> viewer_handle_; | |
| 56 | |
| 57 // The WebContents this class is tracking. | |
| 58 content::WebContents* web_contents_; | |
| 59 }; | |
| 60 | |
| 61 void SelfDeletingRequestDelegate::DidNavigateMainFrame( | |
| 62 const content::LoadCommittedDetails& details, | |
| 63 const content::FrameNavigateParams& params) { | |
| 64 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 65 } | |
| 66 | |
| 67 void SelfDeletingRequestDelegate::RenderProcessGone( | |
| 68 base::TerminationStatus status) { | |
| 69 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 70 } | |
| 71 | |
| 72 void SelfDeletingRequestDelegate::WebContentsDestroyed() { | |
| 73 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
| 74 } | |
| 75 | |
| 76 SelfDeletingRequestDelegate::SelfDeletingRequestDelegate( | |
| 77 content::WebContents* web_contents) | |
| 78 : web_contents_(web_contents) { | |
| 79 content::WebContentsObserver::Observe(web_contents_); | |
| 80 } | |
| 81 | |
| 82 SelfDeletingRequestDelegate::~SelfDeletingRequestDelegate() { | |
| 83 content::WebContentsObserver::Observe(NULL); | |
| 84 } | |
| 85 | |
| 86 void SelfDeletingRequestDelegate::OnArticleReady( | |
| 87 const DistilledArticleProto* article_proto) { | |
| 88 } | |
| 89 | |
| 90 void SelfDeletingRequestDelegate::OnArticleUpdated( | |
| 91 ArticleDistillationUpdate article_update) { | |
| 92 } | |
| 93 | |
| 94 void SelfDeletingRequestDelegate::TakeViewerHandle( | |
| 95 scoped_ptr<ViewerHandle> viewer_handle) { | |
| 96 viewer_handle_ = viewer_handle.Pass(); | |
| 97 } | |
| 98 | |
| 99 // Start loading the viewer URL of the current page in |web_contents|. | |
| 100 void StartNavigationToDistillerViewer(content::WebContents* web_contents, | |
| 101 const GURL& url) { | |
| 102 GURL viewer_url = | |
| 103 url_utils::GetDistillerViewUrlFromUrl(chrome::kDomDistillerScheme, url); | |
| 104 content::NavigationController::LoadURLParams params(viewer_url); | |
|
Yaron
2014/05/22 18:25:02
I think this should have PAGE_TRANSITION_AUTO_BOOK
nyquist
2014/05/22 23:00:54
Done.
| |
| 105 web_contents->GetController().LoadURLWithParams(params); | |
| 106 } | |
| 107 | |
| 108 void StartDistillation(content::WebContents* web_contents) { | |
| 109 // Start distillation using |web_contents|, and ensure ViewerHandle stays | |
| 110 // around until the viewer requests distillation. | |
| 111 SelfDeletingRequestDelegate* view_request_delegate = | |
| 112 new SelfDeletingRequestDelegate(web_contents); | |
| 113 scoped_ptr<content::WebContents> old_web_contents_sptr(web_contents); | |
| 114 scoped_ptr<SourcePageHandleWebContents> source_page_handle( | |
| 115 new SourcePageHandleWebContents(old_web_contents_sptr.Pass())); | |
| 116 DomDistillerService* dom_distiller_service = | |
| 117 DomDistillerServiceFactory::GetForBrowserContext( | |
| 118 web_contents->GetBrowserContext()); | |
| 119 scoped_ptr<DistillerPage> distiller_page = | |
| 120 dom_distiller_service->CreateDefaultDistillerPageWithHandle( | |
| 121 source_page_handle.PassAs<SourcePageHandle>()) | |
| 122 .Pass(); | |
| 123 | |
| 124 const GURL& last_committed_url = web_contents->GetLastCommittedURL(); | |
| 125 scoped_ptr<ViewerHandle> viewer_handle = dom_distiller_service->ViewUrl( | |
| 126 view_request_delegate, distiller_page.Pass(), last_committed_url); | |
| 127 view_request_delegate->TakeViewerHandle(viewer_handle.Pass()); | |
| 128 } | |
| 129 | |
| 130 void DistillCurrentPageAndView(content::WebContents* old_web_contents) { | |
| 131 DCHECK(old_web_contents); | |
| 132 // Create new WebContents. | |
| 133 content::WebContents::CreateParams create_params( | |
| 134 old_web_contents->GetBrowserContext()); | |
| 135 content::WebContents* new_web_contents = | |
| 136 content::WebContents::Create(create_params); | |
| 137 DCHECK(new_web_contents); | |
| 138 | |
| 139 // Copy all navigation state from the old WebContents to the new one. | |
| 140 new_web_contents->GetController().CopyStateFrom( | |
| 141 old_web_contents->GetController()); | |
| 142 | |
| 143 CoreTabHelper::FromWebContents(old_web_contents)->delegate()->SwapTabContents( | |
| 144 old_web_contents, new_web_contents, false, false); | |
| 145 | |
| 146 StartNavigationToDistillerViewer(new_web_contents, | |
| 147 old_web_contents->GetLastCommittedURL()); | |
| 148 StartDistillation(old_web_contents); | |
| 149 } | |
| 150 | |
| 151 } // namespace tab_utils | |
| 152 | |
| 153 } // namespace dom_distiller | |
| OLD | NEW |