1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/search/local_ntp_source.h"
7 #include "base/json/json_string_value_serializer.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/values.h"
14 #include "chrome/browser/search/instant_io_context.h"
15 #include "chrome/browser/search/search.h"
16 #include "chrome/browser/search_engines/template_url_prepopulate_data.h"
17 #include "chrome/browser/search_engines/template_url_service.h"
18 #include "chrome/browser/search_engines/template_url_service_factory.h"
19 #include "chrome/common/url_constants.h"
20 #include "grit/browser_resources.h"
21 #include "grit/generated_resources.h"
22 #include "grit/ui_resources.h"
23 #include "net/url_request/url_request.h"
24 #include "ui/base/l10n/l10n_util.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/base/webui/jstemplate_builder.h"
31 // Signifies a locally constructed resource, i.e. not from grit/.
32 const int kLocalResource = -1;
34 const char kConfigDataFilename[] = "config.js";
36 const struct Resource{
39 const char* mime_type;
41 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" },
42 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" },
43 { kConfigDataFilename, kLocalResource, "application/javascript" },
44 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" },
45 { "images/close_2.png", IDR_CLOSE_2, "image/png" },
46 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" },
47 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" },
48 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" },
49 { "images/2x/google_logo.png",
50 IDR_LOCAL_NTP_IMAGES_2X_LOGO_PNG, "image/png" },
51 { "images/2x/white_google_logo.png",
52 IDR_LOCAL_NTP_IMAGES_2X_WHITE_LOGO_PNG, "image/png" },
55 // Strips any query parameters from the specified path.
56 std::string StripParameters(const std::string& path) {
57 return path.substr(0, path.find("?"));
60 bool DefaultSearchProviderIsGoogle(Profile* profile) {
64 TemplateURLService* template_url_service =
65 TemplateURLServiceFactory::GetForProfile(profile);
66 if (!template_url_service)
69 const TemplateURL* default_provider =
70 template_url_service->GetDefaultSearchProvider();
71 return default_provider &&
72 (TemplateURLPrepopulateData::GetEngineType(*default_provider) ==
73 SEARCH_ENGINE_GOOGLE);
76 // Adds a localized string keyed by resource id to the dictionary.
77 void AddString(base::DictionaryValue* dictionary,
78 const std::string& key,
80 dictionary->SetString(key, l10n_util::GetStringUTF16(resource_id));
83 // Populates |translated_strings| dictionary for the local NTP.
84 scoped_ptr<DictionaryValue> GetTranslatedStrings() {
85 scoped_ptr<base::DictionaryValue> translated_strings(
86 new base::DictionaryValue());
88 if (chrome::ShouldShowRecentTabsOnNTP())
89 AddString(translated_strings.get(), "recentTabs", IDS_RECENT_TABS_MENU);
91 AddString(translated_strings.get(), "thumbnailRemovedNotification",
92 IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION);
93 AddString(translated_strings.get(), "removeThumbnailTooltip",
94 IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP);
95 AddString(translated_strings.get(), "undoThumbnailRemove",
96 IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE);
97 AddString(translated_strings.get(), "restoreThumbnailsShort",
98 IDS_NEW_TAB_RESTORE_THUMBNAILS_SHORT_LINK);
99 AddString(translated_strings.get(), "attributionIntro",
100 IDS_NEW_TAB_ATTRIBUTION_INTRO);
101 AddString(translated_strings.get(), "title", IDS_NEW_TAB_TITLE);
103 return translated_strings.Pass();
106 // Returns a JS dictionary of configuration data for the local NTP.
107 std::string GetConfigData(Profile* profile) {
108 base::DictionaryValue config_data;
109 config_data.Set("translatedStrings", GetTranslatedStrings().release());
110 config_data.SetBoolean("isGooglePage",
111 DefaultSearchProviderIsGoogle(profile));
113 // Serialize the dictionary.
115 JSONStringValueSerializer serializer(&js_text);
116 serializer.Serialize(config_data);
118 std::string config_data_js;
119 config_data_js.append("var configData = ");
120 config_data_js.append(js_text);
121 config_data_js.append(";");
122 return config_data_js;
127 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) {
130 LocalNtpSource::~LocalNtpSource() {
133 std::string LocalNtpSource::GetSource() const {
134 return chrome::kChromeSearchLocalNtpHost;
137 void LocalNtpSource::StartDataRequest(
138 const std::string& path,
139 int render_process_id,
141 const content::URLDataSource::GotDataCallback& callback) {
142 const std::string stripped_path = StripParameters(path);
143 if (stripped_path == kConfigDataFilename) {
144 std::string config_data_js = GetConfigData(profile_);
145 callback.Run(base::RefCountedString::TakeString(&config_data_js));
148 for (size_t i = 0; i < arraysize(kResources); ++i) {
149 if (stripped_path == kResources[i].filename) {
150 scoped_refptr<base::RefCountedStaticMemory> response(
151 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
152 kResources[i].identifier));
153 callback.Run(response.get());
160 std::string LocalNtpSource::GetMimeType(
161 const std::string& path) const {
162 const std::string stripped_path = StripParameters(path);
163 for (size_t i = 0; i < arraysize(kResources); ++i) {
164 if (stripped_path == kResources[i].filename)
165 return kResources[i].mime_type;
167 return std::string();
170 bool LocalNtpSource::ShouldServiceRequest(
171 const net::URLRequest* request) const {
172 DCHECK(request->url().host() == chrome::kChromeSearchLocalNtpHost);
173 if (!InstantIOContext::ShouldServiceRequest(request))
176 if (request->url().SchemeIs(chrome::kChromeSearchScheme)) {
177 DCHECK(StartsWithASCII(request->url().path(), "/", true));
178 std::string filename = request->url().path().substr(1);
179 for (size_t i = 0; i < arraysize(kResources); ++i) {
180 if (filename == kResources[i].filename)
187 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const {
188 // Allow embedding of most visited iframes.
189 return base::StringPrintf("frame-src %s;",
190 chrome::kChromeSearchMostVisitedUrl);