forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistiller_page.cc
172 lines (152 loc) · 6.26 KB
/
distiller_page.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/dom_distiller/core/distiller_page.h"
#include "base/bind.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "grit/components_resources.h"
#include "third_party/dom_distiller_js/dom_distiller.pb.h"
#include "third_party/dom_distiller_js/dom_distiller_json_converter.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
namespace dom_distiller {
namespace {
const char* kOptionsPlaceholder = "$$OPTIONS";
const char* kStringifyPlaceholder = "$$STRINGIFY";
const char* kNewContextPlaceholder = "$$NEW_CONTEXT";
std::string GetDistillerScriptWithOptions(
const dom_distiller::proto::DomDistillerOptions& options,
bool stringify_output,
bool create_new_context) {
std::string script = ResourceBundle::GetSharedInstance()
.GetRawDataResource(IDR_DISTILLER_JS)
.as_string();
if (script.empty()) {
return "";
}
scoped_ptr<base::Value> options_value(
dom_distiller::proto::json::DomDistillerOptions::WriteToValue(options));
std::string options_json;
if (!base::JSONWriter::Write(*options_value, &options_json)) {
NOTREACHED();
}
size_t options_offset = script.find(kOptionsPlaceholder);
DCHECK_NE(std::string::npos, options_offset);
DCHECK_EQ(std::string::npos,
script.find(kOptionsPlaceholder, options_offset + 1));
script =
script.replace(options_offset, strlen(kOptionsPlaceholder), options_json);
std::string stringify = stringify_output ? "true" : "false";
size_t stringify_offset = script.find(kStringifyPlaceholder);
DCHECK_NE(std::string::npos, stringify_offset);
DCHECK_EQ(std::string::npos,
script.find(kStringifyPlaceholder, stringify_offset + 1));
script = script.replace(stringify_offset,
strlen(kStringifyPlaceholder),
stringify);
std::string new_context = create_new_context ? "true" : "false";
size_t new_context_offset = script.find(kNewContextPlaceholder);
DCHECK_NE(std::string::npos, new_context_offset);
DCHECK_EQ(std::string::npos,
script.find(kNewContextPlaceholder, new_context_offset + 1));
script = script.replace(new_context_offset,
strlen(kNewContextPlaceholder),
new_context);
return script;
}
}
DistillerPageFactory::~DistillerPageFactory() {}
DistillerPage::DistillerPage() : ready_(true) {}
DistillerPage::~DistillerPage() {}
void DistillerPage::DistillPage(
const GURL& gurl,
const dom_distiller::proto::DomDistillerOptions options,
const DistillerPageCallback& callback) {
DCHECK(ready_);
// It is only possible to distill one page at a time. |ready_| is reset when
// the callback to OnDistillationDone happens.
ready_ = false;
distiller_page_callback_ = callback;
distillation_start_ = base::TimeTicks::Now();
DistillPageImpl(gurl, GetDistillerScriptWithOptions(options,
StringifyOutput(),
CreateNewContext()));
}
void DistillerPage::OnDistillationDone(const GURL& page_url,
const base::Value* value) {
DCHECK(!ready_);
ready_ = true;
scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result(
new dom_distiller::proto::DomDistillerResult());
bool found_content;
if (value->IsType(base::Value::TYPE_NULL)) {
found_content = false;
} else {
found_content =
dom_distiller::proto::json::DomDistillerResult::ReadFromValue(
value, distiller_result.get());
if (!found_content) {
DVLOG(1) << "Unable to parse DomDistillerResult.";
} else {
base::TimeDelta distillation_time =
base::TimeTicks::Now() - distillation_start_;
UMA_HISTOGRAM_TIMES("DomDistiller.Time.DistillPage", distillation_time);
VLOG(1) << "DomDistiller.Time.DistillPage = " << distillation_time;
if (distiller_result->has_timing_info()) {
const dom_distiller::proto::TimingInfo& timing =
distiller_result->timing_info();
if (timing.has_markup_parsing_time()) {
UMA_HISTOGRAM_TIMES(
"DomDistiller.Time.MarkupParsing",
base::TimeDelta::FromMillisecondsD(timing.markup_parsing_time()));
}
if (timing.has_document_construction_time()) {
UMA_HISTOGRAM_TIMES(
"DomDistiller.Time.DocumentConstruction",
base::TimeDelta::FromMillisecondsD(
timing.document_construction_time()));
}
if (timing.has_article_processing_time()) {
UMA_HISTOGRAM_TIMES(
"DomDistiller.Time.ArticleProcessing",
base::TimeDelta::FromMillisecondsD(
timing.article_processing_time()));
}
if (timing.has_formatting_time()) {
UMA_HISTOGRAM_TIMES(
"DomDistiller.Time.Formatting",
base::TimeDelta::FromMillisecondsD(timing.formatting_time()));
}
if (timing.has_total_time()) {
UMA_HISTOGRAM_TIMES(
"DomDistiller.Time.DistillationTotal",
base::TimeDelta::FromMillisecondsD(timing.total_time()));
VLOG(1) << "DomDistiller.Time.DistillationTotal = " <<
base::TimeDelta::FromMillisecondsD(timing.total_time());
}
}
if (distiller_result->has_statistics_info()) {
const dom_distiller::proto::StatisticsInfo& statistics =
distiller_result->statistics_info();
if (statistics.has_word_count()) {
UMA_HISTOGRAM_CUSTOM_COUNTS(
"DomDistiller.Statistics.WordCount",
statistics.word_count(),
1, 4000, 50);
}
}
}
}
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(distiller_page_callback_,
base::Passed(&distiller_result),
found_content));
}
} // namespace dom_distiller