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

Unified 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: Created 6 years, 8 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/dom_distiller/tab_utils.cc
diff --git a/chrome/browser/dom_distiller/tab_utils.cc b/chrome/browser/dom_distiller/tab_utils.cc
new file mode 100644
index 0000000000000000000000000000000000000000..16e9fb0e043481a9e83a577f558c5a848babd53c
--- /dev/null
+++ b/chrome/browser/dom_distiller/tab_utils.cc
@@ -0,0 +1,138 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/dom_distiller/tab_utils.h"
+
+#include "base/message_loop/message_loop.h"
+#include "chrome/browser/dom_distiller/dom_distiller_service_factory.h"
+#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.
+#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
+#include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
+#include "chrome/common/url_constants.h"
+#include "components/dom_distiller/content/distiller_page_web_contents.h"
+#include "components/dom_distiller/core/distiller_page.h"
+#include "components/dom_distiller/core/dom_distiller_service.h"
+#include "components/dom_distiller/core/task_tracker.h"
+#include "components/dom_distiller/core/url_utils.h"
+#include "content/public/browser/browser_context.h"
+#include "content/public/browser/navigation_controller.h"
+#include "content/public/browser/web_contents.h"
+#include "content/public/browser/web_contents_observer.h"
+
+namespace dom_distiller {
+
+namespace tab_utils {
+
+// An no-op ViewRequestDelegate which holds a ViewerHandle and deletes itself
+// after |delay_seconds|. This class is a band-aid to keep a TaskTracker around
+// for a specified amount of time.
+class DelayedSelfDeletingRequestDelegate : public ViewRequestDelegate {
+ public:
+ // Delays deletion of |this| for |delay_seconds| seconds.
+ explicit DelayedSelfDeletingRequestDelegate(int64 delay_seconds);
+ virtual ~DelayedSelfDeletingRequestDelegate();
+
+ // ViewRequestDelegate implementation.
+ virtual void OnArticleReady(
+ const DistilledArticleProto* article_proto) OVERRIDE;
+ virtual void OnArticleUpdated(
+ ArticleDistillationUpdate article_update) OVERRIDE;
+
+ // Takes ownership of the ViewerHandle to keep distillation alive until |this|
+ // is deleted.
+ void TakeViewerHandle(scoped_ptr<ViewerHandle> viewer_handle);
+
+ private:
+ // Posts a task to delete |this| before the next run of the MessageLoop.
+ void DeleteSoon();
+
+ // The handle to the view request towards the DomDistillerService. It
+ // needs to be kept around to ensure the distillation request finishes.
+ scoped_ptr<ViewerHandle> viewer_handle_;
+};
+
+DelayedSelfDeletingRequestDelegate::DelayedSelfDeletingRequestDelegate(
+ int64 delay_seconds) {
+ base::MessageLoop::current()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&DelayedSelfDeletingRequestDelegate::DeleteSoon,
+ base::Unretained(this)),
+ base::TimeDelta::FromSeconds(delay_seconds));
+}
+
+DelayedSelfDeletingRequestDelegate::~DelayedSelfDeletingRequestDelegate() {
+}
+
+void DelayedSelfDeletingRequestDelegate::OnArticleReady(
+ const DistilledArticleProto* article_proto) {
+}
+
+void DelayedSelfDeletingRequestDelegate::OnArticleUpdated(
+ ArticleDistillationUpdate article_update) {
+}
+
+void DelayedSelfDeletingRequestDelegate::TakeViewerHandle(
+ scoped_ptr<ViewerHandle> viewer_handle) {
+ viewer_handle_ = viewer_handle.Pass();
+}
+
+void DelayedSelfDeletingRequestDelegate::DeleteSoon() {
+ base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
+}
+
+// Start loading the viewer URL of the current page in |web_contents|.
+void StartNavigationToDistillerViewer(content::WebContents* web_contents,
+ const GURL& url) {
+ GURL viewer_url =
+ url_utils::GetDistillerViewUrlFromUrl(chrome::kDomDistillerScheme, url);
+ content::NavigationController::LoadURLParams params(viewer_url);
+ web_contents->GetController().LoadURLWithParams(params);
+}
+
+void StartDistillation(content::WebContents* web_contents) {
+ // Start distillation using |web_contents|.
+ scoped_ptr<content::WebContents> old_web_contents_sptr(web_contents);
+ scoped_ptr<SourcePageHandleWebContents> source_page_handle(
+ new SourcePageHandleWebContents(old_web_contents_sptr.Pass()));
+ DomDistillerService* dom_distiller_service =
+ DomDistillerServiceFactory::GetForBrowserContext(
+ web_contents->GetBrowserContext());
+ scoped_ptr<DistillerPage> distiller_page =
+ dom_distiller_service->CreateDefaultDistillerPageWithHandle(
+ source_page_handle.PassAs<SourcePageHandle>())
+ .Pass();
+
+ DelayedSelfDeletingRequestDelegate* view_request_delegate =
+ new DelayedSelfDeletingRequestDelegate(5);
+ scoped_ptr<ViewerHandle> viewer_handle =
+ dom_distiller_service->ViewUrl(view_request_delegate,
+ distiller_page.Pass(),
+ web_contents->GetLastCommittedURL());
+ view_request_delegate->TakeViewerHandle(viewer_handle.Pass());
+}
+
+void DistillCurrentPageAndView(content::WebContents* old_web_contents) {
+ DCHECK(old_web_contents);
+ // Create new WebContents.
+ content::WebContents::CreateParams create_params(
+ old_web_contents->GetBrowserContext());
+ content::WebContents* new_web_contents =
+ content::WebContents::Create(create_params);
+ DCHECK(new_web_contents);
+
+ // Copy all navigation state from the old WebContents to the new one.
+ new_web_contents->GetController().CopyStateFrom(
+ old_web_contents->GetController());
+
+ CoreTabHelper::FromWebContents(old_web_contents)->delegate()->SwapTabContents(
+ old_web_contents, new_web_contents, false, false);
+
+ StartNavigationToDistillerViewer(new_web_contents,
+ old_web_contents->GetLastCommittedURL());
+ StartDistillation(old_web_contents);
+}
+
+} // namespace tab_utils
+
+} // namespace dom_distiller

Powered by Google App Engine
This is Rietveld 408576698