| 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/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 .GetRawDataResource(IDR_DISTILLER_JS) | |
| 31 .as_string(); | |
| 32 if (script.empty()) { | 30 if (script.empty()) { |
| 33 return ""; | 31 return ""; |
| 34 } | 32 } |
| 35 | 33 |
| 36 scoped_ptr<base::Value> options_value( | 34 scoped_ptr<base::Value> options_value( |
| 37 dom_distiller::proto::json::DomDistillerOptions::WriteToValue(options)); | 35 dom_distiller::proto::json::DomDistillerOptions::WriteToValue(options)); |
| 38 std::string options_json; | 36 std::string options_json; |
| 39 if (!base::JSONWriter::Write(options_value.get(), &options_json)) { | 37 if (!base::JSONWriter::Write(options_value.get(), &options_json)) { |
| 40 NOTREACHED(); | 38 NOTREACHED(); |
| 41 } | 39 } |
| 42 size_t options_offset = script.find(kOptionsPlaceholder); | 40 size_t options_offset = script.find(kOptionsPlaceholder); |
| 43 DCHECK_NE(std::string::npos, options_offset); | 41 DCHECK_NE(std::string::npos, options_offset); |
| 44 DCHECK_EQ(std::string::npos, | 42 DCHECK_EQ(std::string::npos, |
| 45 script.find(kOptionsPlaceholder, options_offset + 1)); | 43 script.find(kOptionsPlaceholder, options_offset + 1)); |
| 46 script = | 44 // Make a copy of |script| so the options can be applied. |
| 47 script.replace(options_offset, strlen(kOptionsPlaceholder), options_json); | 45 std::string script_with_options = script; |
| 48 return script; | 46 script_with_options = script_with_options.replace( |
| 47 options_offset, strlen(kOptionsPlaceholder), options_json); |
| 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(const std::string& distiller_js_script) |
| 56 : ready_(true), distiller_js_script_(distiller_js_script) { |
| 57 } |
| 56 | 58 |
| 57 DistillerPage::~DistillerPage() {} | 59 DistillerPage::~DistillerPage() {} |
| 58 | 60 |
| 59 void DistillerPage::DistillPage( | 61 void DistillerPage::DistillPage( |
| 60 const GURL& gurl, | 62 const GURL& gurl, |
| 61 const dom_distiller::proto::DomDistillerOptions options, | 63 const dom_distiller::proto::DomDistillerOptions options, |
| 62 const DistillerPageCallback& callback) { | 64 const DistillerPageCallback& callback) { |
| 63 DCHECK(ready_); | 65 DCHECK(ready_); |
| 64 // It is only possible to distill one page at a time. |ready_| is reset when | 66 // It is only possible to distill one page at a time. |ready_| is reset when |
| 65 // the callback to OnDistillationDone happens. | 67 // the callback to OnDistillationDone happens. |
| 66 ready_ = false; | 68 ready_ = false; |
| 67 distiller_page_callback_ = callback; | 69 distiller_page_callback_ = callback; |
| 68 DistillPageImpl(gurl, GetDistillerScriptWithOptions(options)); | 70 DistillPageImpl(gurl, |
| 71 GetDistillerScriptWithOptions(distiller_js_script_, options)); |
| 69 } | 72 } |
| 70 | 73 |
| 71 void DistillerPage::OnDistillationDone(const GURL& page_url, | 74 void DistillerPage::OnDistillationDone(const GURL& page_url, |
| 72 const base::Value* value) { | 75 const base::Value* value) { |
| 73 DCHECK(!ready_); | 76 DCHECK(!ready_); |
| 74 ready_ = true; | 77 ready_ = true; |
| 75 | 78 |
| 76 scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result( | 79 scoped_ptr<dom_distiller::proto::DomDistillerResult> distiller_result( |
| 77 new dom_distiller::proto::DomDistillerResult()); | 80 new dom_distiller::proto::DomDistillerResult()); |
| 78 bool found_content; | 81 bool found_content; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 } | 133 } |
| 131 | 134 |
| 132 base::MessageLoop::current()->PostTask( | 135 base::MessageLoop::current()->PostTask( |
| 133 FROM_HERE, | 136 FROM_HERE, |
| 134 base::Bind(distiller_page_callback_, | 137 base::Bind(distiller_page_callback_, |
| 135 base::Passed(&distiller_result), | 138 base::Passed(&distiller_result), |
| 136 found_content)); | 139 found_content)); |
| 137 } | 140 } |
| 138 | 141 |
| 139 } // namespace dom_distiller | 142 } // namespace dom_distiller |
| OLD | NEW |