[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / chrome / browser / search / contextual_search_promo_source_android.cc
blobec36bbdb63585b1e0ee6e1b542919066dd809fa3
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.
5 #include "chrome/browser/search/contextual_search_promo_source_android.h"
7 #include <string>
9 #include "base/json/json_string_value_serializer.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/values.h"
14 #include "chrome/common/url_constants.h"
15 #include "chrome/grit/chromium_strings.h"
16 #include "components/variations/variations_associated_data.h"
17 #include "grit/browser_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/base/webui/jstemplate_builder.h"
21 #include "url/gurl.h"
23 namespace {
25 const char kPromoConfigPath[] = "/config.js";
26 const char kPromoHTMLPath[] = "/promo.html";
27 const char kPromoCSSPath[] = "/promo.css";
28 const char kPromoJSPath[] = "/promo.js";
30 // Field trial related constants.
31 const char kContextualSearchFieldTrialName[] = "ContextualSearch";
32 const char kContextualSearchHidePromoHeaderParam[] = "hide_promo_header";
33 const char kContextualSearchEnabledValue[] = "enabled";
35 // Returns whether we should hide the first-run promo header.
36 bool ShouldHidePromoHeader() {
37 return variations::GetVariationParamValue(
38 kContextualSearchFieldTrialName, kContextualSearchHidePromoHeaderParam) ==
39 kContextualSearchEnabledValue;
42 // Returns a JS dictionary of configuration data for the Contextual Search
43 // promo.
44 std::string GetConfigData() {
45 base::DictionaryValue config_data;
46 config_data.SetBoolean("hideHeader", ShouldHidePromoHeader());
48 // Serialize the dictionary.
49 std::string js_text;
50 JSONStringValueSerializer serializer(&js_text);
51 serializer.Serialize(config_data);
53 std::string config_data_js;
54 config_data_js.append("var config = ");
55 config_data_js.append(js_text);
56 config_data_js.append(";");
57 return config_data_js;
60 } // namespace
62 ContextualSearchPromoSourceAndroid::ContextualSearchPromoSourceAndroid() {}
64 ContextualSearchPromoSourceAndroid::~ContextualSearchPromoSourceAndroid() {}
66 void ContextualSearchPromoSourceAndroid::StartDataRequest(
67 const std::string& path_and_query, int render_process_id,
68 int render_frame_id,
69 const content::URLDataSource::GotDataCallback& callback) {
70 GURL url(std::string(chrome::kChromeUIContextualSearchPromoURL) + "/" +
71 path_and_query);
72 std::string path(url.path());
73 if (path == kPromoHTMLPath) {
74 SendHtmlWithStrings(callback);
75 } else if (path == kPromoCSSPath) {
76 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_CSS, callback);
77 } else if (path == kPromoJSPath) {
78 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_JS, callback);
79 } else if (path == kPromoConfigPath) {
80 SendConfigResource(callback);
81 } else {
82 callback.Run(NULL);
86 std::string ContextualSearchPromoSourceAndroid::GetSource() const {
87 return chrome::kChromeUIContextualSearchPromoHost;
90 std::string ContextualSearchPromoSourceAndroid::GetMimeType(
91 const std::string& path_and_query) const {
92 std::string path(GURL("chrome://host/" + path_and_query).path());
93 if (base::EndsWith(path, ".js", false)) return "application/javascript";
94 if (base::EndsWith(path, ".png", false)) return "image/png";
95 if (base::EndsWith(path, ".css", false)) return "text/css";
96 if (base::EndsWith(path, ".html", false)) return "text/html";
97 if (base::EndsWith(path, ".woff", false)) return "font/woff";
98 if (base::EndsWith(path, ".woff2", false)) return "font/woff2";
99 return std::string();
102 bool ContextualSearchPromoSourceAndroid::ShouldDenyXFrameOptions() const {
103 return false;
106 bool
107 ContextualSearchPromoSourceAndroid::ShouldAddContentSecurityPolicy() const {
108 return false;
111 void ContextualSearchPromoSourceAndroid::SendResource(
112 int resource_id, const content::URLDataSource::GotDataCallback& callback) {
113 scoped_refptr<base::RefCountedStaticMemory> response(
114 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id));
115 callback.Run(response.get());
118 void ContextualSearchPromoSourceAndroid::SendConfigResource(
119 const content::URLDataSource::GotDataCallback& callback) {
120 std::string response = GetConfigData();
121 callback.Run(base::RefCountedString::TakeString(&response));
124 void ContextualSearchPromoSourceAndroid::SendHtmlWithStrings(
125 const content::URLDataSource::GotDataCallback& callback) {
126 base::DictionaryValue strings_data;
127 // The three following statements are part of the description paragraph.
128 strings_data.SetString(
129 "description-1",
130 l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_DESCRIPTION_1));
131 strings_data.SetString(
132 "feature-name",
133 l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_FEATURE_NAME));
134 strings_data.SetString(
135 "description-2",
136 l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_DESCRIPTION_2));
138 strings_data.SetString(
139 "heading", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_HEADER));
140 strings_data.SetString(
141 "optIn", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_OPTIN));
142 strings_data.SetString(
143 "optOut", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_OPTOUT));
144 base::StringPiece html(
145 ResourceBundle::GetSharedInstance().GetRawDataResource(
146 IDR_CONTEXTUAL_SEARCH_PROMO_HTML));
147 std::string response(webui::GetI18nTemplateHtml(html, &strings_data));
148 callback.Run(base::RefCountedString::TakeString(&response));