Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / standalone / content_extractor.cc
index 6f16b01..760b165 100644 (file)
@@ -30,8 +30,19 @@ namespace dom_distiller {
 
 namespace {
 
+// The url to distill.
 const char* kUrlSwitch = "url";
 
+// Indicates that DNS resolution should be disabled for this test.
+const char* kDisableDnsSwitch = "disable-dns";
+
+// Will write the distilled output to the given file instead of to stdout.
+const char* kOutputFile = "output-file";
+
+// Indicates to output a serialized protocol buffer instead of human-readable
+// output.
+const char* kShouldOutputBinary = "output-binary";
+
 scoped_ptr<DomDistillerService> CreateDomDistillerService(
     content::BrowserContext* context,
     const base::FilePath& db_path) {
@@ -50,12 +61,13 @@ scoped_ptr<DomDistillerService> CreateDomDistillerService(
       new DistillerPageWebContentsFactory(context));
   scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory(
       new DistillerURLFetcherFactory(context->GetRequestContext()));
-  scoped_ptr<DistillerFactory> distiller_factory(new DistillerFactoryImpl(
-      distiller_page_factory.Pass(), distiller_url_fetcher_factory.Pass()));
+  scoped_ptr<DistillerFactory> distiller_factory(
+      new DistillerFactoryImpl(distiller_url_fetcher_factory.Pass()));
 
   return scoped_ptr<DomDistillerService>(new DomDistillerService(
       dom_distiller_store.PassAs<DomDistillerStoreInterface>(),
-      distiller_factory.Pass()));
+      distiller_factory.Pass(),
+      distiller_page_factory.Pass()));
 }
 
 void AddComponentsResources() {
@@ -69,15 +81,27 @@ void AddComponentsResources() {
 
 void LogArticle(const DistilledArticleProto& article_proto) {
   std::stringstream output;
-  output << "Article Title: " << article_proto.title() << std::endl;
-  output << "# of pages: " << article_proto.pages_size() << std::endl;
-  for (int i = 0; i < article_proto.pages_size(); ++i) {
-    const DistilledPageProto& page = article_proto.pages(i);
-    output << "Page " << i << std::endl;
-    output << "URL: " << page.url() << std::endl;
-    output << "Content: " << page.html() << std::endl;
+  if (CommandLine::ForCurrentProcess()->HasSwitch(kShouldOutputBinary)) {
+    output << article_proto.SerializeAsString();
+  } else {
+    output << "Article Title: " << article_proto.title() << std::endl;
+    output << "# of pages: " << article_proto.pages_size() << std::endl;
+    for (int i = 0; i < article_proto.pages_size(); ++i) {
+      const DistilledPageProto& page = article_proto.pages(i);
+      output << "Page " << i << std::endl;
+      output << "URL: " << page.url() << std::endl;
+      output << "Content: " << page.html() << std::endl;
+    }
+  }
+
+  std::string data = output.str();
+  if (CommandLine::ForCurrentProcess()->HasSwitch(kOutputFile)) {
+    base::FilePath filename =
+        CommandLine::ForCurrentProcess()->GetSwitchValuePath(kOutputFile);
+    base::WriteFile(filename, data.c_str(), data.size());
+  } else {
+    VLOG(0) << data;
   }
-  VLOG(0) << output.str();
 }
 
 }  // namespace
@@ -86,7 +110,8 @@ class ContentExtractionRequest : public ViewRequestDelegate {
  public:
   void Start(DomDistillerService* service, base::Closure finished_callback) {
     finished_callback_ = finished_callback;
-    viewer_handle_ = service->ViewUrl(this, url_);
+    viewer_handle_ =
+        service->ViewUrl(this, service->CreateDefaultDistillerPage(), url_);
   }
 
   DistilledArticleProto GetArticleCopy() {
@@ -132,7 +157,9 @@ class ContentExtractor : public ContentBrowserTest {
   // Change behavior of the default host resolver to avoid DNS lookup errors, so
   // we can make network calls.
   virtual void SetUpOnMainThread() OVERRIDE {
-    EnableDNSLookupForThisTest();
+    if (!CommandLine::ForCurrentProcess()->HasSwitch(kDisableDnsSwitch)) {
+      EnableDNSLookupForThisTest();
+    }
     CHECK(db_dir_.CreateUniqueTempDir());
     AddComponentsResources();
   }