summaryrefslogtreecommitdiffstats
path: root/chrome/browser/web_resource
diff options
context:
space:
mode:
authormirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-21 01:00:34 +0000
committermirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-09-21 01:00:34 +0000
commit0ef69c756f644654452dd9ce94ab95f14b6c998c (patch)
treecca6d5a31f07791216185e0a49cd5d271891a2c4 /chrome/browser/web_resource
parent2e3e1908a954a3e23ddc08ff7f2023b0d3b92a21 (diff)
downloadchromium_src-0ef69c756f644654452dd9ce94ab95f14b6c998c.zip
chromium_src-0ef69c756f644654452dd9ce94ab95f14b6c998c.tar.gz
chromium_src-0ef69c756f644654452dd9ce94ab95f14b6c998c.tar.bz2
Add the ability to change NTP logos using a command from the web resource server.
BUG=none TEST=none Review URL: http://codereview.chromium.org/3418020 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@60001 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/web_resource')
-rw-r--r--chrome/browser/web_resource/web_resource_service.cc126
-rw-r--r--chrome/browser/web_resource/web_resource_service.h48
2 files changed, 120 insertions, 54 deletions
diff --git a/chrome/browser/web_resource/web_resource_service.cc b/chrome/browser/web_resource/web_resource_service.cc
index 16e0c33..ce34d1b 100644
--- a/chrome/browser/web_resource/web_resource_service.cc
+++ b/chrome/browser/web_resource/web_resource_service.cc
@@ -16,6 +16,8 @@
#include "chrome/browser/profile.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/net/url_fetcher.h"
+#include "chrome/common/notification_service.h"
+#include "chrome/common/notification_type.h"
#include "chrome/common/pref_names.h"
#include "googleurl/src/gurl.h"
#include "net/base/load_flags.h"
@@ -23,6 +25,7 @@
const char* WebResourceService::kCurrentTipPrefName = "current_tip";
const char* WebResourceService::kTipCachePrefName = "tips";
+const char* WebResourceService::kCustomLogoId = "1";
class WebResourceService::WebResourceFetcher
: public URLFetcher::Delegate {
@@ -192,12 +195,8 @@ const char* WebResourceService::kDefaultResourceServer =
"https://clients2.google.com/tools/service/npredir?r=chrometips_win&hl=";
#endif
-const char* WebResourceService::kResourceDirectoryName =
- "Resources";
-
WebResourceService::WebResourceService(Profile* profile)
: prefs_(profile->GetPrefs()),
- web_resource_dir_(profile->GetPath().AppendASCII(kResourceDirectoryName)),
in_fetch_(false) {
Init();
}
@@ -207,21 +206,22 @@ WebResourceService::~WebResourceService() { }
void WebResourceService::Init() {
resource_dispatcher_host_ = g_browser_process->resource_dispatcher_host();
web_resource_fetcher_ = new WebResourceFetcher(this);
- prefs_->RegisterStringPref(prefs::kNTPTipsCacheUpdate, "0");
+ prefs_->RegisterStringPref(prefs::kNTPWebResourceCacheUpdate, "0");
+ prefs_->RegisterBooleanPref(prefs::kNTPCustomLogo, false);
std::string locale = g_browser_process->GetApplicationLocale();
- if (prefs_->HasPrefPath(prefs::kNTPTipsServer)) {
- web_resource_server_ = prefs_->GetString(prefs::kNTPTipsServer);
+ if (prefs_->HasPrefPath(prefs::kNTPWebResourceServer)) {
+ web_resource_server_ = prefs_->GetString(prefs::kNTPWebResourceServer);
// If we are in the correct locale, initialization is done.
if (EndsWith(web_resource_server_, locale, false))
return;
}
- // If we have not yet set a server, or if the tips server is set to the wrong
- // locale, reset the server and force an immediate update of tips.
+ // If we have not yet set a server, or if the web resource server is set to
+ // the wrong locale, reset the server and force an immediate update.
web_resource_server_ = kDefaultResourceServer;
web_resource_server_.append(locale);
- prefs_->SetString(prefs::kNTPTipsCacheUpdate, "");
+ prefs_->SetString(prefs::kNTPWebResourceCacheUpdate, "");
}
void WebResourceService::EndFetch() {
@@ -230,9 +230,48 @@ void WebResourceService::EndFetch() {
void WebResourceService::OnWebResourceUnpacked(
const DictionaryValue& parsed_json) {
+ UnpackLogoSignal(parsed_json);
+ EndFetch();
+}
+
+void WebResourceService::StartAfterDelay() {
+ int delay = kStartResourceFetchDelay;
+ // Check whether we have ever put a value in the web resource cache;
+ // if so, pull it out and see if it's time to update again.
+ if (prefs_->HasPrefPath(prefs::kNTPWebResourceCacheUpdate)) {
+ std::string last_update_pref =
+ prefs_->GetString(prefs::kNTPWebResourceCacheUpdate);
+ if (!last_update_pref.empty()) {
+ double last_update_value;
+ base::StringToDouble(last_update_pref, &last_update_value);
+ int ms_until_update = kCacheUpdateDelay -
+ static_cast<int>((base::Time::Now() - base::Time::FromDoubleT(
+ last_update_value)).InMilliseconds());
+
+ delay = ms_until_update > kCacheUpdateDelay ?
+ kCacheUpdateDelay : (ms_until_update < kStartResourceFetchDelay ?
+ kStartResourceFetchDelay : ms_until_update);
+ }
+ }
+
+ // Start fetch and wait for UpdateResourceCache.
+ web_resource_fetcher_->StartAfterDelay(static_cast<int>(delay));
+}
+
+void WebResourceService::UpdateResourceCache(const std::string& json_data) {
+ UnpackerClient* client = new UnpackerClient(this, json_data);
+ client->Start();
+
+ // Update resource server and cache update time in preferences.
+ prefs_->SetString(prefs::kNTPWebResourceCacheUpdate,
+ base::DoubleToString(base::Time::Now().ToDoubleT()));
+ prefs_->SetString(prefs::kNTPWebResourceServer, web_resource_server_);
+}
+
+void WebResourceService::UnpackTips(const DictionaryValue& parsed_json) {
// Get dictionary of cached preferences.
web_resource_cache_ =
- prefs_->GetMutableDictionary(prefs::kNTPTipsCache);
+ prefs_->GetMutableDictionary(prefs::kNTPWebResourceCache);
// The list of individual tips.
ListValue* tip_holder = new ListValue();
@@ -241,6 +280,7 @@ void WebResourceService::OnWebResourceUnpacked(
DictionaryValue* topic_dict;
ListValue* answer_list;
std::string topic_id;
+ std::string answer_id;
std::string inproduct;
int tip_counter = 0;
@@ -259,45 +299,45 @@ void WebResourceService::OnWebResourceUnpacked(
}
tip_counter++;
}
- // If we have tips, set current tip to zero.
- if (!inproduct.empty())
+ // If tips exist, set current index to 0.
+ if (!inproduct.empty()) {
web_resource_cache_->SetInteger(
WebResourceService::kCurrentTipPrefName, 0);
+ }
}
}
- EndFetch();
}
-void WebResourceService::StartAfterDelay() {
- int delay = kStartResourceFetchDelay;
- // Check whether we have ever put a value in the web resource cache;
- // if so, pull it out and see if it's time to update again.
- if (prefs_->HasPrefPath(prefs::kNTPTipsCacheUpdate)) {
- std::string last_update_pref =
- prefs_->GetString(prefs::kNTPTipsCacheUpdate);
- if (!last_update_pref.empty()) {
- double last_update_value;
- base::StringToDouble(last_update_pref, &last_update_value);
- int ms_until_update = kCacheUpdateDelay -
- static_cast<int>((base::Time::Now() - base::Time::FromDoubleT(
- last_update_value)).InMilliseconds());
+void WebResourceService::UnpackLogoSignal(const DictionaryValue& parsed_json) {
+ DictionaryValue* topic_dict;
+ ListValue* answer_list;
+ std::string logo_id;
- delay = ms_until_update > kCacheUpdateDelay ?
- kCacheUpdateDelay : (ms_until_update < kStartResourceFetchDelay ?
- kStartResourceFetchDelay : ms_until_update);
+ if (parsed_json.GetDictionary("topic", &topic_dict)) {
+ if (topic_dict->GetList("answers", &answer_list)) {
+ for (ListValue::const_iterator tip_iter = answer_list->begin();
+ tip_iter != answer_list->end(); ++tip_iter) {
+ if (!(*tip_iter)->IsType(Value::TYPE_DICTIONARY))
+ continue;
+ DictionaryValue* a_dic =
+ static_cast<DictionaryValue*>(*tip_iter);
+ if (a_dic->GetString("logo_id", &logo_id)) {
+ prefs_->SetBoolean(prefs::kNTPCustomLogo,
+ LowerCaseEqualsASCII(logo_id, kCustomLogoId));
+ NotificationService* service = NotificationService::current();
+ service->Notify(NotificationType::BROWSER_THEME_CHANGED,
+ Source<WebResourceService>(this),
+ NotificationService::NoDetails());
+ return;
+ }
+ }
}
}
-
- // Start fetch and wait for UpdateResourceCache.
- web_resource_fetcher_->StartAfterDelay(static_cast<int>(delay));
-}
-
-void WebResourceService::UpdateResourceCache(const std::string& json_data) {
- UnpackerClient* client = new UnpackerClient(this, json_data);
- client->Start();
-
- // Update resource server and cache update time in preferences.
- prefs_->SetString(prefs::kNTPTipsCacheUpdate,
- base::DoubleToString(base::Time::Now().ToDoubleT()));
- prefs_->SetString(prefs::kNTPTipsServer, web_resource_server_);
+ // If no logo_id is found in the tips, show regular logo.
+ prefs_->SetBoolean(prefs::kNTPCustomLogo,
+ LowerCaseEqualsASCII(logo_id, kCustomLogoId));
+ NotificationService* service = NotificationService::current();
+ service->Notify(NotificationType::BROWSER_THEME_CHANGED,
+ Source<WebResourceService>(this),
+ NotificationService::NoDetails());
}
diff --git a/chrome/browser/web_resource/web_resource_service.h b/chrome/browser/web_resource/web_resource_service.h
index 28e952b..fd46e76 100644
--- a/chrome/browser/web_resource/web_resource_service.h
+++ b/chrome/browser/web_resource/web_resource_service.h
@@ -30,6 +30,7 @@ class WebResourceService
static const char* kCurrentTipPrefName;
static const char* kTipCachePrefName;
+ static const char* kCustomLogoId;
// Default server from which to gather resources.
static const char* kDefaultResourceServer;
@@ -50,12 +51,44 @@ class WebResourceService
// Puts parsed json data in the right places, and writes to prefs file.
void OnWebResourceUnpacked(const DictionaryValue& parsed_json);
+ // Unpack the web resource as a set of tips. Expects json in the form of:
+ // {
+ // "lang": "en",
+ // "topic": {
+ // "topid_id": "24013",
+ // "topics": [
+ // ],
+ // "answers": [
+ // {
+ // "answer_id": "18625",
+ // "inproduct": "Text here will be shown as a tip",
+ // },
+ // ...
+ // ]
+ // }
+ // }
+ //
+ void UnpackTips(const DictionaryValue& parsed_json);
+
+ // Unpack the web resource as a custom logo signal. Expects json in the form
+ // of:
+ // {
+ // "topic": {
+ // "answers": [
+ // {
+ // "logo_id": "1"
+ // },
+ // ...
+ // ]
+ // }
+ // }
+ //
+ void UnpackLogoSignal(const DictionaryValue& parsed_json);
+
// We need to be able to load parsed resource data into preferences file,
// and get proper install directory.
PrefService* prefs_;
- FilePath web_resource_dir_;
-
// Server from which we are currently pulling web resource data.
std::string web_resource_server_;
@@ -72,18 +105,11 @@ class WebResourceService
// kCacheUpdateDelay time, and silently exit.
bool in_fetch_;
- // Maximum number of cached resources available.
- static const int kMaxResourceCacheSize = 6;
-
// Delay on first fetch so we don't interfere with startup.
static const int kStartResourceFetchDelay = 5000;
- // Delay between calls to update the cache (48 hours).
- static const int kCacheUpdateDelay = 48 * 60 * 60 * 1000;
-
- // Name of directory inside the profile where we will store resource-related
- // data (for now, thumbnail images).
- static const char* kResourceDirectoryName;
+ // Delay between calls to update the cache (12 hours).
+ static const int kCacheUpdateDelay = 12 * 60 * 60 * 1000;
DISALLOW_COPY_AND_ASSIGN(WebResourceService);
};