OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/dom_distiller/core/distiller_page.h" | 5 #include "components/dom_distiller/core/distiller_page.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
9 #include "base/location.h" | 9 #include "base/location.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "grit/components_resources.h" | 17 #include "grit/components_resources.h" |
18 #include "third_party/dom_distiller_js/dom_distiller.pb.h" | 18 #include "third_party/dom_distiller_js/dom_distiller.pb.h" |
19 #include "third_party/dom_distiller_js/dom_distiller_json_converter.h" | 19 #include "third_party/dom_distiller_js/dom_distiller_json_converter.h" |
20 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
22 | 22 |
23 namespace dom_distiller { | 23 namespace dom_distiller { |
24 | 24 |
25 namespace { | 25 namespace { |
26 | 26 |
27 const char* kOptionsPlaceholder = "$$OPTIONS"; | 27 const char* kOptionsPlaceholder = "$$OPTIONS"; |
28 const char* kStringifyPlaceholder = "$$STRINGIFY"; | 28 const char* kStringifyPlaceholder = "$$STRINGIFY"; |
29 const char* kNewContextPlaceholder = "$$NEW_CONTEXT"; | |
30 | 29 |
31 std::string GetDistillerScriptWithOptions( | 30 std::string GetDistillerScriptWithOptions( |
32 const dom_distiller::proto::DomDistillerOptions& options, | 31 const dom_distiller::proto::DomDistillerOptions& options, |
33 bool stringify_output, | 32 bool stringify_output) { |
34 bool create_new_context) { | |
35 std::string script = ResourceBundle::GetSharedInstance() | 33 std::string script = ResourceBundle::GetSharedInstance() |
36 .GetRawDataResource(IDR_DISTILLER_JS) | 34 .GetRawDataResource(IDR_DISTILLER_JS) |
37 .as_string(); | 35 .as_string(); |
38 if (script.empty()) { | 36 if (script.empty()) { |
39 return ""; | 37 return ""; |
40 } | 38 } |
41 | 39 |
42 scoped_ptr<base::Value> options_value( | 40 scoped_ptr<base::Value> options_value( |
43 dom_distiller::proto::json::DomDistillerOptions::WriteToValue(options)); | 41 dom_distiller::proto::json::DomDistillerOptions::WriteToValue(options)); |
44 std::string options_json; | 42 std::string options_json; |
45 if (!base::JSONWriter::Write(*options_value, &options_json)) { | 43 if (!base::JSONWriter::Write(*options_value, &options_json)) { |
46 NOTREACHED(); | 44 NOTREACHED(); |
47 } | 45 } |
48 size_t options_offset = script.find(kOptionsPlaceholder); | 46 size_t options_offset = script.find(kOptionsPlaceholder); |
49 DCHECK_NE(std::string::npos, options_offset); | 47 DCHECK_NE(std::string::npos, options_offset); |
50 DCHECK_EQ(std::string::npos, | 48 DCHECK_EQ(std::string::npos, |
51 script.find(kOptionsPlaceholder, options_offset + 1)); | 49 script.find(kOptionsPlaceholder, options_offset + 1)); |
52 script = | 50 script = |
53 script.replace(options_offset, strlen(kOptionsPlaceholder), options_json); | 51 script.replace(options_offset, strlen(kOptionsPlaceholder), options_json); |
54 | 52 |
55 std::string stringify = stringify_output ? "true" : "false"; | 53 std::string stringify = stringify_output ? "true" : "false"; |
56 size_t stringify_offset = script.find(kStringifyPlaceholder); | 54 size_t stringify_offset = script.find(kStringifyPlaceholder); |
57 DCHECK_NE(std::string::npos, stringify_offset); | 55 DCHECK_NE(std::string::npos, stringify_offset); |
58 DCHECK_EQ(std::string::npos, | 56 DCHECK_EQ(std::string::npos, |
59 script.find(kStringifyPlaceholder, stringify_offset + 1)); | 57 script.find(kStringifyPlaceholder, stringify_offset + 1)); |
60 script = script.replace(stringify_offset, | 58 script = script.replace(stringify_offset, |
61 strlen(kStringifyPlaceholder), | 59 strlen(kStringifyPlaceholder), |
62 stringify); | 60 stringify); |
63 | 61 |
64 std::string new_context = create_new_context ? "true" : "false"; | |
65 size_t new_context_offset = script.find(kNewContextPlaceholder); | |
66 DCHECK_NE(std::string::npos, new_context_offset); | |
67 DCHECK_EQ(std::string::npos, | |
68 script.find(kNewContextPlaceholder, new_context_offset + 1)); | |
69 script = script.replace(new_context_offset, | |
70 strlen(kNewContextPlaceholder), | |
71 new_context); | |
72 | |
73 return script; | 62 return script; |
74 } | 63 } |
75 | 64 |
76 } | 65 } |
77 | 66 |
78 DistillerPageFactory::~DistillerPageFactory() {} | 67 DistillerPageFactory::~DistillerPageFactory() {} |
79 | 68 |
80 DistillerPage::DistillerPage() : ready_(true) {} | 69 DistillerPage::DistillerPage() : ready_(true) {} |
81 | 70 |
82 DistillerPage::~DistillerPage() {} | 71 DistillerPage::~DistillerPage() {} |
83 | 72 |
84 void DistillerPage::DistillPage( | 73 void DistillerPage::DistillPage( |
85 const GURL& gurl, | 74 const GURL& gurl, |
86 const dom_distiller::proto::DomDistillerOptions options, | 75 const dom_distiller::proto::DomDistillerOptions options, |
87 const DistillerPageCallback& callback) { | 76 const DistillerPageCallback& callback) { |
88 DCHECK(ready_); | 77 DCHECK(ready_); |
89 // It is only possible to distill one page at a time. |ready_| is reset when | 78 // It is only possible to distill one page at a time. |ready_| is reset when |
90 // the callback to OnDistillationDone happens. | 79 // the callback to OnDistillationDone happens. |
91 ready_ = false; | 80 ready_ = false; |
92 distiller_page_callback_ = callback; | 81 distiller_page_callback_ = callback; |
93 distillation_start_ = base::TimeTicks::Now(); | 82 distillation_start_ = base::TimeTicks::Now(); |
94 DistillPageImpl(gurl, GetDistillerScriptWithOptions(options, | 83 DistillPageImpl(gurl, GetDistillerScriptWithOptions(options, |
95 StringifyOutput(), | 84 StringifyOutput())); |
96 CreateNewContext())); | |
97 } | 85 } |
98 | 86 |
99 void DistillerPage::OnDistillationDone(const GURL& page_url, | 87 void DistillerPage::OnDistillationDone(const GURL& page_url, |
100 const base::Value* value) { | 88 const base::Value* value) { |
101 DCHECK(!ready_); | 89 DCHECK(!ready_); |
102 ready_ = true; | 90 ready_ = true; |
103 | 91 |
104 scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result( | 92 scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result( |
105 new dom_distiller::proto::DomDistillerResult()); | 93 new dom_distiller::proto::DomDistillerResult()); |
106 bool found_content; | 94 bool found_content; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 } | 151 } |
164 } | 152 } |
165 } | 153 } |
166 | 154 |
167 base::ThreadTaskRunnerHandle::Get()->PostTask( | 155 base::ThreadTaskRunnerHandle::Get()->PostTask( |
168 FROM_HERE, base::Bind(distiller_page_callback_, | 156 FROM_HERE, base::Bind(distiller_page_callback_, |
169 base::Passed(&distiller_result), found_content)); | 157 base::Passed(&distiller_result), found_content)); |
170 } | 158 } |
171 | 159 |
172 } // namespace dom_distiller | 160 } // namespace dom_distiller |
OLD | NEW |