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

Unified Diff: chrome/browser/search/local_ntp_source.cc

Issue 19054012: Reload Local NTP on default search provider change. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed comments Created 7 years, 5 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/browser/search/local_ntp_source.h ('k') | chrome/browser/search/search.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/search/local_ntp_source.cc
diff --git a/chrome/browser/search/local_ntp_source.cc b/chrome/browser/search/local_ntp_source.cc
index 84f63817ae5e5c1ee3c4b6d38976e7a9a926ecab..e95bd577eb8ef0a7c8a60f73867476ebce7087e7 100644
--- a/chrome/browser/search/local_ntp_source.cc
+++ b/chrome/browser/search/local_ntp_source.cc
@@ -4,13 +4,18 @@
#include "chrome/browser/search/local_ntp_source.h"
+#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/memory/ref_counted_memory.h"
+#include "base/memory/scoped_ptr.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/search/instant_io_context.h"
#include "chrome/browser/search/search.h"
+#include "chrome/browser/search_engines/template_url_prepopulate_data.h"
+#include "chrome/browser/search_engines/template_url_service.h"
+#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/common/url_constants.h"
#include "grit/browser_resources.h"
#include "grit/generated_resources.h"
@@ -26,7 +31,7 @@ namespace {
// Signifies a locally constructed resource, i.e. not from grit/.
const int kLocalResource = -1;
-const char kTranslatedStringsFilename[] = "translated-strings.js";
+const char kConfigDataFilename[] = "config.js";
const struct Resource{
const char* filename;
@@ -35,7 +40,7 @@ const struct Resource{
} kResources[] = {
{ "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" },
{ "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" },
- { kTranslatedStringsFilename, kLocalResource, "application/javascript" },
+ { kConfigDataFilename, kLocalResource, "application/javascript" },
{ "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" },
{ "images/close_2.png", IDR_CLOSE_2, "image/png" },
{ "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" },
@@ -60,6 +65,22 @@ std::string StripParameters(const std::string& path) {
return path.substr(0, path.find("?"));
}
+bool DefaultSearchProviderIsGoogle(Profile* profile) {
+ if (!profile)
+ return false;
+
+ TemplateURLService* template_url_service =
+ TemplateURLServiceFactory::GetForProfile(profile);
+ if (!template_url_service)
+ return false;
+
+ const TemplateURL* default_provider =
+ template_url_service->GetDefaultSearchProvider();
+ return default_provider &&
+ (TemplateURLPrepopulateData::GetEngineType(default_provider->url()) ==
+ SEARCH_ENGINE_GOOGLE);
+}
+
// Adds a localized string keyed by resource id to the dictionary.
void AddString(base::DictionaryValue* dictionary,
const std::string& key,
@@ -67,30 +88,51 @@ void AddString(base::DictionaryValue* dictionary,
dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id));
}
-// Returns a JS dictionary of translated strings for the local NTP.
-std::string GetTranslatedStrings() {
- base::DictionaryValue translated_strings;
+// Populates |translated_strings| dictionary for the local NTP.
+scoped_ptr<DictionaryValue> GetTranslatedStrings() {
+ scoped_ptr<base::DictionaryValue> translated_strings(
+ new base::DictionaryValue());
+
if (chrome::ShouldShowRecentTabsOnNTP())
- AddString(&translated_strings, "recentTabs", IDS_RECENT_TABS_MENU);
- AddString(&translated_strings, "thumbnailRemovedNotification",
+ AddString(translated_strings.get(), "recentTabs", IDS_RECENT_TABS_MENU);
+
+ AddString(translated_strings.get(), "thumbnailRemovedNotification",
IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION);
- AddString(&translated_strings, "removeThumbnailTooltip",
+ AddString(translated_strings.get(), "removeThumbnailTooltip",
IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP);
- AddString(&translated_strings, "undoThumbnailRemove",
+ AddString(translated_strings.get(), "undoThumbnailRemove",
IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE);
- AddString(&translated_strings, "restoreThumbnailsShort",
+ AddString(translated_strings.get(), "restoreThumbnailsShort",
IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK);
- AddString(&translated_strings, "attributionIntro",
+ AddString(translated_strings.get(), "attributionIntro",
IDS_NEW_TAB_ATTRIBUTION_INTRO);
- AddString(&translated_strings, "title", IDS_NEW_TAB_TITLE);
- std::string translated_strings_js;
- webui::AppendJsonJS(&translated_strings, &translated_strings_js);
- return translated_strings_js;
+ AddString(translated_strings.get(), "title", IDS_NEW_TAB_TITLE);
+
+ return translated_strings.Pass();
+}
+
+// Returns a JS dictionary of configuration data for the local NTP.
+std::string GetConfigData(Profile* profile) {
+ base::DictionaryValue config_data;
+ config_data.Set("translatedStrings", GetTranslatedStrings().release());
+ config_data.SetBoolean("isGooglePage",
+ DefaultSearchProviderIsGoogle(profile));
+
+ // Serialize the dictionary.
+ std::string js_text;
+ JSONStringValueSerializer serializer(&js_text);
+ serializer.Serialize(config_data);
+
+ std::string config_data_js;
+ config_data_js.append("var configData = ");
+ config_data_js.append(js_text);
+ config_data_js.append(";");
+ return config_data_js;
}
} // namespace
-LocalNtpSource::LocalNtpSource() {
+LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) {
}
LocalNtpSource::~LocalNtpSource() {
@@ -106,9 +148,9 @@ void LocalNtpSource::StartDataRequest(
int render_view_id,
const content::URLDataSource::GotDataCallback& callback) {
const std::string stripped_path = StripParameters(path);
- if (stripped_path == kTranslatedStringsFilename) {
- std::string translated_strings_js = GetTranslatedStrings();
- callback.Run(base::RefCountedString::TakeString(&translated_strings_js));
+ if (stripped_path == kConfigDataFilename) {
+ std::string config_data_js = GetConfigData(profile_);
+ callback.Run(base::RefCountedString::TakeString(&config_data_js));
return;
}
for (size_t i = 0; i < arraysize(kResources); ++i) {
« no previous file with comments | « chrome/browser/search/local_ntp_source.h ('k') | chrome/browser/search/search.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698