Commit b1c47491 authored by sdefresne's avatar sdefresne Committed by Commit bot

Upstream dom_distiller iOS factories

BUG=459678
R=noyau@chromium.org

Review URL: https://codereview.chromium.org/972683002

Cr-Commit-Position: refs/heads/master@{#318691}
parent 2bbdeede
......@@ -4,6 +4,8 @@ include_rules = [
"-ios/chrome",
"+ios/chrome/grit",
"+components/dom_distiller/core",
"+components/dom_distiller/ios",
"+components/infobars/core",
"+components/keyed_service/core",
"+components/keyed_service/ios",
......
// Copyright 2015 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 "ios/chrome/browser/dom_distiller/distiller_viewer.h"
#include <string>
#include "components/dom_distiller/core/distilled_page_prefs.h"
#include "components/dom_distiller/core/dom_distiller_service.h"
#include "components/dom_distiller/core/proto/distilled_article.pb.h"
#include "components/dom_distiller/core/task_tracker.h"
#include "components/dom_distiller/core/viewer.h"
#include "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h"
#include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
#include "ui/gfx/geometry/size.h"
namespace dom_distiller {
DistillerViewer::DistillerViewer(ios::ChromeBrowserState* browser_state,
const GURL& url,
const DistillationFinishedCallback& callback)
: url_(url),
callback_(callback),
distilled_page_prefs_(new DistilledPagePrefs(browser_state->GetPrefs())) {
DCHECK(browser_state);
DCHECK(url.is_valid());
dom_distiller::DomDistillerService* distillerService =
dom_distiller::DomDistillerServiceFactory::GetForBrowserState(
browser_state);
viewer_handle_ = distillerService->ViewUrl(
this, distillerService->CreateDefaultDistillerPage(gfx::Size()), url);
}
DistillerViewer::~DistillerViewer() {
}
void DistillerViewer::OnArticleReady(
const DistilledArticleProto* article_proto) {
const std::string html = viewer::GetUnsafeArticleHtml(
article_proto, distilled_page_prefs_->GetTheme(),
distilled_page_prefs_->GetFontFamily());
callback_.Run(url_, html);
// No need to hold on to the ViewerHandle now that distillation is complete.
viewer_handle_.reset();
}
} // namespace dom_distiller
// Copyright 2015 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.
#ifndef IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_
#define IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_
#include "base/memory/scoped_ptr.h"
#include "components/dom_distiller/core/task_tracker.h"
class GURL;
namespace ios {
class ChromeBrowserState;
}
namespace dom_distiller {
class DistilledPagePrefs;
// A very simple and naive implementation of the dom_distiller
// ViewRequestDelegate: From an URL it builds an HTML string and notifies when
// finished.
class DistillerViewer : public dom_distiller::ViewRequestDelegate {
public:
typedef base::Callback<void(const GURL&, const std::string&)>
DistillationFinishedCallback;
DistillerViewer(ios::ChromeBrowserState* browser_state,
const GURL& url,
const DistillationFinishedCallback& callback);
~DistillerViewer() override;
// ViewRequestDelegate.
void OnArticleUpdated(
dom_distiller::ArticleDistillationUpdate article_update) override {}
void OnArticleReady(const DistilledArticleProto* article_proto) override;
private:
// The url of the distilled page.
const GURL url_;
// Callback to invoke when the page is finished.
DistillationFinishedCallback callback_;
// Interface for accessing preferences for distilled pages.
scoped_ptr<DistilledPagePrefs> distilled_page_prefs_;
// Keeps the distiller going until the view is released.
scoped_ptr<dom_distiller::ViewerHandle> viewer_handle_;
DISALLOW_COPY_AND_ASSIGN(DistillerViewer);
};
} // namespace dom_distiller
#endif // IOS_CHROME_BROWSER_DOM_DISTILLER_DISTILLER_VIEWER_H_
// Copyright 2015 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 "ios/chrome/browser/dom_distiller/dom_distiller_service_factory.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/threading/sequenced_worker_pool.h"
#include "components/dom_distiller/core/article_entry.h"
#include "components/dom_distiller/core/distiller.h"
#include "components/dom_distiller/core/dom_distiller_service.h"
#include "components/dom_distiller/core/dom_distiller_store.h"
#include "components/dom_distiller/ios/distiller_page_factory_ios.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/keyed_service/ios/browser_state_dependency_manager.h"
#include "components/leveldb_proto/proto_database.h"
#include "components/leveldb_proto/proto_database_impl.h"
#include "ios/chrome/browser/browser_state/browser_state_otr_helper.h"
#include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.h"
#include "ios/web/public/browser_state.h"
#include "ios/web/public/web_thread.h"
namespace {
// A simple wrapper for DomDistillerService to expose it as a
// KeyedService.
class DomDistillerKeyedService
: public KeyedService,
public dom_distiller::DomDistillerService {
public:
DomDistillerKeyedService(
scoped_ptr<dom_distiller::DomDistillerStoreInterface> store,
scoped_ptr<dom_distiller::DistillerFactory> distiller_factory,
scoped_ptr<dom_distiller::DistillerPageFactory> distiller_page_factory,
scoped_ptr<dom_distiller::DistilledPagePrefs> distilled_page_prefs)
: DomDistillerService(store.Pass(),
distiller_factory.Pass(),
distiller_page_factory.Pass(),
distilled_page_prefs.Pass()) {}
~DomDistillerKeyedService() override {}
private:
DISALLOW_COPY_AND_ASSIGN(DomDistillerKeyedService);
};
} // namespace
namespace dom_distiller {
// static
DomDistillerServiceFactory* DomDistillerServiceFactory::GetInstance() {
return Singleton<DomDistillerServiceFactory>::get();
}
// static
DomDistillerService* DomDistillerServiceFactory::GetForBrowserState(
web::BrowserState* browser_state) {
return static_cast<DomDistillerKeyedService*>(
GetInstance()->GetServiceForBrowserState(browser_state, true));
}
DomDistillerServiceFactory::DomDistillerServiceFactory()
: BrowserStateKeyedServiceFactory(
"DomDistillerService",
BrowserStateDependencyManager::GetInstance()) {
}
DomDistillerServiceFactory::~DomDistillerServiceFactory() {
}
KeyedService* DomDistillerServiceFactory::BuildServiceInstanceFor(
web::BrowserState* browser_state) const {
scoped_refptr<base::SequencedTaskRunner> background_task_runner =
web::WebThread::GetBlockingPool()->GetSequencedTaskRunner(
web::WebThread::GetBlockingPool()->GetSequenceToken());
scoped_ptr<leveldb_proto::ProtoDatabaseImpl<ArticleEntry>> db(
new leveldb_proto::ProtoDatabaseImpl<ArticleEntry>(
background_task_runner));
base::FilePath database_dir(
browser_state->GetStatePath().Append(FILE_PATH_LITERAL("Articles")));
scoped_ptr<DomDistillerStore> dom_distiller_store(
new DomDistillerStore(db.Pass(), database_dir));
scoped_ptr<DistillerPageFactory> distiller_page_factory(
new DistillerPageFactoryIOS(browser_state));
scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory(
new DistillerURLFetcherFactory(browser_state->GetRequestContext()));
dom_distiller::proto::DomDistillerOptions options;
scoped_ptr<DistillerFactory> distiller_factory(
new DistillerFactoryImpl(distiller_url_fetcher_factory.Pass(), options));
scoped_ptr<DistilledPagePrefs> distilled_page_prefs(new DistilledPagePrefs(
ios::ChromeBrowserState::FromBrowserState(browser_state)->GetPrefs()));
DomDistillerKeyedService* service =
new DomDistillerKeyedService(
dom_distiller_store.Pass(), distiller_factory.Pass(),
distiller_page_factory.Pass(), distilled_page_prefs.Pass());
return service;
}
web::BrowserState* DomDistillerServiceFactory::GetBrowserStateToUse(
web::BrowserState* browser_state) const {
// Makes normal profile and off-the-record profile use same service instance.
return GetBrowserStateRedirectedInIncognito(browser_state);
}
} // namespace dom_distiller
// Copyright 2015 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.
#ifndef IOS_CHROME_BROWSER_DOM_DISTILLER_DOM_DISTILLER_SERVICE_FACTORY_H_
#define IOS_CHROME_BROWSER_DOM_DISTILLER_DOM_DISTILLER_SERVICE_FACTORY_H_
#include "components/dom_distiller/core/distilled_page_prefs.h"
#include "components/keyed_service/ios/browser_state_keyed_service_factory.h"
template <typename T> struct DefaultSingletonTraits;
class KeyedService;
namespace dom_distiller {
class DomDistillerService;
}
namespace web {
class BrowserState;
}
namespace dom_distiller {
class DomDistillerServiceFactory : public BrowserStateKeyedServiceFactory {
public:
static DomDistillerServiceFactory* GetInstance();
static DomDistillerService* GetForBrowserState(
web::BrowserState* browser_state);
private:
friend struct DefaultSingletonTraits<DomDistillerServiceFactory>;
DomDistillerServiceFactory();
~DomDistillerServiceFactory() override;
// BrowserStateKeyedServiceFactory implementation.
KeyedService* BuildServiceInstanceFor(
web::BrowserState* browser_state) const override;
web::BrowserState* GetBrowserStateToUse(
web::BrowserState* browser_state) const override;
};
} // namespace dom_distiller
#endif // IOS_CHROME_BROWSER_DOM_DISTILLER_DOM_DISTILLER_SERVICE_FACTORY_H_
......@@ -15,6 +15,8 @@
],
'dependencies': [
'../../base/base.gyp:base',
'../../components/components.gyp:dom_distiller_core',
'../../components/components.gyp:dom_distiller_ios',
'../../components/components.gyp:infobars_core',
'../../components/components.gyp:keyed_service_core',
'../../components/components.gyp:keyed_service_ios',
......@@ -54,6 +56,10 @@
'browser/browser_state/browser_state_otr_helper.h',
'browser/chrome_url_constants.cc',
'browser/chrome_url_constants.h',
'browser/dom_distiller/distiller_viewer.cc',
'browser/dom_distiller/distiller_viewer.h',
'browser/dom_distiller/dom_distiller_service_factory.cc',
'browser/dom_distiller/dom_distiller_service_factory.h',
'browser/infobars/confirm_infobar_controller.h',
'browser/infobars/confirm_infobar_controller.mm',
'browser/infobars/infobar.h',
......@@ -101,12 +107,12 @@
'browser/translate/translate_service_ios.h',
'browser/ui/animation_util.h',
'browser/ui/animation_util.mm',
'browser/ui/commands/UIKit+ChromeExecuteCommand.h',
'browser/ui/commands/UIKit+ChromeExecuteCommand.mm',
'browser/ui/commands/generic_chrome_command.h',
'browser/ui/commands/generic_chrome_command.mm',
'browser/ui/commands/open_url_command.h',
'browser/ui/commands/open_url_command.mm',
'browser/ui/commands/UIKit+ChromeExecuteCommand.h',
'browser/ui/commands/UIKit+ChromeExecuteCommand.mm',
'browser/ui/image_util.h',
'browser/ui/image_util.mm',
'browser/ui/reversed_animation.h',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment