Switch TestFrameNavigationObserver to DidCommitProvisionalLoadForFrame.
[chromium-blink-merge.git] / components / dom_distiller / content / distillable_page_utils.cc
blob5d1519c7a6ee4f07ca1985baa20d156e13d750f8
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 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));
58 return;
59 case DistillerHeuristicsType::OG_ARTICLE:
60 IsOpenGraphArticle(web_contents, callback);
61 return;
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));
67 return;
69 IsDistillablePageForDetector(
70 web_contents, DistillablePageDetector::GetDefault(), callback);
71 return;
72 case DistillerHeuristicsType::NONE:
73 default:
74 base::MessageLoop::current()->PostTask(FROM_HERE,
75 base::Bind(callback, false));
76 return;
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();
84 if (!main_frame) {
85 base::MessageLoop::current()->PostTask(FROM_HERE,
86 base::Bind(callback, false));
87 return;
89 std::string extract_features_js =
90 ResourceBundle::GetSharedInstance()
91 .GetRawDataResource(IDR_EXTRACT_PAGE_FEATURES_JS)
92 .as_string();
93 main_frame->ExecuteJavaScript(
94 base::UTF8ToUTF16(extract_features_js),
95 base::Bind(OnExtractFeaturesJsResult, detector, callback));
98 } // namespace dom_distiller