OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/search/contextual_search_promo_source.h" | |
6 | |
7 #include "base/memory/ref_counted_memory.h" | |
8 #include "base/strings/string_util.h" | |
9 #include "base/strings/stringprintf.h" | |
10 #include "base/values.h" | |
11 #include "chrome/common/url_constants.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" | |
16 #include "ui/base/resource/resource_bundle.h" | |
17 #include "ui/base/webui/jstemplate_builder.h" | |
18 #include "url/gurl.h" | |
19 | |
20 namespace { | |
21 | |
22 const char kPromoHTMLPath[] = "/promo.html"; | |
23 const char kPromoCSSPath[] = "/promo.css"; | |
24 const char kPromoJSPath[] = "/promo.js"; | |
25 const char kPromoRobotoPath[] = "/roboto.woff"; | |
26 | |
27 } // namespace | |
28 | |
29 ContextualSearchPromoSource::ContextualSearchPromoSource() {} | |
30 | |
31 ContextualSearchPromoSource::~ContextualSearchPromoSource() {} | |
32 | |
33 void ContextualSearchPromoSource::StartDataRequest( | |
34 const std::string& path_and_query, int render_process_id, | |
35 int render_frame_id, | |
36 const content::URLDataSource::GotDataCallback& callback) { | |
37 GURL url(std::string(chrome::kChromeUIContextualSearchPromoURL) + "/" + | |
38 path_and_query); | |
39 std::string path(url.path()); | |
40 if (path == kPromoHTMLPath) { | |
41 SendHtmlWithStrings(callback); | |
42 } else if (path == kPromoCSSPath) { | |
43 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_CSS, callback); | |
44 } else if (path == kPromoJSPath) { | |
45 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_JS, callback); | |
46 } else if (path == kPromoRobotoPath) { | |
47 SendResource(IDR_CONTEXTUAL_SEARCH_ROBOTO_WOFF, callback); | |
48 } else { | |
49 callback.Run(NULL); | |
50 } | |
51 } | |
52 | |
53 std::string ContextualSearchPromoSource::GetSource() const { | |
54 return chrome::kChromeUIContextualSearchPromoHost; | |
55 } | |
56 | |
57 std::string ContextualSearchPromoSource::GetMimeType( | |
58 const std::string& path_and_query) const { | |
59 std::string path(GURL("chrome://host/" + path_and_query).path()); | |
60 if (EndsWith(path, ".js", false)) return "application/javascript"; | |
61 if (EndsWith(path, ".png", false)) return "image/png"; | |
62 if (EndsWith(path, ".css", false)) return "text/css"; | |
63 if (EndsWith(path, ".html", false)) return "text/html"; | |
64 if (EndsWith(path, ".woff", false)) return "font/woff"; | |
65 return ""; | |
66 } | |
67 | |
68 bool ContextualSearchPromoSource::ShouldDenyXFrameOptions() const { | |
69 return false; | |
70 } | |
71 | |
72 bool ContextualSearchPromoSource::ShouldAddContentSecurityPolicy() const { | |
73 return false; | |
74 } | |
75 | |
76 void ContextualSearchPromoSource::SendResource( | |
77 int resource_id, const content::URLDataSource::GotDataCallback& callback) { | |
78 scoped_refptr<base::RefCountedStaticMemory> response( | |
79 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); | |
80 callback.Run(response.get()); | |
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 |