Commit 4f2b1c4c authored by mirandac@chromium.org's avatar mirandac@chromium.org

Ensure that tips change when the Chrome language changes.

BUG= http://crbug.com/21394
TEST= Change chrome language.  Close and reopen browser.  Tips should appear in new language, or, if not available, they should not appear.

Review URL: http://codereview.chromium.org/199073

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@25834 0039d316-1c4b-4281-b951-d872f2087c98
parent be623b0f
......@@ -4,6 +4,7 @@
#include "base/string_util.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/dom_ui/tips_handler.h"
#include "chrome/browser/profile.h"
#include "chrome/browser/web_resource/web_resource_service.h"
......@@ -35,6 +36,20 @@ void TipsHandler::HandleGetTips(const Value* content) {
int current_tip_index;
std::string current_tip;
// If tips are not correct for our locale, do not send. Wait for update.
// We need to check here because the new tab page calls for tips before
// the tip service starts up.
PrefService* current_prefs = dom_ui_->GetProfile()->GetPrefs();
if (current_prefs->HasPrefPath(prefs::kNTPTipsServer)) {
std::wstring server = current_prefs->GetString(prefs::kNTPTipsServer);
std::wstring locale = ASCIIToWide(
g_browser_process->GetApplicationLocale());
if (!EndsWith(server, locale, false)) {
dom_ui_->CallJavascriptFunction(L"tips", list_value);
return;
}
}
if (tips_cache_ != NULL && tips_cache_->GetSize() >= 0) {
if (tips_cache_->GetInteger(
WebResourceService::kCurrentTipPrefName, &current_tip_index) &&
......
......@@ -196,15 +196,20 @@ void WebResourceService::Init() {
resource_dispatcher_host_ = g_browser_process->resource_dispatcher_host();
web_resource_fetcher_ = new WebResourceFetcher(this);
prefs_->RegisterStringPref(prefs::kNTPTipsCacheUpdate, L"0");
std::wstring locale = ASCIIToWide(g_browser_process->GetApplicationLocale());
// TODO(mirandac): allow for language change without wiping out prefs file.
if (prefs_->HasPrefPath(prefs::kNTPTipsServer)) {
web_resource_server_ = prefs_->GetString(prefs::kNTPTipsServer);
} else {
web_resource_server_ = kDefaultResourceServer;
web_resource_server_.append(
ASCIIToWide(g_browser_process->GetApplicationLocale()));
web_resource_server_ = prefs_->GetString(prefs::kNTPTipsServer);
// 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.
web_resource_server_ = kDefaultResourceServer;
web_resource_server_.append(locale);
prefs_->SetString(prefs::kNTPTipsCacheUpdate, L"");
}
void WebResourceService::EndFetch() {
......@@ -258,13 +263,15 @@ void WebResourceService::StartAfterDelay() {
if (prefs_->HasPrefPath(prefs::kNTPTipsCacheUpdate)) {
std::wstring last_update_pref =
prefs_->GetString(prefs::kNTPTipsCacheUpdate);
int ms_until_update = kCacheUpdateDelay -
static_cast<int>((base::Time::Now() - base::Time::FromDoubleT(
StringToDouble(WideToASCII(last_update_pref)))).InMilliseconds());
delay = ms_until_update > kCacheUpdateDelay ?
kCacheUpdateDelay : (ms_until_update < kStartResourceFetchDelay ?
kStartResourceFetchDelay : ms_until_update);
if (!last_update_pref.empty()) {
int ms_until_update = kCacheUpdateDelay -
static_cast<int>((base::Time::Now() - base::Time::FromDoubleT(
StringToDouble(WideToASCII(last_update_pref)))).InMilliseconds());
delay = ms_until_update > kCacheUpdateDelay ?
kCacheUpdateDelay : (ms_until_update < kStartResourceFetchDelay ?
kStartResourceFetchDelay : ms_until_update);
}
}
// Start fetch and wait for UpdateResourceCache.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment