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/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "base/values.h" | 15 #include "base/values.h" |
15 #include "chrome/browser/search/instant_io_context.h" | 16 #include "chrome/browser/search/instant_io_context.h" |
16 #include "chrome/browser/search/search.h" | 17 #include "chrome/browser/search/search.h" |
17 #include "chrome/browser/search_engines/template_url_service_factory.h" | 18 #include "chrome/browser/search_engines/template_url_service_factory.h" |
18 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
19 #include "chrome/grit/generated_resources.h" | 20 #include "chrome/grit/generated_resources.h" |
20 #include "components/search_engines/template_url_prepopulate_data.h" | 21 #include "components/search_engines/template_url_prepopulate_data.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 return false; | 72 return false; |
72 | 73 |
73 const TemplateURL* default_provider = | 74 const TemplateURL* default_provider = |
74 template_url_service->GetDefaultSearchProvider(); | 75 template_url_service->GetDefaultSearchProvider(); |
75 return default_provider && | 76 return default_provider && |
76 (TemplateURLPrepopulateData::GetEngineType( | 77 (TemplateURLPrepopulateData::GetEngineType( |
77 *default_provider, template_url_service->search_terms_data()) == | 78 *default_provider, template_url_service->search_terms_data()) == |
78 SEARCH_ENGINE_GOOGLE); | 79 SEARCH_ENGINE_GOOGLE); |
79 } | 80 } |
80 | 81 |
| 82 // Returns whether we are in the Fast NTP experiment or not. |
| 83 bool IsLocalNTPFastEnabled() { |
| 84 return StartsWithASCII(base::FieldTrialList::FindFullName("LocalNTPFast"), |
| 85 "Enabled", true); |
| 86 } |
| 87 |
| 88 // Serves a particular resource. |
| 89 // Used for bypassing the default resources when we are in an experiment. |
| 90 void SendResource(int resource_id, |
| 91 const content::URLDataSource::GotDataCallback& callback) { |
| 92 scoped_refptr<base::RefCountedStaticMemory> response( |
| 93 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(resource_id)); |
| 94 callback.Run(response.get()); |
| 95 } |
| 96 |
81 // Adds a localized string keyed by resource id to the dictionary. | 97 // Adds a localized string keyed by resource id to the dictionary. |
82 void AddString(base::DictionaryValue* dictionary, | 98 void AddString(base::DictionaryValue* dictionary, |
83 const std::string& key, | 99 const std::string& key, |
84 int resource_id) { | 100 int resource_id) { |
85 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id)); | 101 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id)); |
86 } | 102 } |
87 | 103 |
88 // Adds a localized string for the Google searchbox placeholder text. | 104 // Adds a localized string for the Google searchbox placeholder text. |
89 void AddGoogleSearchboxPlaceholderString(base::DictionaryValue* dictionary) { | 105 void AddGoogleSearchboxPlaceholderString(base::DictionaryValue* dictionary) { |
90 base::string16 placeholder = l10n_util::GetStringFUTF16( | 106 base::string16 placeholder = l10n_util::GetStringFUTF16( |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
158 const std::string& path, | 174 const std::string& path, |
159 int render_process_id, | 175 int render_process_id, |
160 int render_frame_id, | 176 int render_frame_id, |
161 const content::URLDataSource::GotDataCallback& callback) { | 177 const content::URLDataSource::GotDataCallback& callback) { |
162 const std::string stripped_path = StripParameters(path); | 178 const std::string stripped_path = StripParameters(path); |
163 if (stripped_path == kConfigDataFilename) { | 179 if (stripped_path == kConfigDataFilename) { |
164 std::string config_data_js = GetConfigData(profile_); | 180 std::string config_data_js = GetConfigData(profile_); |
165 callback.Run(base::RefCountedString::TakeString(&config_data_js)); | 181 callback.Run(base::RefCountedString::TakeString(&config_data_js)); |
166 return; | 182 return; |
167 } | 183 } |
| 184 if (IsLocalNTPFastEnabled()) { |
| 185 if (stripped_path == "local-ntp.html") { |
| 186 return SendResource(IDR_LOCAL_NTP_FAST_HTML, callback); |
| 187 } else if (stripped_path == "local-ntp.js") { |
| 188 return SendResource(IDR_LOCAL_NTP_FAST_JS, callback); |
| 189 } else if (stripped_path == "local-ntp.css") { |
| 190 return SendResource(IDR_LOCAL_NTP_FAST_CSS, callback); |
| 191 } |
| 192 } |
168 float scale = 1.0f; | 193 float scale = 1.0f; |
169 std::string filename; | 194 std::string filename; |
170 webui::ParsePathAndScale( | 195 webui::ParsePathAndScale( |
171 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); | 196 GURL(GetLocalNtpPath() + stripped_path), &filename, &scale); |
172 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); | 197 ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale); |
173 for (size_t i = 0; i < arraysize(kResources); ++i) { | 198 for (size_t i = 0; i < arraysize(kResources); ++i) { |
174 if (filename == kResources[i].filename) { | 199 if (filename == kResources[i].filename) { |
175 scoped_refptr<base::RefCountedStaticMemory> response( | 200 scoped_refptr<base::RefCountedStaticMemory> response( |
176 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | 201 ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
177 kResources[i].identifier, scale_factor)); | 202 kResources[i].identifier, scale_factor)); |
178 callback.Run(response.get()); | 203 callback.Run(response.get()); |
179 return; | 204 return; |
180 } | 205 } |
181 } | 206 } |
182 callback.Run(NULL); | 207 callback.Run(NULL); |
183 } | 208 } |
184 | 209 |
185 std::string LocalNtpSource::GetMimeType( | 210 std::string LocalNtpSource::GetMimeType( |
186 const std::string& path) const { | 211 const std::string& path) const { |
187 const std::string& stripped_path = StripParameters(path); | 212 const std::string stripped_path = StripParameters(path); |
188 std::string filename; | |
189 webui::ParsePathAndScale(GURL(GetLocalNtpPath() + stripped_path), &filename, | |
190 NULL); | |
191 for (size_t i = 0; i < arraysize(kResources); ++i) { | 213 for (size_t i = 0; i < arraysize(kResources); ++i) { |
192 if (filename == kResources[i].filename) | 214 if (stripped_path == kResources[i].filename) |
193 return kResources[i].mime_type; | 215 return kResources[i].mime_type; |
194 } | 216 } |
195 return std::string(); | 217 return std::string(); |
196 } | 218 } |
197 | 219 |
198 bool LocalNtpSource::ShouldServiceRequest( | 220 bool LocalNtpSource::ShouldServiceRequest( |
199 const net::URLRequest* request) const { | 221 const net::URLRequest* request) const { |
200 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost); | 222 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost); |
201 if (!InstantIOContext::ShouldServiceRequest(request)) | 223 if (!InstantIOContext::ShouldServiceRequest(request)) |
202 return false; | 224 return false; |
203 | 225 |
204 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { | 226 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) { |
205 std::string filename; | 227 std::string filename; |
206 webui::ParsePathAndScale(request->url(), &filename, NULL); | 228 webui::ParsePathAndScale(request->url(), &filename, NULL); |
207 for (size_t i = 0; i < arraysize(kResources); ++i) { | 229 for (size_t i = 0; i < arraysize(kResources); ++i) { |
208 if (filename == kResources[i].filename) | 230 if (filename == kResources[i].filename) |
209 return true; | 231 return true; |
210 } | 232 } |
211 } | 233 } |
212 return false; | 234 return false; |
213 } | 235 } |
214 | 236 |
215 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { | 237 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { |
216 // Allow embedding of most visited iframes. | 238 // Allow embedding of most visited iframes. |
217 return base::StringPrintf("frame-src %s;", | 239 return base::StringPrintf("frame-src %s;", |
218 chrome::kChromeSearchMostVisitedUrl); | 240 chrome::kChromeSearchMostVisitedUrl); |
219 } | 241 } |
OLD | NEW |