| 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_android.h" | 5 #include "chrome/browser/search/contextual_search_promo_source_android.h" |
| 6 | 6 |
| 7 #include <string> |
| 8 |
| 9 #include "base/json/json_string_value_serializer.h" |
| 7 #include "base/memory/ref_counted_memory.h" | 10 #include "base/memory/ref_counted_memory.h" |
| 8 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 9 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 10 #include "base/values.h" | 13 #include "base/values.h" |
| 11 #include "chrome/common/url_constants.h" | 14 #include "chrome/common/url_constants.h" |
| 12 #include "chrome/grit/chromium_strings.h" | 15 #include "chrome/grit/chromium_strings.h" |
| 16 #include "components/variations/variations_associated_data.h" |
| 13 #include "grit/browser_resources.h" | 17 #include "grit/browser_resources.h" |
| 14 #include "ui/base/l10n/l10n_util.h" | 18 #include "ui/base/l10n/l10n_util.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 19 #include "ui/base/resource/resource_bundle.h" |
| 16 #include "ui/base/webui/jstemplate_builder.h" | 20 #include "ui/base/webui/jstemplate_builder.h" |
| 17 #include "url/gurl.h" | 21 #include "url/gurl.h" |
| 18 | 22 |
| 19 namespace { | 23 namespace { |
| 20 | 24 |
| 25 const char kPromoConfigPath[] = "/config.js"; |
| 21 const char kPromoHTMLPath[] = "/promo.html"; | 26 const char kPromoHTMLPath[] = "/promo.html"; |
| 22 const char kPromoCSSPath[] = "/promo.css"; | 27 const char kPromoCSSPath[] = "/promo.css"; |
| 23 const char kPromoJSPath[] = "/promo.js"; | 28 const char kPromoJSPath[] = "/promo.js"; |
| 24 const char kRobotoWoffPath[] = "/roboto.woff"; | 29 const char kRobotoWoffPath[] = "/roboto.woff"; |
| 25 const char kRobotoWoff2Path[] = "/roboto.woff2"; | 30 const char kRobotoWoff2Path[] = "/roboto.woff2"; |
| 26 | 31 |
| 32 // Field trial related constants. |
| 33 const char kContextualSearchFieldTrialName[] = "ContextualSearch"; |
| 34 const char kContextualSearchHidePromoHeaderParam[] = "hide_promo_header"; |
| 35 const char kContextualSearchEnabledValue[] = "enabled"; |
| 36 |
| 37 // Returns whether we should hide the first-run promo header. |
| 38 bool ShouldHidePromoHeader() { |
| 39 return variations::GetVariationParamValue( |
| 40 kContextualSearchFieldTrialName, kContextualSearchHidePromoHeaderParam) == |
| 41 kContextualSearchEnabledValue; |
| 42 } |
| 43 |
| 44 // Returns a JS dictionary of configuration data for the Contextual Search |
| 45 // promo. |
| 46 std::string GetConfigData() { |
| 47 base::DictionaryValue config_data; |
| 48 config_data.SetBoolean("hideHeader", ShouldHidePromoHeader()); |
| 49 |
| 50 // Serialize the dictionary. |
| 51 std::string js_text; |
| 52 JSONStringValueSerializer serializer(&js_text); |
| 53 serializer.Serialize(config_data); |
| 54 |
| 55 std::string config_data_js; |
| 56 config_data_js.append("var config = "); |
| 57 config_data_js.append(js_text); |
| 58 config_data_js.append(";"); |
| 59 return config_data_js; |
| 60 } |
| 61 |
| 27 } // namespace | 62 } // namespace |
| 28 | 63 |
| 29 ContextualSearchPromoSourceAndroid::ContextualSearchPromoSourceAndroid() {} | 64 ContextualSearchPromoSourceAndroid::ContextualSearchPromoSourceAndroid() {} |
| 30 | 65 |
| 31 ContextualSearchPromoSourceAndroid::~ContextualSearchPromoSourceAndroid() {} | 66 ContextualSearchPromoSourceAndroid::~ContextualSearchPromoSourceAndroid() {} |
| 32 | 67 |
| 33 void ContextualSearchPromoSourceAndroid::StartDataRequest( | 68 void ContextualSearchPromoSourceAndroid::StartDataRequest( |
| 34 const std::string& path_and_query, int render_process_id, | 69 const std::string& path_and_query, int render_process_id, |
| 35 int render_frame_id, | 70 int render_frame_id, |
| 36 const content::URLDataSource::GotDataCallback& callback) { | 71 const content::URLDataSource::GotDataCallback& callback) { |
| 37 GURL url(std::string(chrome::kChromeUIContextualSearchPromoURL) + "/" + | 72 GURL url(std::string(chrome::kChromeUIContextualSearchPromoURL) + "/" + |
| 38 path_and_query); | 73 path_and_query); |
| 39 std::string path(url.path()); | 74 std::string path(url.path()); |
| 40 if (path == kPromoHTMLPath) { | 75 if (path == kPromoHTMLPath) { |
| 41 SendHtmlWithStrings(callback); | 76 SendHtmlWithStrings(callback); |
| 42 } else if (path == kPromoCSSPath) { | 77 } else if (path == kPromoCSSPath) { |
| 43 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_CSS, callback); | 78 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_CSS, callback); |
| 44 } else if (path == kPromoJSPath) { | 79 } else if (path == kPromoJSPath) { |
| 45 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_JS, callback); | 80 SendResource(IDR_CONTEXTUAL_SEARCH_PROMO_JS, callback); |
| 81 } else if (path == kPromoConfigPath) { |
| 82 SendConfigResource(callback); |
| 46 } else if (path == kRobotoWoffPath) { | 83 } else if (path == kRobotoWoffPath) { |
| 47 SendResource(IDR_ROBOTO_WOFF, callback); | 84 SendResource(IDR_ROBOTO_WOFF, callback); |
| 48 } else if (path == kRobotoWoff2Path) { | 85 } else if (path == kRobotoWoff2Path) { |
| 49 SendResource(IDR_ROBOTO_WOFF2, callback); | 86 SendResource(IDR_ROBOTO_WOFF2, callback); |
| 50 } else { | 87 } else { |
| 51 callback.Run(NULL); | 88 callback.Run(NULL); |
| 52 } | 89 } |
| 53 } | 90 } |
| 54 | 91 |
| 55 std::string ContextualSearchPromoSourceAndroid::GetSource() const { | 92 std::string ContextualSearchPromoSourceAndroid::GetSource() const { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 77 return false; | 114 return false; |
| 78 } | 115 } |
| 79 | 116 |
| 80 void ContextualSearchPromoSourceAndroid::SendResource( | 117 void ContextualSearchPromoSourceAndroid::SendResource( |
| 81 int resource_id, const content::URLDataSource::GotDataCallback& callback) { | 118 int resource_id, const content::URLDataSource::GotDataCallback& callback) { |
| 82 scoped_refptr<base::RefCountedStaticMemory> response( | 119 scoped_refptr<base::RefCountedStaticMemory> response( |
| 83 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); | 120 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); |
| 84 callback.Run(response.get()); | 121 callback.Run(response.get()); |
| 85 } | 122 } |
| 86 | 123 |
| 124 void ContextualSearchPromoSourceAndroid::SendConfigResource( |
| 125 const content::URLDataSource::GotDataCallback& callback) { |
| 126 std::string response = GetConfigData(); |
| 127 callback.Run(base::RefCountedString::TakeString(&response)); |
| 128 } |
| 129 |
| 87 void ContextualSearchPromoSourceAndroid::SendHtmlWithStrings( | 130 void ContextualSearchPromoSourceAndroid::SendHtmlWithStrings( |
| 88 const content::URLDataSource::GotDataCallback& callback) { | 131 const content::URLDataSource::GotDataCallback& callback) { |
| 89 base::DictionaryValue strings_data; | 132 base::DictionaryValue strings_data; |
| 90 strings_data.SetString( | 133 strings_data.SetString( |
| 91 "description", | 134 "description", |
| 92 l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_DESCRIPTION)); | 135 l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_DESCRIPTION)); |
| 93 strings_data.SetString( | 136 strings_data.SetString( |
| 94 "heading", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_HEADER)); | 137 "heading", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_HEADER)); |
| 95 strings_data.SetString( | 138 strings_data.SetString( |
| 96 "optIn", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_OPTIN)); | 139 "optIn", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_OPTIN)); |
| 97 strings_data.SetString( | 140 strings_data.SetString( |
| 98 "optOut", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_OPTOUT)); | 141 "optOut", l10n_util::GetStringUTF16(IDS_CONTEXTUAL_SEARCH_PROMO_OPTOUT)); |
| 99 base::StringPiece html( | 142 base::StringPiece html( |
| 100 ResourceBundle::GetSharedInstance().GetRawDataResource( | 143 ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 101 IDR_CONTEXTUAL_SEARCH_PROMO_HTML)); | 144 IDR_CONTEXTUAL_SEARCH_PROMO_HTML)); |
| 102 webui::UseVersion2 version; | 145 webui::UseVersion2 version; |
| 103 std::string response(webui::GetI18nTemplateHtml(html, &strings_data)); | 146 std::string response(webui::GetI18nTemplateHtml(html, &strings_data)); |
| 104 callback.Run(base::RefCountedString::TakeString(&response)); | 147 callback.Run(base::RefCountedString::TakeString(&response)); |
| 105 } | 148 } |
| OLD | NEW |