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

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

Issue 1004223002: Pass more information by argument to the dom_distiller script. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 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 const char* kStringifyPlaceholder = "$$STRINGIFY";
27 const char* kNewContextPlaceholder = "$$NEW_CONTEXT";
26 28
27 std::string GetDistillerScriptWithOptions( 29 std::string GetDistillerScriptWithOptions(
28 const dom_distiller::proto::DomDistillerOptions& options) { 30 const dom_distiller::proto::DomDistillerOptions& options,
31 bool stringify_output,
32 bool create_new_context) {
29 std::string script = ResourceBundle::GetSharedInstance() 33 std::string script = ResourceBundle::GetSharedInstance()
30 .GetRawDataResource(IDR_DISTILLER_JS) 34 .GetRawDataResource(IDR_DISTILLER_JS)
31 .as_string(); 35 .as_string();
32 if (script.empty()) { 36 if (script.empty()) {
33 return ""; 37 return "";
34 } 38 }
35 39
36 scoped_ptr<base::Value> options_value( 40 scoped_ptr<base::Value> options_value(
37 dom_distiller::proto::json::DomDistillerOptions::WriteToValue(options)); 41 dom_distiller::proto::json::DomDistillerOptions::WriteToValue(options));
38 std::string options_json; 42 std::string options_json;
39 if (!base::JSONWriter::Write(options_value.get(), &options_json)) { 43 if (!base::JSONWriter::Write(options_value.get(), &options_json)) {
40 NOTREACHED(); 44 NOTREACHED();
41 } 45 }
42 size_t options_offset = script.find(kOptionsPlaceholder); 46 size_t options_offset = script.find(kOptionsPlaceholder);
43 DCHECK_NE(std::string::npos, options_offset); 47 DCHECK_NE(std::string::npos, options_offset);
44 DCHECK_EQ(std::string::npos, 48 DCHECK_EQ(std::string::npos,
45 script.find(kOptionsPlaceholder, options_offset + 1)); 49 script.find(kOptionsPlaceholder, options_offset + 1));
46 script = 50 script =
47 script.replace(options_offset, strlen(kOptionsPlaceholder), options_json); 51 script.replace(options_offset, strlen(kOptionsPlaceholder), options_json);
52
53 std::string stringify = stringify_output ? "true" : "false";
54 size_t stringify_offset = script.find(kStringifyPlaceholder);
55 DCHECK_NE(std::string::npos, stringify_offset);
56 DCHECK_EQ(std::string::npos,
57 script.find(kStringifyPlaceholder, stringify_offset + 1));
58 script = script.replace(stringify_offset,
59 strlen(kStringifyPlaceholder),
60 stringify);
61
62 std::string new_context = create_new_context ? "true" : "false";
63 size_t new_context_offset = script.find(kNewContextPlaceholder);
64 DCHECK_NE(std::string::npos, new_context_offset);
65 DCHECK_EQ(std::string::npos,
66 script.find(kNewContextPlaceholder, new_context_offset + 1));
67 script = script.replace(new_context_offset,
68 strlen(kNewContextPlaceholder),
69 new_context);
70
48 return script; 71 return script;
49 } 72 }
50 73
51 } 74 }
52 75
53 DistillerPageFactory::~DistillerPageFactory() {} 76 DistillerPageFactory::~DistillerPageFactory() {}
54 77
55 DistillerPage::DistillerPage() : ready_(true) {} 78 DistillerPage::DistillerPage() : ready_(true) {}
56 79
57 DistillerPage::~DistillerPage() {} 80 DistillerPage::~DistillerPage() {}
58 81
59 void DistillerPage::DistillPage( 82 void DistillerPage::DistillPage(
60 const GURL& gurl, 83 const GURL& gurl,
61 const dom_distiller::proto::DomDistillerOptions options, 84 const dom_distiller::proto::DomDistillerOptions options,
62 const DistillerPageCallback& callback) { 85 const DistillerPageCallback& callback) {
63 DCHECK(ready_); 86 DCHECK(ready_);
64 // It is only possible to distill one page at a time. |ready_| is reset when 87 // It is only possible to distill one page at a time. |ready_| is reset when
65 // the callback to OnDistillationDone happens. 88 // the callback to OnDistillationDone happens.
66 ready_ = false; 89 ready_ = false;
67 distiller_page_callback_ = callback; 90 distiller_page_callback_ = callback;
68 DistillPageImpl(gurl, GetDistillerScriptWithOptions(options)); 91 DistillPageImpl(gurl, GetDistillerScriptWithOptions(options,
92 StringifyOutput(),
93 CreateNewContext()));
69 } 94 }
70 95
71 void DistillerPage::OnDistillationDone(const GURL& page_url, 96 void DistillerPage::OnDistillationDone(const GURL& page_url,
72 const base::Value* value) { 97 const base::Value* value) {
73 DCHECK(!ready_); 98 DCHECK(!ready_);
74 ready_ = true; 99 ready_ = true;
75 100
76 scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result( 101 scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result(
77 new dom_distiller::proto::DomDistillerResult()); 102 new dom_distiller::proto::DomDistillerResult());
78 bool found_content; 103 bool found_content;
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 } 155 }
131 156
132 base::MessageLoop::current()->PostTask( 157 base::MessageLoop::current()->PostTask(
133 FROM_HERE, 158 FROM_HERE,
134 base::Bind(distiller_page_callback_, 159 base::Bind(distiller_page_callback_,
135 base::Passed(&distiller_result), 160 base::Passed(&distiller_result),
136 found_content)); 161 found_content));
137 } 162 }
138 163
139 } // namespace dom_distiller 164 } // namespace dom_distiller
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698