Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(302)

Side by Side Diff: components/dom_distiller/core/distiller_page.cc

Issue 901793002: Add support for providing an external file for extracting content. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/logging.h" 9 #include "base/logging.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/metrics/histogram.h" 11 #include "base/metrics/histogram.h"
12 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "grit/components_resources.h" 15 #include "grit/components_resources.h"
16 #include "third_party/dom_distiller_js/dom_distiller.pb.h" 16 #include "third_party/dom_distiller_js/dom_distiller.pb.h"
17 #include "third_party/dom_distiller_js/dom_distiller_json_converter.h" 17 #include "third_party/dom_distiller_js/dom_distiller_json_converter.h"
18 #include "ui/base/resource/resource_bundle.h" 18 #include "ui/base/resource/resource_bundle.h"
19 #include "url/gurl.h" 19 #include "url/gurl.h"
20 20
21 namespace dom_distiller { 21 namespace dom_distiller {
22 22
23 namespace { 23 namespace {
24 24
25 const char* kOptionsPlaceholder = "$$OPTIONS"; 25 const char* kOptionsPlaceholder = "$$OPTIONS";
26 26
27 std::string GetDistillerScriptWithOptions( 27 std::string GetDistillerScriptWithOptions(
28 const std::string& script,
28 const dom_distiller::proto::DomDistillerOptions& options) { 29 const dom_distiller::proto::DomDistillerOptions& options) {
29 std::string script = ResourceBundle::GetSharedInstance() 30 // Make a copy of |script| so the options can be applied.
30 .GetRawDataResource(IDR_DISTILLER_JS) 31 std::string script_with_options = script;
31 .as_string(); 32 if (script_with_options.empty()) {
cjhopman 2015/02/05 02:09:40 this could just check script.empty() rather than w
nyquist 2015/02/06 18:50:32 Done.
32 if (script.empty()) {
33 return ""; 33 return "";
34 } 34 }
35 35
36 scoped_ptr<base::Value> options_value( 36 scoped_ptr<base::Value> options_value(
37 dom_distiller::proto::json::DomDistillerOptions::WriteToValue(options)); 37 dom_distiller::proto::json::DomDistillerOptions::WriteToValue(options));
38 std::string options_json; 38 std::string options_json;
39 if (!base::JSONWriter::Write(options_value.get(), &options_json)) { 39 if (!base::JSONWriter::Write(options_value.get(), &options_json)) {
40 NOTREACHED(); 40 NOTREACHED();
41 } 41 }
42 size_t options_offset = script.find(kOptionsPlaceholder); 42 size_t options_offset = script_with_options.find(kOptionsPlaceholder);
43 DCHECK_NE(std::string::npos, options_offset); 43 DCHECK_NE(std::string::npos, options_offset);
44 DCHECK_EQ(std::string::npos, 44 DCHECK_EQ(std::string::npos,
45 script.find(kOptionsPlaceholder, options_offset + 1)); 45 script_with_options.find(kOptionsPlaceholder, options_offset + 1));
46 script = 46 script_with_options = script_with_options.replace(
cjhopman 2015/02/05 02:09:40 I'd move the copy down here where it's needed.
nyquist 2015/02/06 18:50:32 Done.
47 script.replace(options_offset, strlen(kOptionsPlaceholder), options_json); 47 options_offset, strlen(kOptionsPlaceholder), options_json);
48 return script; 48 return script_with_options;
49 } 49 }
50 50
51 } 51 }
52 52
53 DistillerPageFactory::~DistillerPageFactory() {} 53 DistillerPageFactory::~DistillerPageFactory() {}
54 54
55 DistillerPage::DistillerPage() : ready_(true) {} 55 DistillerPage::DistillerPage() : ready_(true) {
56 distiller_js_script_ = ResourceBundle::GetSharedInstance()
57 .GetRawDataResource(IDR_DISTILLER_JS)
58 .as_string();
59 }
60
61 DistillerPage::DistillerPage(std::string distiller_js_script)
62 : ready_(true), distiller_js_script_(distiller_js_script) {
63 }
56 64
57 DistillerPage::~DistillerPage() {} 65 DistillerPage::~DistillerPage() {}
58 66
59 void DistillerPage::DistillPage( 67 void DistillerPage::DistillPage(
60 const GURL& gurl, 68 const GURL& gurl,
61 const dom_distiller::proto::DomDistillerOptions options, 69 const dom_distiller::proto::DomDistillerOptions options,
62 const DistillerPageCallback& callback) { 70 const DistillerPageCallback& callback) {
63 DCHECK(ready_); 71 DCHECK(ready_);
64 // It is only possible to distill one page at a time. |ready_| is reset when 72 // It is only possible to distill one page at a time. |ready_| is reset when
65 // the callback to OnDistillationDone happens. 73 // the callback to OnDistillationDone happens.
66 ready_ = false; 74 ready_ = false;
67 distiller_page_callback_ = callback; 75 distiller_page_callback_ = callback;
68 DistillPageImpl(gurl, GetDistillerScriptWithOptions(options)); 76 DistillPageImpl(gurl,
77 GetDistillerScriptWithOptions(distiller_js_script_, options));
69 } 78 }
70 79
71 void DistillerPage::OnDistillationDone(const GURL& page_url, 80 void DistillerPage::OnDistillationDone(const GURL& page_url,
72 const base::Value* value) { 81 const base::Value* value) {
73 DCHECK(!ready_); 82 DCHECK(!ready_);
74 ready_ = true; 83 ready_ = true;
75 84
76 scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result( 85 scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result(
77 new dom_distiller::proto::DomDistillerResult()); 86 new dom_distiller::proto::DomDistillerResult());
78 bool found_content; 87 bool found_content;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 } 139 }
131 140
132 base::MessageLoop::current()->PostTask( 141 base::MessageLoop::current()->PostTask(
133 FROM_HERE, 142 FROM_HERE,
134 base::Bind(distiller_page_callback_, 143 base::Bind(distiller_page_callback_,
135 base::Passed(&distiller_result), 144 base::Passed(&distiller_result),
136 found_content)); 145 found_content));
137 } 146 }
138 147
139 } // namespace dom_distiller 148 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698