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 // Much shorter update time if looking for a promo to display. |
| 222 cache_update_delay_ = |
| 223 WebResourceServiceUtil::CanShowPromo(profile_) ? kShortCacheUpdateDelay : |
| 224 kLongCacheUpdateDelay; |
203 resource_dispatcher_host_ = g_browser_process->resource_dispatcher_host(); | 225 resource_dispatcher_host_ = g_browser_process->resource_dispatcher_host(); |
204 web_resource_fetcher_.reset(new WebResourceFetcher(this)); | 226 web_resource_fetcher_.reset(new WebResourceFetcher(this)); |
205 prefs_->RegisterStringPref(prefs::kNTPWebResourceCacheUpdate, "0"); | 227 prefs_->RegisterStringPref(prefs::kNTPWebResourceCacheUpdate, "0"); |
206 prefs_->RegisterRealPref(prefs::kNTPCustomLogoStart, 0); | 228 prefs_->RegisterRealPref(prefs::kNTPCustomLogoStart, 0); |
207 prefs_->RegisterRealPref(prefs::kNTPCustomLogoEnd, 0); | 229 prefs_->RegisterRealPref(prefs::kNTPCustomLogoEnd, 0); |
| 230 prefs_->RegisterRealPref(prefs::kNTPPromoStart, 0); |
| 231 prefs_->RegisterRealPref(prefs::kNTPPromoEnd, 0); |
| 232 prefs_->RegisterStringPref(prefs::kNTPPromoLine, std::string()); |
| 233 prefs_->RegisterBooleanPref(prefs::kNTPPromoClosed, false); |
208 | 234 |
209 if (prefs_->HasPrefPath(prefs::kNTPLogoResourceServer)) { | 235 if (prefs_->HasPrefPath(prefs::kNTPWebResourceCache)) { |
210 web_resource_server_ = prefs_->GetString(prefs::kNTPLogoResourceServer); | 236 web_resource_server_ = prefs_->GetString(prefs::kNTPWebResourceCache); |
211 return; | 237 return; |
212 } | 238 } |
213 | 239 |
214 // If we have not yet set a server, reset and force an immediate update. | 240 // If we have not yet set a server, reset and force an immediate update. |
215 web_resource_server_ = kDefaultResourceServer; | 241 web_resource_server_ = kDefaultWebResourceServer; |
216 prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, ""); | 242 prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, ""); |
217 } | 243 } |
218 | 244 |
219 void WebResourceService::EndFetch() { | 245 void WebResourceService::EndFetch() { |
220 in_fetch_ = false; | 246 in_fetch_ = false; |
221 } | 247 } |
222 | 248 |
223 void WebResourceService::OnWebResourceUnpacked( | 249 void WebResourceService::OnWebResourceUnpacked( |
224 const DictionaryValue& parsed_json) { | 250 const DictionaryValue& parsed_json) { |
225 UnpackLogoSignal(parsed_json); | 251 UnpackLogoSignal(parsed_json); |
| 252 if (WebResourceServiceUtil::CanShowPromo(profile_)) |
| 253 UnpackPromoSignal(parsed_json); |
226 EndFetch(); | 254 EndFetch(); |
227 } | 255 } |
228 | 256 |
229 void WebResourceService::StartAfterDelay() { | 257 void WebResourceService::StartAfterDelay() { |
230 int delay = kStartResourceFetchDelay; | 258 int delay = kStartResourceFetchDelay; |
231 // Check whether we have ever put a value in the web resource cache; | 259 // 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. | 260 // if so, pull it out and see if it's time to update again. |
233 if (prefs_->HasPrefPath(prefs::kNTPWebResourceCacheUpdate)) { | 261 if (prefs_->HasPrefPath(prefs::kNTPWebResourceCacheUpdate)) { |
234 std::string last_update_pref = | 262 std::string last_update_pref = |
235 prefs_->GetString(prefs::kNTPWebResourceCacheUpdate); | 263 prefs_->GetString(prefs::kNTPWebResourceCacheUpdate); |
236 if (!last_update_pref.empty()) { | 264 if (!last_update_pref.empty()) { |
237 double last_update_value; | 265 double last_update_value; |
238 base::StringToDouble(last_update_pref, &last_update_value); | 266 base::StringToDouble(last_update_pref, &last_update_value); |
239 int ms_until_update = kCacheUpdateDelay - | 267 int ms_until_update = cache_update_delay_ - |
240 static_cast<int>((base::Time::Now() - base::Time::FromDoubleT( | 268 static_cast<int>((base::Time::Now() - base::Time::FromDoubleT( |
241 last_update_value)).InMilliseconds()); | 269 last_update_value)).InMilliseconds()); |
242 | 270 |
243 delay = ms_until_update > kCacheUpdateDelay ? | 271 delay = ms_until_update > cache_update_delay_ ? |
244 kCacheUpdateDelay : (ms_until_update < kStartResourceFetchDelay ? | 272 cache_update_delay_ : (ms_until_update < kStartResourceFetchDelay ? |
245 kStartResourceFetchDelay : ms_until_update); | 273 kStartResourceFetchDelay : ms_until_update); |
246 } | 274 } |
247 } | 275 } |
248 | 276 |
249 // Start fetch and wait for UpdateResourceCache. | 277 // Start fetch and wait for UpdateResourceCache. |
250 web_resource_fetcher_->StartAfterDelay(static_cast<int>(delay)); | 278 web_resource_fetcher_->StartAfterDelay(static_cast<int>(delay)); |
251 } | 279 } |
252 | 280 |
253 void WebResourceService::UpdateResourceCache(const std::string& json_data) { | 281 void WebResourceService::UpdateResourceCache(const std::string& json_data) { |
254 UnpackerClient* client = new UnpackerClient(this, json_data); | 282 UnpackerClient* client = new UnpackerClient(this, json_data); |
255 client->Start(); | 283 client->Start(); |
256 | 284 |
257 // Update resource server and cache update time in preferences. | 285 // Update resource server and cache update time in preferences. |
258 prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, | 286 prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, |
259 base::DoubleToString(base::Time::Now().ToDoubleT())); | 287 base::DoubleToString(base::Time::Now().ToDoubleT())); |
260 prefs_->SetString(prefs::kNTPLogoResourceServer, web_resource_server_); | 288 prefs_->SetString(prefs::kNTPWebResourceServer, web_resource_server_); |
261 } | 289 } |
262 | 290 |
263 void WebResourceService::UnpackTips(const DictionaryValue& parsed_json) { | 291 void WebResourceService::UnpackTips(const DictionaryValue& parsed_json) { |
264 // Get dictionary of cached preferences. | 292 // Get dictionary of cached preferences. |
265 web_resource_cache_ = | 293 web_resource_cache_ = |
266 prefs_->GetMutableDictionary(prefs::kNTPWebResourceCache); | 294 prefs_->GetMutableDictionary(prefs::kNTPWebResourceCache); |
267 | 295 |
268 // The list of individual tips. | 296 // The list of individual tips. |
269 ListValue* tip_holder = new ListValue(); | 297 ListValue* tip_holder = new ListValue(); |
270 web_resource_cache_->Set(WebResourceService::kTipCachePrefName, tip_holder); | 298 web_resource_cache_->Set(WebResourceService::kTipCachePrefName, tip_holder); |
(...skipping 22 matching lines...) Expand all Loading... |
293 } | 321 } |
294 // If tips exist, set current index to 0. | 322 // If tips exist, set current index to 0. |
295 if (!inproduct.empty()) { | 323 if (!inproduct.empty()) { |
296 web_resource_cache_->SetInteger( | 324 web_resource_cache_->SetInteger( |
297 WebResourceService::kCurrentTipPrefName, 0); | 325 WebResourceService::kCurrentTipPrefName, 0); |
298 } | 326 } |
299 } | 327 } |
300 } | 328 } |
301 } | 329 } |
302 | 330 |
| 331 void WebResourceService::UnpackPromoSignal(const DictionaryValue& parsed_json) { |
| 332 DictionaryValue* topic_dict; |
| 333 ListValue* answer_list; |
| 334 double old_promo_start = 0; |
| 335 double old_promo_end = 0; |
| 336 double promo_start = 0; |
| 337 double promo_end = 0; |
| 338 |
| 339 // Check for preexisting start and end values. |
| 340 if (prefs_->HasPrefPath(prefs::kNTPPromoStart) && |
| 341 prefs_->HasPrefPath(prefs::kNTPPromoEnd)) { |
| 342 old_promo_start = prefs_->GetReal(prefs::kNTPPromoStart); |
| 343 old_promo_end = prefs_->GetReal(prefs::kNTPPromoEnd); |
| 344 } |
| 345 |
| 346 // Check for newly received start and end values. |
| 347 if (parsed_json.GetDictionary("topic", &topic_dict)) { |
| 348 if (topic_dict->GetList("answers", &answer_list)) { |
| 349 std::string promo_start_string = ""; |
| 350 std::string promo_end_string = ""; |
| 351 std::string promo_string = ""; |
| 352 for (ListValue::const_iterator tip_iter = answer_list->begin(); |
| 353 tip_iter != answer_list->end(); ++tip_iter) { |
| 354 if (!(*tip_iter)->IsType(Value::TYPE_DICTIONARY)) |
| 355 continue; |
| 356 DictionaryValue* a_dic = |
| 357 static_cast<DictionaryValue*>(*tip_iter); |
| 358 std::string promo_signal; |
| 359 if (a_dic->GetString("name", &promo_signal)) { |
| 360 if (promo_signal == "promo_start") { |
| 361 a_dic->GetString("inproduct", &promo_start_string); |
| 362 a_dic->GetString("tooltip", &promo_string); |
| 363 prefs_->SetString(prefs::kNTPPromoLine, promo_string); |
| 364 } else if (promo_signal == "promo_end") { |
| 365 a_dic->GetString("inproduct", &promo_end_string); |
| 366 } |
| 367 } |
| 368 } |
| 369 if (!promo_start_string.empty() && |
| 370 promo_start_string.length() > 0 && |
| 371 !promo_end_string.empty() && |
| 372 promo_end_string.length() > 0) { |
| 373 base::Time start_time; |
| 374 base::Time end_time; |
| 375 if (base::Time::FromString( |
| 376 ASCIIToWide(promo_start_string).c_str(), &start_time) && |
| 377 base::Time::FromString( |
| 378 ASCIIToWide(promo_end_string).c_str(), &end_time)) { |
| 379 promo_start = start_time.ToDoubleT(); |
| 380 promo_end = end_time.ToDoubleT(); |
| 381 } |
| 382 } |
| 383 } |
| 384 } |
| 385 |
| 386 // If start or end times have changed, trigger a new web resource |
| 387 // notification, so that the logo on the NTP is updated. This check is |
| 388 // outside the reading of the web resource data, because the absence of |
| 389 // dates counts as a triggering change if there were dates before. |
| 390 if (!(old_promo_start == promo_start) || |
| 391 !(old_promo_end == promo_end)) { |
| 392 prefs_->SetReal(prefs::kNTPPromoStart, promo_start); |
| 393 prefs_->SetReal(prefs::kNTPPromoEnd, promo_end); |
| 394 NotificationService* service = NotificationService::current(); |
| 395 service->Notify(NotificationType::WEB_RESOURCE_STATE_CHANGED, |
| 396 Source<WebResourceService>(this), |
| 397 NotificationService::NoDetails()); |
| 398 } |
| 399 } |
| 400 |
303 void WebResourceService::UnpackLogoSignal(const DictionaryValue& parsed_json) { | 401 void WebResourceService::UnpackLogoSignal(const DictionaryValue& parsed_json) { |
304 DictionaryValue* topic_dict; | 402 DictionaryValue* topic_dict; |
305 ListValue* answer_list; | 403 ListValue* answer_list; |
306 double old_logo_start = 0; | 404 double old_logo_start = 0; |
307 double old_logo_end = 0; | 405 double old_logo_end = 0; |
308 double logo_start = 0; | 406 double logo_start = 0; |
309 double logo_end = 0; | 407 double logo_end = 0; |
310 | 408 |
311 // Check for preexisting start and end values. | 409 // Check for preexisting start and end values. |
312 if (prefs_->HasPrefPath(prefs::kNTPCustomLogoStart) && | 410 if (prefs_->HasPrefPath(prefs::kNTPCustomLogoStart) && |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
354 | 452 |
355 // If logo start or end times have changed, trigger a new web resource | 453 // 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 | 454 // 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 | 455 // outside the reading of the web resource data, because the absence of |
358 // dates counts as a triggering change if there were dates before. | 456 // dates counts as a triggering change if there were dates before. |
359 if (!(old_logo_start == logo_start) || | 457 if (!(old_logo_start == logo_start) || |
360 !(old_logo_end == logo_end)) { | 458 !(old_logo_end == logo_end)) { |
361 prefs_->SetReal(prefs::kNTPCustomLogoStart, logo_start); | 459 prefs_->SetReal(prefs::kNTPCustomLogoStart, logo_start); |
362 prefs_->SetReal(prefs::kNTPCustomLogoEnd, logo_end); | 460 prefs_->SetReal(prefs::kNTPCustomLogoEnd, logo_end); |
363 NotificationService* service = NotificationService::current(); | 461 NotificationService* service = NotificationService::current(); |
364 service->Notify(NotificationType::WEB_RESOURCE_AVAILABLE, | 462 service->Notify(NotificationType::WEB_RESOURCE_STATE_CHANGED, |
365 Source<WebResourceService>(this), | 463 Source<WebResourceService>(this), |
366 NotificationService::NoDetails()); | 464 NotificationService::NoDetails()); |
367 } | 465 } |
368 } | 466 } |
| 467 |
| 468 |
| 469 namespace WebResourceServiceUtil { |
| 470 |
| 471 bool CanShowPromo(Profile* profile) { |
| 472 bool promo_closed = false; |
| 473 PrefService* prefs = profile->GetPrefs(); |
| 474 if (prefs->HasPrefPath(prefs::kNTPPromoClosed)) |
| 475 promo_closed = prefs->GetBoolean(prefs::kNTPPromoClosed); |
| 476 std::string channel = platform_util::GetVersionStringModifier(); |
| 477 ExtensionsService* extensions_service = profile->GetExtensionsService(); |
| 478 bool promo_options_set = |
| 479 sync_ui_util::GetStatus( |
| 480 profile->GetProfileSyncService()) == sync_ui_util::SYNCED || |
| 481 (extensions_service && extensions_service->HasInstalledExtensions()); |
| 482 |
| 483 return channel == "dev" && |
| 484 !promo_closed && |
| 485 promo_options_set && |
| 486 g_browser_process->GetApplicationLocale() == "en-US"; |
| 487 } |
| 488 |
| 489 } // namespace WebResourceService |
| 490 |
OLD | NEW |