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/common/distiller_messages.h" | |
6 #include "components/dom_distiller/content/renderer/distillability_agent.h" | |
7 #include "components/dom_distiller/core/distillable_page_detector.h" | |
8 #include "components/dom_distiller/core/experiments.h" | |
9 #include "components/dom_distiller/core/page_features.h" | |
10 #include "content/public/renderer/render_frame.h" | |
11 | |
12 #include "third_party/WebKit/public/platform/WebDistillability.h" | |
13 #include "third_party/WebKit/public/web/WebDocument.h" | |
14 #include "third_party/WebKit/public/web/WebElement.h" | |
15 #include "third_party/WebKit/public/web/WebLocalFrame.h" | |
16 #include "third_party/WebKit/public/web/WebNode.h" | |
17 #include "third_party/WebKit/public/web/WebNodeList.h" | |
18 | |
19 namespace dom_distiller { | |
20 | |
21 using namespace blink; | |
22 | |
23 namespace { | |
24 | |
25 bool needToUpdate(bool is_loaded) { | |
26 switch (GetDistillerHeuristicsType()) { | |
27 case DistillerHeuristicsType::ALWAYS_TRUE: | |
28 return !is_loaded; | |
29 case DistillerHeuristicsType::OG_ARTICLE: | |
30 return !is_loaded; | |
31 case DistillerHeuristicsType::ADABOOST_MODEL: | |
32 return true; | |
33 case DistillerHeuristicsType::NONE: | |
34 default: | |
35 return false; | |
36 } | |
37 } | |
38 | |
39 bool isLast(bool is_loaded) { | |
mdjones
2015/11/03 02:54:45
Please add some doc for these helpers.
wychen
2015/11/03 07:56:39
Done.
| |
40 switch (GetDistillerHeuristicsType()) { | |
41 case DistillerHeuristicsType::ALWAYS_TRUE: | |
42 return true; | |
43 case DistillerHeuristicsType::OG_ARTICLE: | |
44 return true; | |
45 case DistillerHeuristicsType::ADABOOST_MODEL: | |
46 return is_loaded; | |
47 case DistillerHeuristicsType::NONE: | |
48 default: | |
49 return true; | |
50 } | |
51 } | |
52 | |
53 bool IsDistillablePageAdaboost(WebDocument& doc, | |
54 const DistillablePageDetector* detector) { | |
55 WebDistillabilityFeatures features = doc.distillabilityFeatures(); | |
56 GURL parsed_url(doc.url()); | |
57 if (!parsed_url.is_valid()) { | |
58 return false; | |
59 } | |
60 return detector->Classify(CalculateDerivedFeatures(features, parsed_url)); | |
61 } | |
62 | |
63 bool IsDistillablePage(bool is_mobile_optimized, WebDocument& doc) { | |
64 switch (GetDistillerHeuristicsType()) { | |
65 case DistillerHeuristicsType::ALWAYS_TRUE: | |
66 return true; | |
67 case DistillerHeuristicsType::OG_ARTICLE: | |
68 return doc.distillabilityFeatures().openGraph; | |
69 case DistillerHeuristicsType::ADABOOST_MODEL: | |
70 // The adaboost model is only applied to non-mobile pages. | |
71 if (is_mobile_optimized) { | |
72 return false; | |
73 } | |
74 return IsDistillablePageAdaboost( | |
75 doc, DistillablePageDetector::GetDefault()); | |
76 case DistillerHeuristicsType::NONE: | |
77 default: | |
78 return false; | |
79 } | |
80 } | |
81 | |
82 } // namespace | |
83 | |
84 DistillabilityAgent::DistillabilityAgent( | |
85 content::RenderFrame* render_frame) | |
86 : RenderFrameObserver(render_frame) { | |
87 } | |
88 | |
89 void DistillabilityAgent::DidMeaningfulLayout( | |
90 WebMeaningfulLayout layout_type) { | |
91 if (layout_type != WebMeaningfulLayout::FinishedParsing && | |
92 layout_type != WebMeaningfulLayout::FinishedLoading) { | |
93 return; | |
94 } | |
95 | |
96 DCHECK(render_frame()); | |
97 if (!render_frame()->IsMainFrame()) return; | |
98 DCHECK(render_frame()->GetWebFrame()); | |
99 WebDocument doc = render_frame()->GetWebFrame()->document(); | |
100 if (doc.isNull() || doc.body().isNull()) return; | |
101 | |
102 bool is_loaded = layout_type == WebMeaningfulLayout::FinishedLoading; | |
103 if (!needToUpdate(is_loaded)) return; | |
104 | |
105 bool is_mobile_friendly = doc.isMobileFriendly(); | |
106 Send(new FrameHostMsg_Distillability(routing_id(), | |
107 IsDistillablePage(is_mobile_friendly, doc), isLast(is_loaded))); | |
108 } | |
109 | |
110 | |
111 DistillabilityAgent::~DistillabilityAgent() {} | |
112 | |
113 } // namespace dom_distiller | |
OLD | NEW |