| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/local_ntp_source.h" | 5 #include "chrome/browser/search/local_ntp_source.h" |
| 6 | 6 |
| 7 #include "base/json/json_string_value_serializer.h" | 7 #include "base/json/json_string_value_serializer.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/metrics/field_trial.h" |
| 11 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 13 #include "base/values.h" | 14 #include "base/values.h" |
| 14 #include "chrome/browser/search/instant_io_context.h" | 15 #include "chrome/browser/search/instant_io_context.h" |
| 15 #include "chrome/browser/search/search.h" | 16 #include "chrome/browser/search/search.h" |
| 16 #include "chrome/browser/search_engines/template_url_service_factory.h" | 17 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 17 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
| 18 #include "components/search_engines/template_url_prepopulate_data.h" | 19 #include "components/search_engines/template_url_prepopulate_data.h" |
| 19 #include "components/search_engines/template_url_service.h" | 20 #include "components/search_engines/template_url_service.h" |
| 20 #include "grit/browser_resources.h" | 21 #include "grit/browser_resources.h" |
| 21 #include "grit/generated_resources.h" | 22 #include "grit/generated_resources.h" |
| 22 #include "grit/theme_resources.h" | 23 #include "grit/theme_resources.h" |
| 23 #include "grit/ui_resources.h" | 24 #include "grit/ui_resources.h" |
| 24 #include "net/url_request/url_request.h" | 25 #include "net/url_request/url_request.h" |
| 25 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
| 26 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| 27 #include "ui/base/webui/jstemplate_builder.h" | 28 #include "ui/base/webui/jstemplate_builder.h" |
| 28 #include "ui/base/webui/web_ui_util.h" | 29 #include "ui/base/webui/web_ui_util.h" |
| 29 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 34 // Constants related to the new NTP design field trial. |
| 35 const char kNewNTPDesignFieldTrialName[] = "NewNTPDesign"; |
| 36 const char kNewNTPDesignFieldTrialEnabledPrefix[] = "Enabled"; |
| 37 |
| 38 // Class name to be used for the new design in local resources. |
| 39 const char kNewNTPDesignClassName[] = "md"; |
| 40 |
| 33 // Signifies a locally constructed resource, i.e. not from grit/. | 41 // Signifies a locally constructed resource, i.e. not from grit/. |
| 34 const int kLocalResource = -1; | 42 const int kLocalResource = -1; |
| 35 | 43 |
| 36 const char kConfigDataFilename[] = "config.js"; | 44 const char kConfigDataFilename[] = "config.js"; |
| 45 const char kLocalNTPFilename[] = "local-ntp.html"; |
| 37 | 46 |
| 38 const struct Resource{ | 47 const struct Resource{ |
| 39 const char* filename; | 48 const char* filename; |
| 40 int identifier; | 49 int identifier; |
| 41 const char* mime_type; | 50 const char* mime_type; |
| 42 } kResources[] = { | 51 } kResources[] = { |
| 43 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, | 52 { kLocalNTPFilename, IDR_LOCAL_NTP_HTML, "text/html" }, |
| 44 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, | 53 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, |
| 45 { kConfigDataFilename, kLocalResource, "application/javascript" }, | 54 { kConfigDataFilename, kLocalResource, "application/javascript" }, |
| 46 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, | 55 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, |
| 47 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, | 56 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, |
| 48 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" }, | 57 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" }, |
| 49 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" }, | 58 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" }, |
| 50 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" }, | 59 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" }, |
| 51 { "images/google_logo.png", IDR_LOCAL_NTP_IMAGES_LOGO_PNG, "image/png" }, | 60 { "images/google_logo.png", IDR_LOCAL_NTP_IMAGES_LOGO_PNG, "image/png" }, |
| 52 { "images/white_google_logo.png", | 61 { "images/white_google_logo.png", |
| 53 IDR_LOCAL_NTP_IMAGES_WHITE_LOGO_PNG, "image/png" }, | 62 IDR_LOCAL_NTP_IMAGES_WHITE_LOGO_PNG, "image/png" }, |
| (...skipping 14 matching lines...) Expand all Loading... |
| 68 return false; | 77 return false; |
| 69 | 78 |
| 70 const TemplateURL* default_provider = | 79 const TemplateURL* default_provider = |
| 71 template_url_service->GetDefaultSearchProvider(); | 80 template_url_service->GetDefaultSearchProvider(); |
| 72 return default_provider && | 81 return default_provider && |
| 73 (TemplateURLPrepopulateData::GetEngineType( | 82 (TemplateURLPrepopulateData::GetEngineType( |
| 74 *default_provider, template_url_service->search_terms_data()) == | 83 *default_provider, template_url_service->search_terms_data()) == |
| 75 SEARCH_ENGINE_GOOGLE); | 84 SEARCH_ENGINE_GOOGLE); |
| 76 } | 85 } |
| 77 | 86 |
| 87 // Returns whether the user is part of a group where the new NTP design is |
| 88 // enabled. |
| 89 bool IsNewDesignEnabled() { |
| 90 return StartsWithASCII( |
| 91 base::FieldTrialList::FindFullName(kNewNTPDesignFieldTrialName), |
| 92 kNewNTPDesignFieldTrialEnabledPrefix, true); |
| 93 } |
| 94 |
| 78 // Adds a localized string keyed by resource id to the dictionary. | 95 // Adds a localized string keyed by resource id to the dictionary. |
| 79 void AddString(base::DictionaryValue* dictionary, | 96 void AddString(base::DictionaryValue* dictionary, |
| 80 const std::string& key, | 97 const std::string& key, |
| 81 int resource_id) { | 98 int resource_id) { |
| 82 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id)); | 99 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id)); |
| 83 } | 100 } |
| 84 | 101 |
| 85 // Populates |translated_strings| dictionary for the local NTP. | 102 // Populates |translated_strings| dictionary for the local NTP. |
| 86 scoped_ptr<base::DictionaryValue> GetTranslatedStrings() { | 103 scoped_ptr<base::DictionaryValue> GetTranslatedStrings() { |
| 87 scoped_ptr<base::DictionaryValue> translated_strings( | 104 scoped_ptr<base::DictionaryValue> translated_strings( |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 const std::string& path, | 160 const std::string& path, |
| 144 int render_process_id, | 161 int render_process_id, |
| 145 int render_frame_id, | 162 int render_frame_id, |
| 146 const content::URLDataSource::GotDataCallback& callback) { | 163 const content::URLDataSource::GotDataCallback& callback) { |
| 147 const std::string stripped_path = StripParameters(path); | 164 const std::string stripped_path = StripParameters(path); |
| 148 if (stripped_path == kConfigDataFilename) { | 165 if (stripped_path == kConfigDataFilename) { |
| 149 std::string config_data_js = GetConfigData(profile_); | 166 std::string config_data_js = GetConfigData(profile_); |
| 150 callback.Run(base::RefCountedString::TakeString(&config_data_js)); | 167 callback.Run(base::RefCountedString::TakeString(&config_data_js)); |
| 151 return; | 168 return; |
| 152 } | 169 } |
| 170 if (stripped_path == kLocalNTPFilename) { |
| 171 SendResourceWithClass( |
| 172 IDR_LOCAL_NTP_HTML, IsNewDesignEnabled() ? kNewNTPDesignClassName : "", |
| 173 callback); |
| 174 return; |
| 175 } |
| 153 float scale = 1.0f; | 176 float scale = 1.0f; |
| 154 std::string filename; | 177 std::string filename; |
| 155 webui::ParsePathAndScale( | 178 webui::ParsePathAndScale( |
| 156 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); | 179 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); |
| 157 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); | 180 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); |
| 158 for (size_t i = 0; i < arraysize(kResources); ++i) { | 181 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| 159 if (filename == kResources[i].filename) { | 182 if (filename == kResources[i].filename) { |
| 160 scoped_refptr<base::RefCountedStaticMemory> response( | 183 scoped_refptr<base::RefCountedStaticMemory> response( |
| 161 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | 184 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
| 162 kResources[i].identifier, scale_factor)); | 185 kResources[i].identifier, scale_factor)); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 192 } | 215 } |
| 193 } | 216 } |
| 194 return false; | 217 return false; |
| 195 } | 218 } |
| 196 | 219 |
| 197 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { | 220 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { |
| 198 // Allow embedding of most visited iframes. | 221 // Allow embedding of most visited iframes. |
| 199 return base::StringPrintf("frame-src %s;", | 222 return base::StringPrintf("frame-src %s;", |
| 200 chrome::kChromeSearchMostVisitedUrl); | 223 chrome::kChromeSearchMostVisitedUrl); |
| 201 } | 224 } |
| 225 |
| 226 void LocalNtpSource::SendResourceWithClass( |
| 227 int resource_id, |
| 228 const std::string& class_name, |
| 229 const content::URLDataSource::GotDataCallback& callback) { |
| 230 base::StringPiece resource_data = |
| 231 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
| 232 std::string response(resource_data.as_string()); |
| 233 ReplaceFirstSubstringAfterOffset(&response, 0, "{{CLASS}}", class_name); |
| 234 callback.Run(base::RefCountedString::TakeString(&response)); |
| 235 } |
| OLD | NEW |