| 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" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "ui/base/webui/jstemplate_builder.h" | 28 #include "ui/base/webui/jstemplate_builder.h" |
| 29 #include "ui/base/webui/web_ui_util.h" | 29 #include "ui/base/webui/web_ui_util.h" |
| 30 #include "url/gurl.h" | 30 #include "url/gurl.h" |
| 31 | 31 |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 // Constants related to the Material Design NTP field trial. | 34 // Constants related to the Material Design NTP field trial. |
| 35 const char kMaterialDesignNTPFieldTrialName[] = "MaterialDesignNTP"; | 35 const char kMaterialDesignNTPFieldTrialName[] = "MaterialDesignNTP"; |
| 36 const char kMaterialDesignNTPFieldTrialEnabledPrefix[] = "Enabled"; | 36 const char kMaterialDesignNTPFieldTrialEnabledPrefix[] = "Enabled"; |
| 37 | 37 |
| 38 // Class name to be used for the new design in local resources. | 38 // Name to be used for the new design in local resources. |
| 39 const char kMaterialDesignNTPClassName[] = "md"; | 39 const char kMaterialDesignNTPName[] = "md"; |
| 40 | 40 |
| 41 // Signifies a locally constructed resource, i.e. not from grit/. | 41 // Signifies a locally constructed resource, i.e. not from grit/. |
| 42 const int kLocalResource = -1; | 42 const int kLocalResource = -1; |
| 43 | 43 |
| 44 const char kConfigDataFilename[] = "config.js"; | 44 const char kConfigDataFilename[] = "config.js"; |
| 45 const char kLocalNTPFilename[] = "local-ntp.html"; | 45 const char kLocalNTPFilename[] = "local-ntp.html"; |
| 46 | 46 |
| 47 const struct Resource{ | 47 const struct Resource{ |
| 48 const char* filename; | 48 const char* filename; |
| 49 int identifier; | 49 int identifier; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 return translated_strings.Pass(); | 120 return translated_strings.Pass(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 // Returns a JS dictionary of configuration data for the local NTP. | 123 // Returns a JS dictionary of configuration data for the local NTP. |
| 124 std::string GetConfigData(Profile* profile) { | 124 std::string GetConfigData(Profile* profile) { |
| 125 base::DictionaryValue config_data; | 125 base::DictionaryValue config_data; |
| 126 config_data.Set("translatedStrings", GetTranslatedStrings().release()); | 126 config_data.Set("translatedStrings", GetTranslatedStrings().release()); |
| 127 config_data.SetBoolean("isGooglePage", | 127 config_data.SetBoolean("isGooglePage", |
| 128 DefaultSearchProviderIsGoogle(profile) && | 128 DefaultSearchProviderIsGoogle(profile) && |
| 129 chrome::ShouldShowGoogleLocalNTP()); | 129 chrome::ShouldShowGoogleLocalNTP()); |
| 130 if (IsMaterialDesignEnabled()) { |
| 131 scoped_ptr<base::Value> design_value( |
| 132 new base::StringValue(kMaterialDesignNTPName)); |
| 133 config_data.Set("ntpDesignName", design_value.release()); |
| 134 } |
| 130 | 135 |
| 131 // Serialize the dictionary. | 136 // Serialize the dictionary. |
| 132 std::string js_text; | 137 std::string js_text; |
| 133 JSONStringValueSerializer serializer(&js_text); | 138 JSONStringValueSerializer serializer(&js_text); |
| 134 serializer.Serialize(config_data); | 139 serializer.Serialize(config_data); |
| 135 | 140 |
| 136 std::string config_data_js; | 141 std::string config_data_js; |
| 137 config_data_js.append("var configData = "); | 142 config_data_js.append("var configData = "); |
| 138 config_data_js.append(js_text); | 143 config_data_js.append(js_text); |
| 139 config_data_js.append(";"); | 144 config_data_js.append(";"); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 164 const content::URLDataSource::GotDataCallback& callback) { | 169 const content::URLDataSource::GotDataCallback& callback) { |
| 165 const std::string stripped_path = StripParameters(path); | 170 const std::string stripped_path = StripParameters(path); |
| 166 if (stripped_path == kConfigDataFilename) { | 171 if (stripped_path == kConfigDataFilename) { |
| 167 std::string config_data_js = GetConfigData(profile_); | 172 std::string config_data_js = GetConfigData(profile_); |
| 168 callback.Run(base::RefCountedString::TakeString(&config_data_js)); | 173 callback.Run(base::RefCountedString::TakeString(&config_data_js)); |
| 169 return; | 174 return; |
| 170 } | 175 } |
| 171 if (stripped_path == kLocalNTPFilename) { | 176 if (stripped_path == kLocalNTPFilename) { |
| 172 SendResourceWithClass( | 177 SendResourceWithClass( |
| 173 IDR_LOCAL_NTP_HTML, | 178 IDR_LOCAL_NTP_HTML, |
| 174 IsMaterialDesignEnabled() ? kMaterialDesignNTPClassName : "", | 179 IsMaterialDesignEnabled() ? kMaterialDesignNTPName : "", |
| 175 callback); | 180 callback); |
| 176 return; | 181 return; |
| 177 } | 182 } |
| 178 float scale = 1.0f; | 183 float scale = 1.0f; |
| 179 std::string filename; | 184 std::string filename; |
| 180 webui::ParsePathAndScale( | 185 webui::ParsePathAndScale( |
| 181 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); | 186 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); |
| 182 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); | 187 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); |
| 183 for (size_t i = 0; i < arraysize(kResources); ++i) { | 188 for (size_t i = 0; i < arraysize(kResources); ++i) { |
| 184 if (filename == kResources[i].filename) { | 189 if (filename == kResources[i].filename) { |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 void LocalNtpSource::SendResourceWithClass( | 233 void LocalNtpSource::SendResourceWithClass( |
| 229 int resource_id, | 234 int resource_id, |
| 230 const std::string& class_name, | 235 const std::string& class_name, |
| 231 const content::URLDataSource::GotDataCallback& callback) { | 236 const content::URLDataSource::GotDataCallback& callback) { |
| 232 base::StringPiece resource_data = | 237 base::StringPiece resource_data = |
| 233 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); | 238 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id); |
| 234 std::string response(resource_data.as_string()); | 239 std::string response(resource_data.as_string()); |
| 235 ReplaceFirstSubstringAfterOffset(&response, 0, "{{CLASS}}", class_name); | 240 ReplaceFirstSubstringAfterOffset(&response, 0, "{{CLASS}}", class_name); |
| 236 callback.Run(base::RefCountedString::TakeString(&response)); | 241 callback.Run(base::RefCountedString::TakeString(&response)); |
| 237 } | 242 } |
| OLD | NEW |