OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/dom_distiller/content/distillable_page_utils.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "components/dom_distiller/core/distillable_page_detector.h" |
| 12 #include "components/dom_distiller/core/page_features.h" |
| 13 #include "content/public/browser/render_frame_host.h" |
| 14 #include "grit/components_resources.h" |
| 15 #include "ui/base/resource/resource_bundle.h" |
| 16 |
| 17 namespace dom_distiller { |
| 18 namespace { |
| 19 void OnOGArticleJsResult(base::Callback<void(bool)> callback, |
| 20 const base::Value* result) { |
| 21 bool success = false; |
| 22 if (result) { |
| 23 result->GetAsBoolean(&success); |
| 24 } |
| 25 callback.Run(success); |
| 26 } |
| 27 |
| 28 void OnExtractFeaturesJsResult(const DistillablePageDetector* detector, |
| 29 base::Callback<void(bool)> callback, |
| 30 const base::Value* result) { |
| 31 callback.Run(detector->Classify(CalculateDerivedFeaturesFromJSON(result))); |
| 32 } |
| 33 } // namespace |
| 34 |
| 35 void IsOpenGraphArticle(content::WebContents* web_contents, |
| 36 base::Callback<void(bool)> callback) { |
| 37 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
| 38 if (!main_frame) { |
| 39 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 40 base::Bind(callback, false)); |
| 41 return; |
| 42 } |
| 43 std::string og_article_js = ResourceBundle::GetSharedInstance() |
| 44 .GetRawDataResource(IDR_IS_DISTILLABLE_JS) |
| 45 .as_string(); |
| 46 main_frame->ExecuteJavaScript(base::UTF8ToUTF16(og_article_js), |
| 47 base::Bind(OnOGArticleJsResult, callback)); |
| 48 } |
| 49 |
| 50 void IsDistillablePage(content::WebContents* web_contents, |
| 51 base::Callback<void(bool)> callback) { |
| 52 IsDistillablePageForDetector(web_contents, |
| 53 DistillablePageDetector::GetDefault(), callback); |
| 54 } |
| 55 |
| 56 void IsDistillablePageForDetector(content::WebContents* web_contents, |
| 57 const DistillablePageDetector* detector, |
| 58 base::Callback<void(bool)> callback) { |
| 59 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
| 60 if (!main_frame) { |
| 61 base::MessageLoop::current()->PostTask(FROM_HERE, |
| 62 base::Bind(callback, false)); |
| 63 return; |
| 64 } |
| 65 std::string extract_features_js = |
| 66 ResourceBundle::GetSharedInstance() |
| 67 .GetRawDataResource(IDR_EXTRACT_PAGE_FEATURES_JS) |
| 68 .as_string(); |
| 69 main_frame->ExecuteJavaScript( |
| 70 base::UTF8ToUTF16(extract_features_js), |
| 71 base::Bind(OnExtractFeaturesJsResult, detector, callback)); |
| 72 } |
| 73 |
| 74 } // namespace dom_distiller |
OLD | NEW |