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 // Returns whether it is necessary to send updates back to the browser. |
| 26 // The number of updates can be from 0 to 2. See the tests in |
| 27 // "distillable_page_utils_browsertest.cc". |
| 28 // Most heuristics types only require one update after parsing. |
| 29 // Adaboost is the only one doing the second update, which is after loading. |
| 30 bool needToUpdate(bool is_loaded) { |
| 31 switch (GetDistillerHeuristicsType()) { |
| 32 case DistillerHeuristicsType::ALWAYS_TRUE: |
| 33 return !is_loaded; |
| 34 case DistillerHeuristicsType::OG_ARTICLE: |
| 35 return !is_loaded; |
| 36 case DistillerHeuristicsType::ADABOOST_MODEL: |
| 37 return true; |
| 38 case DistillerHeuristicsType::NONE: |
| 39 default: |
| 40 return false; |
| 41 } |
| 42 } |
| 43 |
| 44 // Returns whether this update is the last one for the page. |
| 45 bool isLast(bool is_loaded) { |
| 46 switch (GetDistillerHeuristicsType()) { |
| 47 case DistillerHeuristicsType::ALWAYS_TRUE: |
| 48 return true; |
| 49 case DistillerHeuristicsType::OG_ARTICLE: |
| 50 return true; |
| 51 case DistillerHeuristicsType::ADABOOST_MODEL: |
| 52 return is_loaded; |
| 53 case DistillerHeuristicsType::NONE: |
| 54 default: |
| 55 return true; |
| 56 } |
| 57 } |
| 58 |
| 59 bool IsDistillablePageAdaboost(WebDocument& doc, |
| 60 const DistillablePageDetector* detector) { |
| 61 WebDistillabilityFeatures features = doc.distillabilityFeatures(); |
| 62 GURL parsed_url(doc.url()); |
| 63 if (!parsed_url.is_valid()) { |
| 64 return false; |
| 65 } |
| 66 // The adaboost model is only applied to non-mobile pages. |
| 67 if (features.isMobileFriendly) { |
| 68 return false; |
| 69 } |
| 70 return detector->Classify(CalculateDerivedFeatures(features, parsed_url)); |
| 71 } |
| 72 |
| 73 bool IsDistillablePage(WebDocument& doc) { |
| 74 switch (GetDistillerHeuristicsType()) { |
| 75 case DistillerHeuristicsType::ALWAYS_TRUE: |
| 76 return true; |
| 77 case DistillerHeuristicsType::OG_ARTICLE: |
| 78 return doc.distillabilityFeatures().openGraph; |
| 79 case DistillerHeuristicsType::ADABOOST_MODEL: |
| 80 return IsDistillablePageAdaboost( |
| 81 doc, DistillablePageDetector::GetDefault()); |
| 82 case DistillerHeuristicsType::NONE: |
| 83 default: |
| 84 return false; |
| 85 } |
| 86 } |
| 87 |
| 88 } // namespace |
| 89 |
| 90 DistillabilityAgent::DistillabilityAgent( |
| 91 content::RenderFrame* render_frame) |
| 92 : RenderFrameObserver(render_frame) { |
| 93 } |
| 94 |
| 95 void DistillabilityAgent::DidMeaningfulLayout( |
| 96 WebMeaningfulLayout layout_type) { |
| 97 if (layout_type != WebMeaningfulLayout::FinishedParsing && |
| 98 layout_type != WebMeaningfulLayout::FinishedLoading) { |
| 99 return; |
| 100 } |
| 101 |
| 102 DCHECK(render_frame()); |
| 103 if (!render_frame()->IsMainFrame()) return; |
| 104 DCHECK(render_frame()->GetWebFrame()); |
| 105 WebDocument doc = render_frame()->GetWebFrame()->document(); |
| 106 if (doc.isNull() || doc.body().isNull()) return; |
| 107 |
| 108 bool is_loaded = layout_type == WebMeaningfulLayout::FinishedLoading; |
| 109 if (!needToUpdate(is_loaded)) return; |
| 110 |
| 111 Send(new FrameHostMsg_Distillability(routing_id(), |
| 112 IsDistillablePage(doc), isLast(is_loaded))); |
| 113 } |
| 114 |
| 115 |
| 116 DistillabilityAgent::~DistillabilityAgent() {} |
| 117 |
| 118 } // namespace dom_distiller |
OLD | NEW |