| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/dom_distiller/content/browser/distillable_page_utils.h" | 5 #include "components/dom_distiller/content/browser/distillable_page_utils.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" | 9 #include "base/single_thread_task_runner.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/thread_task_runner_handle.h" | 11 #include "base/thread_task_runner_handle.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "components/dom_distiller/content/browser/distillability_driver.h" |
| 13 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" | 14 #include "components/dom_distiller/content/browser/distiller_javascript_utils.h" |
| 15 #include "components/dom_distiller/content/common/distiller_messages.h" |
| 14 #include "components/dom_distiller/core/distillable_page_detector.h" | 16 #include "components/dom_distiller/core/distillable_page_detector.h" |
| 15 #include "components/dom_distiller/core/experiments.h" | 17 #include "components/dom_distiller/core/experiments.h" |
| 16 #include "components/dom_distiller/core/page_features.h" | 18 #include "components/dom_distiller/core/page_features.h" |
| 17 #include "content/public/browser/render_frame_host.h" | 19 #include "content/public/browser/render_frame_host.h" |
| 18 #include "grit/components_resources.h" | 20 #include "grit/components_resources.h" |
| 19 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
| 20 | 22 |
| 21 namespace dom_distiller { | 23 namespace dom_distiller { |
| 22 namespace { | |
| 23 void OnOGArticleJsResult(base::Callback<void(bool)> callback, | |
| 24 const base::Value* result) { | |
| 25 bool success = false; | |
| 26 if (result) { | |
| 27 result->GetAsBoolean(&success); | |
| 28 } | |
| 29 callback.Run(success); | |
| 30 } | |
| 31 | 24 |
| 32 void OnExtractFeaturesJsResult(const DistillablePageDetector* detector, | 25 void setCallback(content::WebContents* web_contents, |
| 33 base::Callback<void(bool)> callback, | 26 base::Callback<void(bool, bool)> callback) { |
| 34 const base::Value* result) { | 27 WebContentsDistillabilityDriver::CreateForWebContents(web_contents); |
| 35 callback.Run(detector->Classify(CalculateDerivedFeaturesFromJSON(result))); | |
| 36 } | |
| 37 } // namespace | |
| 38 | 28 |
| 39 void IsOpenGraphArticle(content::WebContents* web_contents, | 29 WebContentsDistillabilityDriver *driver = |
| 40 base::Callback<void(bool)> callback) { | 30 WebContentsDistillabilityDriver::FromWebContents(web_contents); |
| 41 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); | 31 CHECK(driver); |
| 42 if (!main_frame) { | 32 driver->SetCallback(callback); |
| 43 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 44 base::Bind(callback, false)); | |
| 45 return; | |
| 46 } | |
| 47 std::string og_article_js = ResourceBundle::GetSharedInstance() | |
| 48 .GetRawDataResource(IDR_IS_DISTILLABLE_JS) | |
| 49 .as_string(); | |
| 50 RunIsolatedJavaScript(main_frame, og_article_js, | |
| 51 base::Bind(OnOGArticleJsResult, callback)); | |
| 52 } | |
| 53 | |
| 54 void IsDistillablePage(content::WebContents* web_contents, | |
| 55 bool is_mobile_optimized, | |
| 56 base::Callback<void(bool)> callback) { | |
| 57 switch (GetDistillerHeuristicsType()) { | |
| 58 case DistillerHeuristicsType::ALWAYS_TRUE: | |
| 59 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 60 base::Bind(callback, true)); | |
| 61 return; | |
| 62 case DistillerHeuristicsType::OG_ARTICLE: | |
| 63 IsOpenGraphArticle(web_contents, callback); | |
| 64 return; | |
| 65 case DistillerHeuristicsType::ADABOOST_MODEL: | |
| 66 // The adaboost model is only applied to non-mobile pages. | |
| 67 if (is_mobile_optimized) { | |
| 68 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 69 FROM_HERE, base::Bind(callback, false)); | |
| 70 return; | |
| 71 } | |
| 72 IsDistillablePageForDetector( | |
| 73 web_contents, DistillablePageDetector::GetDefault(), callback); | |
| 74 return; | |
| 75 case DistillerHeuristicsType::NONE: | |
| 76 default: | |
| 77 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 78 FROM_HERE, base::Bind(callback, false)); | |
| 79 return; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 void IsDistillablePageForDetector(content::WebContents* web_contents, | |
| 84 const DistillablePageDetector* detector, | |
| 85 base::Callback<void(bool)> callback) { | |
| 86 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); | |
| 87 if (!main_frame) { | |
| 88 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | |
| 89 base::Bind(callback, false)); | |
| 90 return; | |
| 91 } | |
| 92 std::string extract_features_js = | |
| 93 ResourceBundle::GetSharedInstance() | |
| 94 .GetRawDataResource(IDR_EXTRACT_PAGE_FEATURES_JS) | |
| 95 .as_string(); | |
| 96 RunIsolatedJavaScript( | |
| 97 main_frame, extract_features_js, | |
| 98 base::Bind(OnExtractFeaturesJsResult, detector, callback)); | |
| 99 } | 33 } |
| 100 | 34 |
| 101 } // namespace dom_distiller | 35 } // namespace dom_distiller |
| OLD | NEW |