Chromium Code Reviews| 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 { | 24 namespace { |
| 25 | |
| 23 void OnOGArticleJsResult(base::Callback<void(bool)> callback, | 26 void OnOGArticleJsResult(base::Callback<void(bool)> callback, |
| 24 const base::Value* result) { | 27 const base::Value* result) { |
| 25 bool success = false; | 28 bool success = false; |
| 26 if (result) { | 29 if (result) { |
| 27 result->GetAsBoolean(&success); | 30 result->GetAsBoolean(&success); |
| 28 } | 31 } |
| 29 callback.Run(success); | 32 callback.Run(success); |
| 30 } | 33 } |
| 31 | 34 |
| 35 #if defined(OS_IOS) | |
|
mdjones
2015/09/29 17:31:42
I don't think we need this for code in the dom_dis
wychen
2015/09/29 21:46:57
Done.
| |
| 32 void OnExtractFeaturesJsResult(const DistillablePageDetector* detector, | 36 void OnExtractFeaturesJsResult(const DistillablePageDetector* detector, |
| 33 base::Callback<void(bool)> callback, | 37 base::Callback<void(bool)> callback, |
| 34 const base::Value* result) { | 38 const base::Value* result) { |
| 35 callback.Run(detector->Classify(CalculateDerivedFeaturesFromJSON(result))); | 39 callback.Run(detector->Classify(CalculateDerivedFeaturesFromJSON(result))); |
| 36 } | 40 } |
| 41 #else // defined(OS_IOS) | |
| 42 void OnExtractFeaturesNativeResult( | |
| 43 const DistillablePageDetector* detector, | |
| 44 base::Callback<void(bool)> callback, | |
| 45 bool isOGArticle, | |
| 46 const std::string& url, | |
| 47 int numElements, | |
| 48 int numAnchors, | |
| 49 int numForms, | |
| 50 const std::string& innerText, | |
| 51 const std::string& textContent, | |
| 52 const std::string& innerHTML) { | |
| 53 | |
| 54 GURL parsed_url(url); | |
| 55 if (!parsed_url.is_valid()) { | |
| 56 callback.Run(false); | |
| 57 return; | |
| 58 } | |
| 59 | |
| 60 auto res = CalculateDerivedFeatures( | |
| 61 isOGArticle, | |
| 62 parsed_url, | |
| 63 numElements, | |
| 64 numAnchors, | |
| 65 numForms, | |
| 66 innerText, | |
| 67 textContent, | |
| 68 innerHTML | |
| 69 ); | |
| 70 callback.Run(detector->Classify(res)); | |
| 71 } | |
| 72 #endif // defined(OS_IOS) | |
| 73 | |
| 37 } // namespace | 74 } // namespace |
| 38 | 75 |
| 39 void IsOpenGraphArticle(content::WebContents* web_contents, | 76 void IsOpenGraphArticle(content::WebContents* web_contents, |
| 40 base::Callback<void(bool)> callback) { | 77 base::Callback<void(bool)> callback) { |
| 41 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); | 78 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
| 42 if (!main_frame) { | 79 if (!main_frame) { |
| 43 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 80 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 44 base::Bind(callback, false)); | 81 base::Bind(callback, false)); |
| 45 return; | 82 return; |
| 46 } | 83 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 | 119 |
| 83 void IsDistillablePageForDetector(content::WebContents* web_contents, | 120 void IsDistillablePageForDetector(content::WebContents* web_contents, |
| 84 const DistillablePageDetector* detector, | 121 const DistillablePageDetector* detector, |
| 85 base::Callback<void(bool)> callback) { | 122 base::Callback<void(bool)> callback) { |
| 86 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); | 123 content::RenderFrameHost* main_frame = web_contents->GetMainFrame(); |
| 87 if (!main_frame) { | 124 if (!main_frame) { |
| 88 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 125 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 89 base::Bind(callback, false)); | 126 base::Bind(callback, false)); |
| 90 return; | 127 return; |
| 91 } | 128 } |
| 129 | |
| 130 #if defined(OS_IOS) | |
|
mdjones
2015/09/29 17:31:42
Same as above (not needed for content).
wychen
2015/09/29 21:46:57
Done.
| |
| 92 std::string extract_features_js = | 131 std::string extract_features_js = |
| 93 ResourceBundle::GetSharedInstance() | 132 ResourceBundle::GetSharedInstance() |
| 94 .GetRawDataResource(IDR_EXTRACT_PAGE_FEATURES_JS) | 133 .GetRawDataResource(IDR_EXTRACT_PAGE_FEATURES_JS) |
| 95 .as_string(); | 134 .as_string(); |
| 96 RunIsolatedJavaScript( | 135 RunIsolatedJavaScript( |
| 97 main_frame, extract_features_js, | 136 main_frame, extract_features_js, |
| 98 base::Bind(OnExtractFeaturesJsResult, detector, callback)); | 137 base::Bind(OnExtractFeaturesJsResult, detector, callback)); |
| 138 #else | |
| 139 WebContentsDistillabilityDriver::CreateForWebContents(web_contents); | |
| 140 WebContentsDistillabilityDriver *driver = | |
| 141 WebContentsDistillabilityDriver::FromWebContents(web_contents); | |
| 142 CHECK(driver); | |
| 143 driver->ExtractFeatures( | |
| 144 base::Bind(OnExtractFeaturesNativeResult, detector, callback)); | |
| 145 #endif | |
| 99 } | 146 } |
| 100 | 147 |
| 101 } // namespace dom_distiller | 148 } // namespace dom_distiller |
| OLD | NEW |