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.
5 #include "components/dom_distiller/content/distillable_page_utils.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/experiments.h"
13 #include "components/dom_distiller/core/page_features.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "grit/components_resources.h"
16 #include "ui/base/resource/resource_bundle.h"
18 namespace dom_distiller
{
20 void OnOGArticleJsResult(base::Callback
<void(bool)> callback
,
21 const base::Value
* result
) {
24 result
->GetAsBoolean(&success
);
26 callback
.Run(success
);
29 void OnExtractFeaturesJsResult(const DistillablePageDetector
* detector
,
30 base::Callback
<void(bool)> callback
,
31 const base::Value
* result
) {
32 callback
.Run(detector
->Classify(CalculateDerivedFeaturesFromJSON(result
)));
36 void IsOpenGraphArticle(content::WebContents
* web_contents
,
37 base::Callback
<void(bool)> callback
) {
38 content::RenderFrameHost
* main_frame
= web_contents
->GetMainFrame();
40 base::MessageLoop::current()->PostTask(FROM_HERE
,
41 base::Bind(callback
, false));
44 std::string og_article_js
= ResourceBundle::GetSharedInstance()
45 .GetRawDataResource(IDR_IS_DISTILLABLE_JS
)
47 main_frame
->ExecuteJavaScript(base::UTF8ToUTF16(og_article_js
),
48 base::Bind(OnOGArticleJsResult
, callback
));
51 void IsDistillablePage(content::WebContents
* web_contents
,
52 bool is_mobile_optimized
,
53 base::Callback
<void(bool)> callback
) {
54 switch (GetDistillerHeuristicsType()) {
55 case DistillerHeuristicsType::ALWAYS_TRUE
:
56 base::MessageLoop::current()->PostTask(FROM_HERE
,
57 base::Bind(callback
, true));
59 case DistillerHeuristicsType::OG_ARTICLE
:
60 IsOpenGraphArticle(web_contents
, callback
);
62 case DistillerHeuristicsType::ADABOOST_MODEL
:
63 // The adaboost model is only applied to non-mobile pages.
64 if (is_mobile_optimized
) {
65 base::MessageLoop::current()->PostTask(FROM_HERE
,
66 base::Bind(callback
, false));
69 IsDistillablePageForDetector(
70 web_contents
, DistillablePageDetector::GetDefault(), callback
);
72 case DistillerHeuristicsType::NONE
:
74 base::MessageLoop::current()->PostTask(FROM_HERE
,
75 base::Bind(callback
, false));
80 void IsDistillablePageForDetector(content::WebContents
* web_contents
,
81 const DistillablePageDetector
* detector
,
82 base::Callback
<void(bool)> callback
) {
83 content::RenderFrameHost
* main_frame
= web_contents
->GetMainFrame();
85 base::MessageLoop::current()->PostTask(FROM_HERE
,
86 base::Bind(callback
, false));
89 std::string extract_features_js
=
90 ResourceBundle::GetSharedInstance()
91 .GetRawDataResource(IDR_EXTRACT_PAGE_FEATURES_JS
)
93 main_frame
->ExecuteJavaScript(
94 base::UTF8ToUTF16(extract_features_js
),
95 base::Bind(OnExtractFeaturesJsResult
, detector
, callback
));
98 } // namespace dom_distiller