[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / components / dom_distiller / content / distillable_page_utils.cc
blob520299223ce033a73461fcf4617e3245cad8be6f
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"
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/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 {
19 namespace {
20 void OnOGArticleJsResult(base::Callback<void(bool)> callback,
21 const base::Value* result) {
22 bool success = false;
23 if (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)));
34 } // namespace
36 void IsOpenGraphArticle(content::WebContents* web_contents,
37 base::Callback<void(bool)> callback) {
38 content::RenderFrameHost* main_frame = web_contents->GetMainFrame();
39 if (!main_frame) {
40 base::MessageLoop::current()->PostTask(FROM_HERE,
41 base::Bind(callback, false));
42 return;
44 std::string og_article_js = ResourceBundle::GetSharedInstance()
45 .GetRawDataResource(IDR_IS_DISTILLABLE_JS)
46 .as_string();
47 main_frame->ExecuteJavaScript(base::UTF8ToUTF16(og_article_js),
48 base::Bind(OnOGArticleJsResult, callback));
51 void IsDistillablePage(content::WebContents* web_contents,
52 base::Callback<void(bool)> callback) {
53 switch (GetDistillerHeuristicsType()) {
54 case DistillerHeuristicsType::NONE:
55 base::MessageLoop::current()->PostTask(FROM_HERE,
56 base::Bind(callback, false));
57 return;
58 case DistillerHeuristicsType::OG_ARTICLE:
59 IsOpenGraphArticle(web_contents, callback);
60 return;
61 case DistillerHeuristicsType::ADABOOST_MODEL:
62 IsDistillablePageForDetector(
63 web_contents, DistillablePageDetector::GetDefault(), callback);
64 return;
68 void IsDistillablePageForDetector(content::WebContents* web_contents,
69 const DistillablePageDetector* detector,
70 base::Callback<void(bool)> callback) {
71 content::RenderFrameHost* main_frame = web_contents->GetMainFrame();
72 if (!main_frame) {
73 base::MessageLoop::current()->PostTask(FROM_HERE,
74 base::Bind(callback, false));
75 return;
77 std::string extract_features_js =
78 ResourceBundle::GetSharedInstance()
79 .GetRawDataResource(IDR_EXTRACT_PAGE_FEATURES_JS)
80 .as_string();
81 main_frame->ExecuteJavaScript(
82 base::UTF8ToUTF16(extract_features_js),
83 base::Bind(OnExtractFeaturesJsResult, detector, callback));
86 } // namespace dom_distiller