OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/web_resource/web_resource_service.h" | 5 #include "chrome/browser/web_resource/web_resource_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/browser_process.h" | 14 #include "chrome/browser/browser_process.h" |
15 #include "chrome/browser/browser_thread.h" | 15 #include "chrome/browser/browser_thread.h" |
16 #include "chrome/browser/extensions/extensions_service.h" | |
17 #include "chrome/browser/platform_util.h" | |
16 #include "chrome/browser/profile.h" | 18 #include "chrome/browser/profile.h" |
19 #include "chrome/browser/sync/sync_ui_util.h" | |
17 #include "chrome/common/chrome_switches.h" | 20 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/net/url_fetcher.h" | 21 #include "chrome/common/net/url_fetcher.h" |
19 #include "chrome/common/notification_service.h" | 22 #include "chrome/common/notification_service.h" |
20 #include "chrome/common/notification_type.h" | 23 #include "chrome/common/notification_type.h" |
21 #include "chrome/common/pref_names.h" | 24 #include "chrome/common/pref_names.h" |
22 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
23 #include "net/base/load_flags.h" | 26 #include "net/base/load_flags.h" |
24 #include "net/url_request/url_request_status.h" | 27 #include "net/url_request/url_request_status.h" |
25 | 28 |
29 namespace { | |
30 | |
31 // Delay on first fetch so we don't interfere with startup. | |
32 static const int kStartResourceFetchDelay = 5000; | |
33 | |
34 // Long delay between calls to update the cache (48 hours). | |
35 static const int kLongCacheUpdateDelay = 48 * 60 * 60 * 1000; | |
36 | |
37 // Short delay between calls to update the cache (8 hours). | |
38 static const int kShortCacheUpdateDelay = 8 * 60 * 60 * 1000; | |
39 | |
40 } // namespace | |
41 | |
26 const char* WebResourceService::kCurrentTipPrefName = "current_tip"; | 42 const char* WebResourceService::kCurrentTipPrefName = "current_tip"; |
27 const char* WebResourceService::kTipCachePrefName = "tips"; | 43 const char* WebResourceService::kTipCachePrefName = "tips"; |
28 | 44 |
29 class WebResourceService::WebResourceFetcher | 45 class WebResourceService::WebResourceFetcher |
30 : public URLFetcher::Delegate { | 46 : public URLFetcher::Delegate { |
31 public: | 47 public: |
32 explicit WebResourceFetcher(WebResourceService* web_resource_service) : | 48 explicit WebResourceFetcher(WebResourceService* web_resource_service) : |
33 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), | 49 ALLOW_THIS_IN_INITIALIZER_LIST(fetcher_factory_(this)), |
34 web_resource_service_(web_resource_service) { | 50 web_resource_service_(web_resource_service) { |
35 } | 51 } |
36 | 52 |
37 // Delay initial load of resource data into cache so as not to interfere | 53 // Delay initial load of resource data into cache so as not to interfere |
38 // with startup time. | 54 // with startup time. |
39 void StartAfterDelay(int delay_ms) { | 55 void StartAfterDelay(int delay_ms) { |
40 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 56 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
41 fetcher_factory_.NewRunnableMethod(&WebResourceFetcher::StartFetch), | 57 fetcher_factory_.NewRunnableMethod(&WebResourceFetcher::StartFetch), |
42 delay_ms); | 58 delay_ms); |
43 } | 59 } |
44 | 60 |
45 // Initializes the fetching of data from the resource server. Data | 61 // Initializes the fetching of data from the resource server. Data |
46 // load calls OnURLFetchComplete. | 62 // load calls OnURLFetchComplete. |
47 void StartFetch() { | 63 void StartFetch() { |
48 // Balanced in OnURLFetchComplete. | 64 // Balanced in OnURLFetchComplete. |
49 web_resource_service_->AddRef(); | 65 web_resource_service_->AddRef(); |
50 // First, put our next cache load on the MessageLoop. | 66 // First, put our next cache load on the MessageLoop. |
51 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 67 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
52 fetcher_factory_.NewRunnableMethod(&WebResourceFetcher::StartFetch), | 68 fetcher_factory_.NewRunnableMethod(&WebResourceFetcher::StartFetch), |
53 kCacheUpdateDelay); | 69 web_resource_service_->cache_update_delay()); |
54 // If we are still fetching data, exit. | 70 // If we are still fetching data, exit. |
55 if (web_resource_service_->in_fetch_) | 71 if (web_resource_service_->in_fetch_) |
56 return; | 72 return; |
57 else | 73 else |
58 web_resource_service_->in_fetch_ = true; | 74 web_resource_service_->in_fetch_ = true; |
59 | 75 |
60 url_fetcher_.reset(new URLFetcher(GURL( | 76 url_fetcher_.reset(new URLFetcher(GURL( |
61 web_resource_service_->web_resource_server_), | 77 web_resource_service_->web_resource_server_), |
62 URLFetcher::GET, this)); | 78 URLFetcher::GET, this)); |
63 // Do not let url fetcher affect existing state in profile (by setting | 79 // Do not let url fetcher affect existing state in profile (by setting |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 scoped_refptr<WebResourceService> web_resource_service_; | 196 scoped_refptr<WebResourceService> web_resource_service_; |
181 | 197 |
182 // Holds raw JSON string. | 198 // Holds raw JSON string. |
183 const std::string& json_data_; | 199 const std::string& json_data_; |
184 | 200 |
185 // True if we got a response from the utility process and have cleaned up | 201 // True if we got a response from the utility process and have cleaned up |
186 // already. | 202 // already. |
187 bool got_response_; | 203 bool got_response_; |
188 }; | 204 }; |
189 | 205 |
190 // Server for custom logo signals. | 206 // Server for dynamically loaded NTP HTML elements. TODO(mirandac): append |
191 const char* WebResourceService::kDefaultResourceServer = | 207 // locale for future usage, when we're serving localizable strings. |
208 const char* WebResourceService::kDefaultWebResourceServer = | |
192 "https://www.google.com/support/chrome/bin/topic/30248/inproduct"; | 209 "https://www.google.com/support/chrome/bin/topic/30248/inproduct"; |
193 | 210 |
194 WebResourceService::WebResourceService(Profile* profile) | 211 WebResourceService::WebResourceService(Profile* profile) |
195 : prefs_(profile->GetPrefs()), | 212 : prefs_(profile->GetPrefs()), |
213 profile_(profile), | |
196 in_fetch_(false) { | 214 in_fetch_(false) { |
197 Init(); | 215 Init(); |
198 } | 216 } |
199 | 217 |
200 WebResourceService::~WebResourceService() { } | 218 WebResourceService::~WebResourceService() { } |
201 | 219 |
202 void WebResourceService::Init() { | 220 void WebResourceService::Init() { |
221 std::string channel = platform_util::GetVersionStringModifier(); | |
222 // Much shorter update time if looking for a promo to display. | |
223 cache_update_delay_ = | |
224 WebResourceServiceUtil::CanShowPromo(profile_) ? kShortCacheUpdateDelay : | |
225 kLongCacheUpdateDelay; | |
203 resource_dispatcher_host_ = g_browser_process->resource_dispatcher_host(); | 226 resource_dispatcher_host_ = g_browser_process->resource_dispatcher_host(); |
204 web_resource_fetcher_.reset(new WebResourceFetcher(this)); | 227 web_resource_fetcher_.reset(new WebResourceFetcher(this)); |
205 prefs_->RegisterStringPref(prefs::kNTPWebResourceCacheUpdate, "0"); | 228 prefs_->RegisterStringPref(prefs::kNTPWebResourceCacheUpdate, "0"); |
206 prefs_->RegisterRealPref(prefs::kNTPCustomLogoStart, 0); | 229 prefs_->RegisterRealPref(prefs::kNTPCustomLogoStart, 0); |
207 prefs_->RegisterRealPref(prefs::kNTPCustomLogoEnd, 0); | 230 prefs_->RegisterRealPref(prefs::kNTPCustomLogoEnd, 0); |
231 prefs_->RegisterRealPref(prefs::kNTPPromoStart, 0); | |
232 prefs_->RegisterRealPref(prefs::kNTPPromoEnd, 0); | |
233 prefs_->RegisterStringPref(prefs::kNTPPromoLine, std::string()); | |
234 prefs_->RegisterBooleanPref(prefs::kNTPPromoClosed, false); | |
208 | 235 |
209 if (prefs_->HasPrefPath(prefs::kNTPLogoResourceServer)) { | 236 if (prefs_->HasPrefPath(prefs::kNTPWebResourceCache)) { |
210 web_resource_server_ = prefs_->GetString(prefs::kNTPLogoResourceServer); | 237 web_resource_server_ = prefs_->GetString(prefs::kNTPWebResourceCache); |
211 return; | 238 return; |
212 } | 239 } |
213 | 240 |
214 // If we have not yet set a server, reset and force an immediate update. | 241 // If we have not yet set a server, reset and force an immediate update. |
215 web_resource_server_ = kDefaultResourceServer; | 242 web_resource_server_ = kDefaultWebResourceServer; |
216 prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, ""); | 243 prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, ""); |
217 } | 244 } |
218 | 245 |
219 void WebResourceService::EndFetch() { | 246 void WebResourceService::EndFetch() { |
220 in_fetch_ = false; | 247 in_fetch_ = false; |
221 } | 248 } |
222 | 249 |
223 void WebResourceService::OnWebResourceUnpacked( | 250 void WebResourceService::OnWebResourceUnpacked( |
224 const DictionaryValue& parsed_json) { | 251 const DictionaryValue& parsed_json) { |
225 UnpackLogoSignal(parsed_json); | 252 UnpackLogoSignal(parsed_json); |
253 if (WebResourceServiceUtil::CanShowPromo(profile_)) | |
254 UnpackPromoSignal(parsed_json); | |
226 EndFetch(); | 255 EndFetch(); |
227 } | 256 } |
228 | 257 |
229 void WebResourceService::StartAfterDelay() { | 258 void WebResourceService::StartAfterDelay() { |
230 int delay = kStartResourceFetchDelay; | 259 int delay = kStartResourceFetchDelay; |
231 // Check whether we have ever put a value in the web resource cache; | 260 // Check whether we have ever put a value in the web resource cache; |
232 // if so, pull it out and see if it's time to update again. | 261 // if so, pull it out and see if it's time to update again. |
233 if (prefs_->HasPrefPath(prefs::kNTPWebResourceCacheUpdate)) { | 262 if (prefs_->HasPrefPath(prefs::kNTPWebResourceCacheUpdate)) { |
234 std::string last_update_pref = | 263 std::string last_update_pref = |
235 prefs_->GetString(prefs::kNTPWebResourceCacheUpdate); | 264 prefs_->GetString(prefs::kNTPWebResourceCacheUpdate); |
236 if (!last_update_pref.empty()) { | 265 if (!last_update_pref.empty()) { |
237 double last_update_value; | 266 double last_update_value; |
238 base::StringToDouble(last_update_pref, &last_update_value); | 267 base::StringToDouble(last_update_pref, &last_update_value); |
239 int ms_until_update = kCacheUpdateDelay - | 268 int ms_until_update = cache_update_delay_ - |
240 static_cast<int>((base::Time::Now() - base::Time::FromDoubleT( | 269 static_cast<int>((base::Time::Now() - base::Time::FromDoubleT( |
241 last_update_value)).InMilliseconds()); | 270 last_update_value)).InMilliseconds()); |
242 | 271 |
243 delay = ms_until_update > kCacheUpdateDelay ? | 272 delay = ms_until_update > cache_update_delay_ ? |
244 kCacheUpdateDelay : (ms_until_update < kStartResourceFetchDelay ? | 273 cache_update_delay_ : (ms_until_update < kStartResourceFetchDelay ? |
245 kStartResourceFetchDelay : ms_until_update); | 274 kStartResourceFetchDelay : ms_until_update); |
246 } | 275 } |
247 } | 276 } |
248 | 277 |
249 // Start fetch and wait for UpdateResourceCache. | 278 // Start fetch and wait for UpdateResourceCache. |
250 web_resource_fetcher_->StartAfterDelay(static_cast<int>(delay)); | 279 web_resource_fetcher_->StartAfterDelay(static_cast<int>(delay)); |
251 } | 280 } |
252 | 281 |
253 void WebResourceService::UpdateResourceCache(const std::string& json_data) { | 282 void WebResourceService::UpdateResourceCache(const std::string& json_data) { |
254 UnpackerClient* client = new UnpackerClient(this, json_data); | 283 UnpackerClient* client = new UnpackerClient(this, json_data); |
255 client->Start(); | 284 client->Start(); |
256 | 285 |
257 // Update resource server and cache update time in preferences. | 286 // Update resource server and cache update time in preferences. |
258 prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, | 287 prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, |
259 base::DoubleToString(base::Time::Now().ToDoubleT())); | 288 base::DoubleToString(base::Time::Now().ToDoubleT())); |
260 prefs_->SetString(prefs::kNTPLogoResourceServer, web_resource_server_); | 289 prefs_->SetString(prefs::kNTPWebResourceServer, web_resource_server_); |
261 } | 290 } |
262 | 291 |
263 void WebResourceService::UnpackTips(const DictionaryValue& parsed_json) { | 292 void WebResourceService::UnpackTips(const DictionaryValue& parsed_json) { |
264 // Get dictionary of cached preferences. | 293 // Get dictionary of cached preferences. |
265 web_resource_cache_ = | 294 web_resource_cache_ = |
266 prefs_->GetMutableDictionary(prefs::kNTPWebResourceCache); | 295 prefs_->GetMutableDictionary(prefs::kNTPWebResourceCache); |
267 | 296 |
268 // The list of individual tips. | 297 // The list of individual tips. |
269 ListValue* tip_holder = new ListValue(); | 298 ListValue* tip_holder = new ListValue(); |
270 web_resource_cache_->Set(WebResourceService::kTipCachePrefName, tip_holder); | 299 web_resource_cache_->Set(WebResourceService::kTipCachePrefName, tip_holder); |
(...skipping 22 matching lines...) Expand all Loading... | |
293 } | 322 } |
294 // If tips exist, set current index to 0. | 323 // If tips exist, set current index to 0. |
295 if (!inproduct.empty()) { | 324 if (!inproduct.empty()) { |
296 web_resource_cache_->SetInteger( | 325 web_resource_cache_->SetInteger( |
297 WebResourceService::kCurrentTipPrefName, 0); | 326 WebResourceService::kCurrentTipPrefName, 0); |
298 } | 327 } |
299 } | 328 } |
300 } | 329 } |
301 } | 330 } |
302 | 331 |
332 void WebResourceService::UnpackPromoSignal(const DictionaryValue& parsed_json) { | |
333 DictionaryValue* topic_dict; | |
334 ListValue* answer_list; | |
335 double old_promo_start = 0; | |
336 double old_promo_end = 0; | |
337 double promo_start = 0; | |
338 double promo_end = 0; | |
339 | |
340 // Check for preexisting start and end values. | |
341 if (prefs_->HasPrefPath(prefs::kNTPPromoStart) && | |
342 prefs_->HasPrefPath(prefs::kNTPPromoEnd)) { | |
343 old_promo_start = prefs_->GetReal(prefs::kNTPPromoStart); | |
344 old_promo_end = prefs_->GetReal(prefs::kNTPPromoEnd); | |
345 } | |
346 | |
347 // Check for newly received start and end values. | |
348 if (parsed_json.GetDictionary("topic", &topic_dict)) { | |
349 if (topic_dict->GetList("answers", &answer_list)) { | |
350 std::string promo_start_string = ""; | |
351 std::string promo_end_string = ""; | |
352 std::string promo_string = ""; | |
353 for (ListValue::const_iterator tip_iter = answer_list->begin(); | |
354 tip_iter != answer_list->end(); ++tip_iter) { | |
355 if (!(*tip_iter)->IsType(Value::TYPE_DICTIONARY)) | |
356 continue; | |
357 DictionaryValue* a_dic = | |
358 static_cast<DictionaryValue*>(*tip_iter); | |
359 std::string promo_signal; | |
360 if (a_dic->GetString("name", &promo_signal)) { | |
361 if (promo_signal == "promo_start") { | |
362 a_dic->GetString("inproduct", &promo_start_string); | |
363 a_dic->GetString("tooltip", &promo_string); | |
364 prefs_->SetString(prefs::kNTPPromoLine, promo_string); | |
365 } else if (promo_signal == "promo_end") { | |
366 a_dic->GetString("inproduct", &promo_end_string); | |
367 } | |
368 } | |
369 } | |
370 if (!promo_start_string.empty() && | |
371 promo_start_string.length() > 0 && | |
372 !promo_end_string.empty() && | |
373 promo_end_string.length() > 0) { | |
374 base::Time start_time; | |
375 base::Time end_time; | |
376 if (base::Time::FromString( | |
377 ASCIIToWide(promo_start_string).c_str(), &start_time) && | |
378 base::Time::FromString( | |
379 ASCIIToWide(promo_end_string).c_str(), &end_time)) { | |
380 promo_start = start_time.ToDoubleT(); | |
381 promo_end = end_time.ToDoubleT(); | |
382 } | |
383 } | |
384 } | |
385 } | |
386 | |
387 // If start or end times have changed, trigger a new web resource | |
388 // notification, so that the logo on the NTP is updated. This check is | |
389 // outside the reading of the web resource data, because the absence of | |
390 // dates counts as a triggering change if there were dates before. | |
391 if (!(old_promo_start == promo_start) || | |
392 !(old_promo_end == promo_end)) { | |
393 prefs_->SetReal(prefs::kNTPPromoStart, promo_start); | |
394 prefs_->SetReal(prefs::kNTPPromoEnd, promo_end); | |
395 NotificationService* service = NotificationService::current(); | |
396 service->Notify(NotificationType::WEB_RESOURCE_STATE_CHANGED, | |
397 Source<WebResourceService>(this), | |
398 NotificationService::NoDetails()); | |
399 } | |
400 } | |
401 | |
303 void WebResourceService::UnpackLogoSignal(const DictionaryValue& parsed_json) { | 402 void WebResourceService::UnpackLogoSignal(const DictionaryValue& parsed_json) { |
304 DictionaryValue* topic_dict; | 403 DictionaryValue* topic_dict; |
305 ListValue* answer_list; | 404 ListValue* answer_list; |
306 double old_logo_start = 0; | 405 double old_logo_start = 0; |
307 double old_logo_end = 0; | 406 double old_logo_end = 0; |
308 double logo_start = 0; | 407 double logo_start = 0; |
309 double logo_end = 0; | 408 double logo_end = 0; |
310 | 409 |
311 // Check for preexisting start and end values. | 410 // Check for preexisting start and end values. |
312 if (prefs_->HasPrefPath(prefs::kNTPCustomLogoStart) && | 411 if (prefs_->HasPrefPath(prefs::kNTPCustomLogoStart) && |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 | 453 |
355 // If logo start or end times have changed, trigger a new web resource | 454 // If logo start or end times have changed, trigger a new web resource |
356 // notification, so that the logo on the NTP is updated. This check is | 455 // notification, so that the logo on the NTP is updated. This check is |
357 // outside the reading of the web resource data, because the absence of | 456 // outside the reading of the web resource data, because the absence of |
358 // dates counts as a triggering change if there were dates before. | 457 // dates counts as a triggering change if there were dates before. |
359 if (!(old_logo_start == logo_start) || | 458 if (!(old_logo_start == logo_start) || |
360 !(old_logo_end == logo_end)) { | 459 !(old_logo_end == logo_end)) { |
361 prefs_->SetReal(prefs::kNTPCustomLogoStart, logo_start); | 460 prefs_->SetReal(prefs::kNTPCustomLogoStart, logo_start); |
362 prefs_->SetReal(prefs::kNTPCustomLogoEnd, logo_end); | 461 prefs_->SetReal(prefs::kNTPCustomLogoEnd, logo_end); |
363 NotificationService* service = NotificationService::current(); | 462 NotificationService* service = NotificationService::current(); |
364 service->Notify(NotificationType::WEB_RESOURCE_AVAILABLE, | 463 service->Notify(NotificationType::WEB_RESOURCE_STATE_CHANGED, |
365 Source<WebResourceService>(this), | 464 Source<WebResourceService>(this), |
366 NotificationService::NoDetails()); | 465 NotificationService::NoDetails()); |
367 } | 466 } |
368 } | 467 } |
468 | |
469 | |
470 namespace WebResourceServiceUtil { | |
471 | |
472 bool CanShowPromo(Profile* profile) { | |
473 bool promo_closed = false; | |
474 PrefService* prefs = profile->GetPrefs(); | |
475 if (prefs->HasPrefPath(prefs::kNTPPromoClosed)) | |
476 promo_closed = prefs->GetBoolean(prefs::kNTPPromoClosed); | |
477 std::string channel = platform_util::GetVersionStringModifier(); | |
478 ExtensionsService* extensions_service = profile->GetExtensionsService(); | |
479 bool promo_options_set = | |
480 sync_ui_util::GetStatus( | |
481 profile->GetProfileSyncService()) == sync_ui_util::SYNCED || | |
482 (extensions_service && extensions_service->HasInstalledExtensions()); | |
483 | |
484 return channel == "dev" && | |
arv (Not doing code reviews)
2010/11/24 00:02:43
Should we only show this on dev?
Miranda Callahan
2010/11/24 00:39:41
As discussed in chat -- yes.
| |
485 !promo_closed && | |
486 promo_options_set && | |
487 g_browser_process->GetApplicationLocale() == "en-US"; | |
488 } | |
489 | |
490 } // namespace WebResourceService | |
OLD | NEW |