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 "chrome/browser/search/contextual_search_promo_source.h" | 5 #include "chrome/browser/search/contextual_search_promo_source.h" |
6 | 6 |
7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 10 #include "base/values.h" |
10 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
11 #include "grit/browser_resources.h" | 12 #include "grit/browser_resources.h" |
| 13 #include "grit/chromium_strings.h" |
| 14 #include "grit/generated_resources.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
12 #include "ui/base/resource/resource_bundle.h" | 16 #include "ui/base/resource/resource_bundle.h" |
| 17 #include "ui/base/webui/jstemplate_builder.h" |
13 #include "url/gurl.h" | 18 #include "url/gurl.h" |
14 | 19 |
15 namespace { | 20 namespace { |
16 | 21 |
17 const char kPromoHTMLPath[] = "/promo.html"; | 22 const char kPromoHTMLPath[] = "/promo.html"; |
18 const char kPromoCSSPath[] = "/promo.css"; | 23 const char kPromoCSSPath[] = "/promo.css"; |
19 const char kPromoJSPath[] = "/promo.js"; | 24 const char kPromoJSPath[] = "/promo.js"; |
| 25 const char kPromoRobotoPath[] = "/roboto.woff"; |
20 | 26 |
21 } // namespace | 27 } // namespace |
22 | 28 |
23 ContextualSearchPromoSource::ContextualSearchPromoSource() {} | 29 ContextualSearchPromoSource::ContextualSearchPromoSource() {} |
24 | 30 |
25 ContextualSearchPromoSource::~ContextualSearchPromoSource() {} | 31 ContextualSearchPromoSource::~ContextualSearchPromoSource() {} |
26 | 32 |
27 void ContextualSearchPromoSource::StartDataRequest( | 33 void ContextualSearchPromoSource::StartDataRequest( |
28 const std::string& path_and_query, int render_process_id, | 34 const std::string& path_and_query, int render_process_id, |
29 int render_frame_id, | 35 int render_frame_id, |
30 const content::URLDataSource::GotDataCallback& callback) { | 36 const content::URLDataSource::GotDataCallback& callback) { |
31 GURL url(std::string(chrome::kChromeUIContextualSearchPromoURL) + "/" + | 37 GURL url(std::string(chrome::kChromeUIContextualSearchPromoURL) + "/" + |
32 path_and_query); | 38 path_and_query); |
33 std::string path(url.path()); | 39 std::string path(url.path()); |
34 if (path == kPromoHTMLPath) { | 40 if (path == kPromoHTMLPath) { |
35 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_HTML, callback); | 41 SendHtmlWithStrings(callback); |
36 } else if (path == kPromoCSSPath) { | 42 } else if (path == kPromoCSSPath) { |
37 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_CSS, callback); | 43 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_CSS, callback); |
38 } else if (path == kPromoJSPath) { | 44 } else if (path == kPromoJSPath) { |
39 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_JS, callback); | 45 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_JS, callback); |
| 46 } else if (path == kPromoRobotoPath) { |
| 47 SendResource(IDR_CONTEXTUAL_SEARCH_ROBOTO_WOFF, callback); |
40 } else { | 48 } else { |
41 callback.Run(NULL); | 49 callback.Run(NULL); |
42 } | 50 } |
43 } | 51 } |
44 | 52 |
45 std::string ContextualSearchPromoSource::GetSource() const { | 53 std::string ContextualSearchPromoSource::GetSource() const { |
46 return chrome::kChromeUIContextualSearchPromoHost; | 54 return chrome::kChromeUIContextualSearchPromoHost; |
47 } | 55 } |
48 | 56 |
49 std::string ContextualSearchPromoSource::GetMimeType( | 57 std::string ContextualSearchPromoSource::GetMimeType( |
50 const std::string& path_and_query) const { | 58 const std::string& path_and_query) const { |
51 std::string path(GURL("chrome://host/" + path_and_query).path()); | 59 std::string path(GURL("chrome://host/" + path_and_query).path()); |
52 if (EndsWith(path, ".js", false)) return "application/javascript"; | 60 if (EndsWith(path, ".js", false)) return "application/javascript"; |
53 if (EndsWith(path, ".png", false)) return "image/png"; | 61 if (EndsWith(path, ".png", false)) return "image/png"; |
54 if (EndsWith(path, ".css", false)) return "text/css"; | 62 if (EndsWith(path, ".css", false)) return "text/css"; |
55 if (EndsWith(path, ".html", false)) return "text/html"; | 63 if (EndsWith(path, ".html", false)) return "text/html"; |
| 64 if (EndsWith(path, ".woff", false)) return "font/woff"; |
56 return ""; | 65 return ""; |
57 } | 66 } |
58 | 67 |
59 bool ContextualSearchPromoSource::ShouldDenyXFrameOptions() const { | 68 bool ContextualSearchPromoSource::ShouldDenyXFrameOptions() const { |
60 return false; | 69 return false; |
61 } | 70 } |
62 | 71 |
| 72 bool ContextualSearchPromoSource::ShouldAddContentSecurityPolicy() const { |
| 73 return false; |
| 74 } |
| 75 |
63 void ContextualSearchPromoSource::SendResource( | 76 void ContextualSearchPromoSource::SendResource( |
64 int resource_id, const content::URLDataSource::GotDataCallback& callback) { | 77 int resource_id, const content::URLDataSource::GotDataCallback& callback) { |
65 scoped_refptr<base::RefCountedStaticMemory> response( | 78 scoped_refptr<base::RefCountedStaticMemory> response( |
66 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); | 79 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); |
67 callback.Run(response.get()); | 80 callback.Run(response.get()); |
68 } | 81 } |
| 82 |
| 83 void ContextualSearchPromoSource::SendHtmlWithStrings( |
| 84 const content::URLDataSource::GotDataCallback& callback) { |
| 85 base::DictionaryValue strings_data; |
| 86 strings_data.SetString( |
| 87 "description", |
| 88 l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_DESCRIPTION)); |
| 89 strings_data.SetString( |
| 90 "optIn", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_OPTIN)); |
| 91 strings_data.SetString( |
| 92 "optOut", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_OPTOUT)); |
| 93 base::StringPiece html( |
| 94 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 95 IDR_CONTEXTUAL_SEARCH_PROMO_HTML)); |
| 96 webui::UseVersion2 version; |
| 97 std::string response(webui::GetI18nTemplateHtml(html, &strings_data)); |
| 98 callback.Run(base::RefCountedString::TakeString(&response)); |
| 99 } |
OLD | NEW |