Use app list item shadow for app list folders.
[chromium-blink-merge.git] / chrome / browser / dom_distiller / tab_utils.cc
blob06e0ee2a1c4b95850e91a2e0f78422258c6856fe
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.
5 #include "chrome/browser/dom_distiller/tab_utils.h"
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 "components/dom_distiller/content/distiller_page_web_contents.h"
12 #include "components/dom_distiller/core/distiller_page.h"
13 #include "components/dom_distiller/core/dom_distiller_service.h"
14 #include "components/dom_distiller/core/task_tracker.h"
15 #include "components/dom_distiller/core/url_constants.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"
22 namespace {
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;
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 ~SelfDeletingRequestDelegate() override;
43 // ViewRequestDelegate implementation.
44 void OnArticleReady(const DistilledArticleProto* article_proto) override;
45 void OnArticleUpdated(ArticleDistillationUpdate article_update) override;
47 // content::WebContentsObserver implementation.
48 void DidNavigateMainFrame(
49 const content::LoadCommittedDetails& details,
50 const content::FrameNavigateParams& params) override;
51 void RenderProcessGone(base::TerminationStatus status) override;
52 void WebContentsDestroyed() override;
54 // Takes ownership of the ViewerHandle to keep distillation alive until |this|
55 // is deleted.
56 void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle);
58 private:
59 // The handle to the view request towards the DomDistillerService. It
60 // needs to be kept around to ensure the distillation request finishes.
61 scoped_ptr<ViewerHandle> viewer_handle_;
64 void SelfDeletingRequestDelegate::DidNavigateMainFrame(
65 const content::LoadCommittedDetails& details,
66 const content::FrameNavigateParams& params) {
67 Observe(NULL);
68 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
71 void SelfDeletingRequestDelegate::RenderProcessGone(
72 base::TerminationStatus status) {
73 Observe(NULL);
74 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
77 void SelfDeletingRequestDelegate::WebContentsDestroyed() {
78 Observe(NULL);
79 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
82 SelfDeletingRequestDelegate::SelfDeletingRequestDelegate(
83 content::WebContents* web_contents)
84 : WebContentsObserver(web_contents) {
87 SelfDeletingRequestDelegate::~SelfDeletingRequestDelegate() {
90 void SelfDeletingRequestDelegate::OnArticleReady(
91 const DistilledArticleProto* article_proto) {
94 void SelfDeletingRequestDelegate::OnArticleUpdated(
95 ArticleDistillationUpdate article_update) {
98 void SelfDeletingRequestDelegate::TakeViewerHandle(
99 scoped_ptr<ViewerHandle> viewer_handle) {
100 viewer_handle_ = viewer_handle.Pass();
103 // Start loading the viewer URL of the current page in |web_contents|.
104 void StartNavigationToDistillerViewer(content::WebContents* web_contents,
105 const GURL& url) {
106 GURL viewer_url = dom_distiller::url_utils::GetDistillerViewUrlFromUrl(
107 dom_distiller::kDomDistillerScheme, url);
108 content::NavigationController::LoadURLParams params(viewer_url);
109 params.transition_type = ui::PAGE_TRANSITION_AUTO_BOOKMARK;
110 web_contents->GetController().LoadURLWithParams(params);
113 void MaybeStartDistillation(
114 scoped_ptr<SourcePageHandleWebContents> source_page_handle) {
115 const GURL& last_committed_url =
116 source_page_handle->web_contents()->GetLastCommittedURL();
117 if (!dom_distiller::url_utils::IsUrlDistillable(last_committed_url))
118 return;
120 // Start distillation using |source_page_handle|, and ensure ViewerHandle
121 // stays around until the viewer requests distillation.
122 SelfDeletingRequestDelegate* view_request_delegate =
123 new SelfDeletingRequestDelegate(source_page_handle->web_contents());
124 DomDistillerService* dom_distiller_service =
125 DomDistillerServiceFactory::GetForBrowserContext(
126 source_page_handle->web_contents()->GetBrowserContext());
127 scoped_ptr<DistillerPage> distiller_page =
128 dom_distiller_service->CreateDefaultDistillerPageWithHandle(
129 source_page_handle.Pass()).Pass();
131 scoped_ptr<ViewerHandle> viewer_handle = dom_distiller_service->ViewUrl(
132 view_request_delegate, distiller_page.Pass(), last_committed_url);
133 view_request_delegate->TakeViewerHandle(viewer_handle.Pass());
136 } // namespace
138 void DistillCurrentPageAndView(content::WebContents* old_web_contents) {
139 DCHECK(old_web_contents);
140 // Create new WebContents.
141 content::WebContents::CreateParams create_params(
142 old_web_contents->GetBrowserContext());
143 content::WebContents* new_web_contents =
144 content::WebContents::Create(create_params);
145 DCHECK(new_web_contents);
147 // Copy all navigation state from the old WebContents to the new one.
148 new_web_contents->GetController().CopyStateFrom(
149 old_web_contents->GetController());
151 // StartNavigationToDistillerViewer must come before swapping the tab contents
152 // to avoid triggering a reload of the page. This reloadmakes it very
153 // difficult to distinguish between the intermediate reload and a user hitting
154 // the back button.
155 StartNavigationToDistillerViewer(new_web_contents,
156 old_web_contents->GetLastCommittedURL());
158 CoreTabHelper::FromWebContents(old_web_contents)->delegate()->SwapTabContents(
159 old_web_contents, new_web_contents, false, false);
161 scoped_ptr<SourcePageHandleWebContents> source_page_handle(
162 new SourcePageHandleWebContents(old_web_contents, true));
164 MaybeStartDistillation(source_page_handle.Pass());
167 void DistillAndView(content::WebContents* source_web_contents,
168 content::WebContents* destination_web_contents) {
169 DCHECK(source_web_contents);
170 DCHECK(destination_web_contents);
172 scoped_ptr<SourcePageHandleWebContents> source_page_handle(
173 new SourcePageHandleWebContents(source_web_contents, false));
175 MaybeStartDistillation(source_page_handle.Pass());
176 StartNavigationToDistillerViewer(destination_web_contents,
177 source_web_contents->GetLastCommittedURL());