Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/content/dom_distiller_viewer_source.h" | 5 #include "components/dom_distiller/content/dom_distiller_viewer_source.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 14 #include "components/dom_distiller/core/dom_distiller_service.h" | 14 #include "components/dom_distiller/core/dom_distiller_service.h" |
| 15 #include "components/dom_distiller/core/proto/distilled_page.pb.h" | 15 #include "components/dom_distiller/core/proto/distilled_page.pb.h" |
| 16 #include "components/dom_distiller/core/task_tracker.h" | 16 #include "components/dom_distiller/core/task_tracker.h" |
| 17 #include "content/public/common/url_constants.h" | 17 #include "content/public/common/url_constants.h" |
| 18 #include "grit/component_strings.h" | 18 #include "grit/component_strings.h" |
| 19 #include "grit/dom_distiller_resources.h" | 19 #include "grit/dom_distiller_resources.h" |
| 20 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
| 23 #include "url/gurl.h" | 23 #include "url/gurl.h" |
| 24 | 24 |
| 25 namespace { | 25 namespace { |
| 26 | 26 |
| 27 const std::string kCssPath = "readability.css"; | |
|
Tom Sepez
2014/01/14 22:22:50
This creates a Non-POD static initializer. Use con
Nico
2014/01/15 05:35:26
Yup.
nyquist
2014/01/15 22:50:59
Done.
| |
| 28 | |
| 27 std::string ReplaceHtmlTemplateValues(std::string title, std::string content) { | 29 std::string ReplaceHtmlTemplateValues(std::string title, std::string content) { |
| 28 base::StringPiece html_template = | 30 base::StringPiece html_template = |
| 29 ResourceBundle::GetSharedInstance().GetRawDataResource( | 31 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 30 IDR_DOM_DISTILLER_VIEWER_HTML); | 32 IDR_DOM_DISTILLER_VIEWER_HTML); |
| 31 std::vector<std::string> substitutions; | 33 std::vector<std::string> substitutions; |
| 32 substitutions.push_back(title); // $1 | 34 substitutions.push_back(title); // $1 |
| 33 substitutions.push_back(title); // $2 | 35 substitutions.push_back(kCssPath); // $2 |
| 34 substitutions.push_back(content); // $3 | 36 substitutions.push_back(title); // $3 |
| 37 substitutions.push_back(content); // $4 | |
| 35 return ReplaceStringPlaceholders(html_template, substitutions, NULL); | 38 return ReplaceStringPlaceholders(html_template, substitutions, NULL); |
| 36 } | 39 } |
| 37 } | 40 } |
| 38 | 41 |
| 39 namespace dom_distiller { | 42 namespace dom_distiller { |
| 40 | 43 |
| 41 // Handles receiving data asynchronously for a specific entry, and passing | 44 // Handles receiving data asynchronously for a specific entry, and passing |
| 42 // it along to the data callback for the data source. | 45 // it along to the data callback for the data source. |
| 43 class RequestViewerHandle : public ViewRequestDelegate { | 46 class RequestViewerHandle : public ViewRequestDelegate { |
| 44 public: | 47 public: |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 | 99 |
| 97 std::string DomDistillerViewerSource::GetSource() const { | 100 std::string DomDistillerViewerSource::GetSource() const { |
| 98 return chrome::kDomDistillerScheme; | 101 return chrome::kDomDistillerScheme; |
| 99 } | 102 } |
| 100 | 103 |
| 101 void DomDistillerViewerSource::StartDataRequest( | 104 void DomDistillerViewerSource::StartDataRequest( |
| 102 const std::string& path, | 105 const std::string& path, |
| 103 int render_process_id, | 106 int render_process_id, |
| 104 int render_view_id, | 107 int render_view_id, |
| 105 const content::URLDataSource::GotDataCallback& callback) { | 108 const content::URLDataSource::GotDataCallback& callback) { |
| 109 if (kCssPath == path) { | |
| 110 std::string css = ResourceBundle::GetSharedInstance() | |
| 111 .GetRawDataResource(IDR_DISTILLER_CSS) | |
|
Tom Sepez
2014/01/14 22:22:50
nit: not sure if the style guide wants the "."s on
Nico
2014/01/15 05:35:26
I think this is what clang-format produces.
nyquist
2014/01/15 22:50:59
Yeah, this is pure clang-format. Keeping as is the
| |
| 112 .as_string(); | |
| 113 callback.Run(base::RefCountedString::TakeString(&css)); | |
| 114 return; | |
| 115 } | |
| 116 | |
| 106 RequestViewerHandle* request_viewer_handle = | 117 RequestViewerHandle* request_viewer_handle = |
| 107 new RequestViewerHandle(callback); | 118 new RequestViewerHandle(callback); |
| 108 std::string entry_id = StringToUpperASCII(path); | 119 std::string entry_id = StringToUpperASCII(path); |
| 109 scoped_ptr<ViewerHandle> viewer_handle = | 120 scoped_ptr<ViewerHandle> viewer_handle = |
| 110 dom_distiller_service_->ViewEntry(request_viewer_handle, entry_id); | 121 dom_distiller_service_->ViewEntry(request_viewer_handle, entry_id); |
| 111 if (viewer_handle) { | 122 if (viewer_handle) { |
| 112 // The service returned a |ViewerHandle| and guarantees it will call | 123 // The service returned a |ViewerHandle| and guarantees it will call |
| 113 // the |RequestViewerHandle|, so passing ownership to it, to ensure the | 124 // the |RequestViewerHandle|, so passing ownership to it, to ensure the |
| 114 // request is not cancelled. | 125 // request is not cancelled. |
| 115 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); | 126 request_viewer_handle->TakeViewerHandle(viewer_handle.Pass()); |
| 116 } else { | 127 } else { |
| 117 // The service did not return a |ViewerHandle|, which means the | 128 // The service did not return a |ViewerHandle|, which means the |
| 118 // |RequestViewerHandle| will never be called, so cleanup now. | 129 // |RequestViewerHandle| will never be called, so cleanup now. |
| 119 delete request_viewer_handle; | 130 delete request_viewer_handle; |
| 120 | 131 |
| 121 std::string title = l10n_util::GetStringUTF8( | 132 std::string title = l10n_util::GetStringUTF8( |
| 122 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); | 133 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_TITLE); |
| 123 std::string content = l10n_util::GetStringUTF8( | 134 std::string content = l10n_util::GetStringUTF8( |
| 124 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); | 135 IDS_DOM_DISTILLER_VIEWER_FAILED_TO_FIND_ARTICLE_CONTENT); |
| 125 std::string html = ReplaceHtmlTemplateValues(title, content); | 136 std::string html = ReplaceHtmlTemplateValues(title, content); |
| 126 callback.Run(base::RefCountedString::TakeString(&html)); | 137 callback.Run(base::RefCountedString::TakeString(&html)); |
| 127 } | 138 } |
| 128 }; | 139 }; |
| 129 | 140 |
| 130 std::string DomDistillerViewerSource::GetMimeType(const std::string& path) | 141 std::string DomDistillerViewerSource::GetMimeType(const std::string& path) |
| 131 const { | 142 const { |
| 143 if (path == kCssPath) | |
| 144 return "text/css"; | |
| 132 return "text/html"; | 145 return "text/html"; |
| 133 } | 146 } |
| 134 | 147 |
| 135 bool DomDistillerViewerSource::ShouldServiceRequest( | 148 bool DomDistillerViewerSource::ShouldServiceRequest( |
| 136 const net::URLRequest* request) const { | 149 const net::URLRequest* request) const { |
| 137 return request->url().SchemeIs(chrome::kDomDistillerScheme); | 150 return request->url().SchemeIs(chrome::kDomDistillerScheme); |
| 138 } | 151 } |
| 139 | 152 |
| 140 void DomDistillerViewerSource::WillServiceRequest( | 153 void DomDistillerViewerSource::WillServiceRequest( |
| 141 const net::URLRequest* request, | 154 const net::URLRequest* request, |
| 142 std::string* path) const { | 155 std::string* path) const { |
| 143 // Since the full request is not available to StartDataRequest, replace the | 156 if (*path != kCssPath) { |
| 144 // path to contain the data needed. | 157 // Since the full request is not available to StartDataRequest, replace the |
| 145 path->assign(request->url().host()); | 158 // path to contain the data needed. |
| 159 path->assign(request->url().host()); | |
| 160 } | |
| 146 }; | 161 }; |
| 147 | 162 |
| 148 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { | 163 std::string DomDistillerViewerSource::GetContentSecurityPolicyFrameSrc() const { |
| 149 return "frame-src none;"; | 164 return "default-src 'none'; style-src 'self'"; |
| 150 } | 165 } |
| 151 | 166 |
| 152 } // namespace dom_distiller | 167 } // namespace dom_distiller |
| OLD | NEW |