Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: chrome/browser/web_resource/web_resource_service.cc

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

Powered by Google App Engine
This is Rietveld 408576698