| 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/distillable_page_utils.h" | 5 #include "components/dom_distiller/content/distillable_page_utils.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/location.h" |
| 9 #include "base/single_thread_task_runner.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "base/thread_task_runner_handle.h" |
| 10 #include "base/values.h" | 12 #include "base/values.h" |
| 11 #include "components/dom_distiller/core/distillable_page_detector.h" | 13 #include "components/dom_distiller/core/distillable_page_detector.h" |
| 12 #include "components/dom_distiller/core/experiments.h" | 14 #include "components/dom_distiller/core/experiments.h" |
| 13 #include "components/dom_distiller/core/page_features.h" | 15 #include "components/dom_distiller/core/page_features.h" |
| 14 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
| 15 #include "grit/components_resources.h" | 17 #include "grit/components_resources.h" |
| 16 #include "ui/base/resource/resource_bundle.h" | 18 #include "ui/base/resource/resource_bundle.h" |
| 17 | 19 |
| 18 namespace dom_distiller { | 20 namespace dom_distiller { |
| 19 namespace { | 21 namespace { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 30 base::Callback<void(bool)> callback, | 32 base::Callback<void(bool)> callback, |
| 31 const base::Value* result) { | 33 const base::Value* result) { |
| 32 callback.Run(detector->Classify(CalculateDerivedFeaturesFromJSON(result))); | 34 callback.Run(detector->Classify(CalculateDerivedFeaturesFromJSON(result))); |
| 33 } | 35 } |
| 34 } // namespace | 36 } // namespace |
| 35 | 37 |
| 36 void IsOpenGraphArticle(content::WebContents* web_contents, | 38 void IsOpenGraphArticle(content::WebContents* web_contents, |
| 37 base::Callback<void(bool)> callback) { | 39 base::Callback<void(bool)> callback) { |
| 38 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); | 40 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
| 39 if (!main_frame) { | 41 if (!main_frame) { |
| 40 base::MessageLoop::current()->PostTask(FROM_HERE, | 42 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 41 base::Bind(callback, false)); | 43 base::Bind(callback, false)); |
| 42 return; | 44 return; |
| 43 } | 45 } |
| 44 std::string og_article_js = ResourceBundle::GetSharedInstance() | 46 std::string og_article_js = ResourceBundle::GetSharedInstance() |
| 45 .GetRawDataResource(IDR_IS_DISTILLABLE_JS) | 47 .GetRawDataResource(IDR_IS_DISTILLABLE_JS) |
| 46 .as_string(); | 48 .as_string(); |
| 47 main_frame->ExecuteJavaScript(base::UTF8ToUTF16(og_article_js), | 49 main_frame->ExecuteJavaScript(base::UTF8ToUTF16(og_article_js), |
| 48 base::Bind(OnOGArticleJsResult, callback)); | 50 base::Bind(OnOGArticleJsResult, callback)); |
| 49 } | 51 } |
| 50 | 52 |
| 51 void IsDistillablePage(content::WebContents* web_contents, | 53 void IsDistillablePage(content::WebContents* web_contents, |
| 52 bool is_mobile_optimized, | 54 bool is_mobile_optimized, |
| 53 base::Callback<void(bool)> callback) { | 55 base::Callback<void(bool)> callback) { |
| 54 switch (GetDistillerHeuristicsType()) { | 56 switch (GetDistillerHeuristicsType()) { |
| 55 case DistillerHeuristicsType::ALWAYS_TRUE: | 57 case DistillerHeuristicsType::ALWAYS_TRUE: |
| 56 base::MessageLoop::current()->PostTask(FROM_HERE, | 58 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 57 base::Bind(callback, true)); | 59 base::Bind(callback, true)); |
| 58 return; | 60 return; |
| 59 case DistillerHeuristicsType::OG_ARTICLE: | 61 case DistillerHeuristicsType::OG_ARTICLE: |
| 60 IsOpenGraphArticle(web_contents, callback); | 62 IsOpenGraphArticle(web_contents, callback); |
| 61 return; | 63 return; |
| 62 case DistillerHeuristicsType::ADABOOST_MODEL: | 64 case DistillerHeuristicsType::ADABOOST_MODEL: |
| 63 // The adaboost model is only applied to non-mobile pages. | 65 // The adaboost model is only applied to non-mobile pages. |
| 64 if (is_mobile_optimized) { | 66 if (is_mobile_optimized) { |
| 65 base::MessageLoop::current()->PostTask(FROM_HERE, | 67 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 66 base::Bind(callback, false)); | 68 FROM_HERE, base::Bind(callback, false)); |
| 67 return; | 69 return; |
| 68 } | 70 } |
| 69 IsDistillablePageForDetector( | 71 IsDistillablePageForDetector( |
| 70 web_contents, DistillablePageDetector::GetDefault(), callback); | 72 web_contents, DistillablePageDetector::GetDefault(), callback); |
| 71 return; | 73 return; |
| 72 case DistillerHeuristicsType::NONE: | 74 case DistillerHeuristicsType::NONE: |
| 73 default: | 75 default: |
| 74 base::MessageLoop::current()->PostTask(FROM_HERE, | 76 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 75 base::Bind(callback, false)); | 77 FROM_HERE, base::Bind(callback, false)); |
| 76 return; | 78 return; |
| 77 } | 79 } |
| 78 } | 80 } |
| 79 | 81 |
| 80 void IsDistillablePageForDetector(content::WebContents* web_contents, | 82 void IsDistillablePageForDetector(content::WebContents* web_contents, |
| 81 const DistillablePageDetector* detector, | 83 const DistillablePageDetector* detector, |
| 82 base::Callback<void(bool)> callback) { | 84 base::Callback<void(bool)> callback) { |
| 83 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); | 85 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
| 84 if (!main_frame) { | 86 if (!main_frame) { |
| 85 base::MessageLoop::current()->PostTask(FROM_HERE, | 87 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 86 base::Bind(callback, false)); | 88 base::Bind(callback, false)); |
| 87 return; | 89 return; |
| 88 } | 90 } |
| 89 std::string extract_features_js = | 91 std::string extract_features_js = |
| 90 ResourceBundle::GetSharedInstance() | 92 ResourceBundle::GetSharedInstance() |
| 91 .GetRawDataResource(IDR_EXTRACT_PAGE_FEATURES_JS) | 93 .GetRawDataResource(IDR_EXTRACT_PAGE_FEATURES_JS) |
| 92 .as_string(); | 94 .as_string(); |
| 93 main_frame->ExecuteJavaScript( | 95 main_frame->ExecuteJavaScript( |
| 94 base::UTF8ToUTF16(extract_features_js), | 96 base::UTF8ToUTF16(extract_features_js), |
| 95 base::Bind(OnExtractFeaturesJsResult, detector, callback)); | 97 base::Bind(OnExtractFeaturesJsResult, detector, callback)); |
| 96 } | 98 } |
| 97 | 99 |
| 98 } // namespace dom_distiller | 100 } // namespace dom_distiller |
| OLD | NEW |