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

Side by Side Diff: chrome/browser/search/local_ntp_source.cc

Issue 1400423002: Adds a new theme.css endpoint for NTP that sets the background of NTP to the current theme backgrou… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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/search/local_ntp_source.h" 5 #include "chrome/browser/search/local_ntp_source.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/json/json_string_value_serializer.h" 8 #include "base/json/json_string_value_serializer.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ref_counted_memory.h" 10 #include "base/memory/ref_counted_memory.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/field_trial.h" 12 #include "base/metrics/field_trial.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/search/instant_io_context.h" 17 #include "chrome/browser/search/instant_io_context.h"
18 #include "chrome/browser/search/local_files_ntp_source.h" 18 #include "chrome/browser/search/local_files_ntp_source.h"
19 #include "chrome/browser/search/search.h" 19 #include "chrome/browser/search/search.h"
20 #include "chrome/browser/search_engines/template_url_service_factory.h" 20 #include "chrome/browser/search_engines/template_url_service_factory.h"
21 #include "chrome/browser/themes/theme_properties.h"
22 #include "chrome/browser/themes/theme_service.h"
23 #include "chrome/browser/themes/theme_service_factory.h"
21 #include "chrome/common/chrome_switches.h" 24 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/url_constants.h" 25 #include "chrome/common/url_constants.h"
23 #include "chrome/grit/generated_resources.h" 26 #include "chrome/grit/generated_resources.h"
24 #include "components/search_engines/template_url_prepopulate_data.h" 27 #include "components/search_engines/template_url_prepopulate_data.h"
25 #include "components/search_engines/template_url_service.h" 28 #include "components/search_engines/template_url_service.h"
26 #include "grit/browser_resources.h" 29 #include "grit/browser_resources.h"
27 #include "grit/theme_resources.h" 30 #include "grit/theme_resources.h"
28 #include "net/url_request/url_request.h" 31 #include "net/url_request/url_request.h"
32 #include "third_party/skia/include/core/SkColor.h"
29 #include "ui/base/l10n/l10n_util.h" 33 #include "ui/base/l10n/l10n_util.h"
30 #include "ui/base/resource/resource_bundle.h" 34 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/base/ui_base_switches.h" 35 #include "ui/base/ui_base_switches.h"
32 #include "ui/base/webui/jstemplate_builder.h" 36 #include "ui/base/webui/jstemplate_builder.h"
33 #include "ui/base/webui/web_ui_util.h" 37 #include "ui/base/webui/web_ui_util.h"
34 #include "ui/resources/grit/ui_resources.h" 38 #include "ui/resources/grit/ui_resources.h"
35 #include "url/gurl.h" 39 #include "url/gurl.h"
36 40
37 namespace { 41 namespace {
38 42
39 // Signifies a locally constructed resource, i.e. not from grit/. 43 // Signifies a locally constructed resource, i.e. not from grit/.
40 const int kLocalResource = -1; 44 const int kLocalResource = -1;
41 45
42 const char kConfigDataFilename[] = "config.js"; 46 const char kConfigDataFilename[] = "config.js";
47 const char kThemeCSSFilename[] = "theme.css";
43 48
44 const struct Resource{ 49 const struct Resource{
45 const char* filename; 50 const char* filename;
46 int identifier; 51 int identifier;
47 const char* mime_type; 52 const char* mime_type;
48 } kResources[] = { 53 } kResources[] = {
49 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" }, 54 { "local-ntp.html", IDR_LOCAL_NTP_HTML, "text/html" },
50 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" }, 55 { "local-ntp.js", IDR_LOCAL_NTP_JS, "application/javascript" },
51 { kConfigDataFilename, kLocalResource, "application/javascript" }, 56 { kConfigDataFilename, kLocalResource, "application/javascript" },
57 { kThemeCSSFilename, kLocalResource, "text/css" },
52 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" }, 58 { "local-ntp.css", IDR_LOCAL_NTP_CSS, "text/css" },
53 { "images/close_2.png", IDR_CLOSE_2, "image/png" }, 59 { "images/close_2.png", IDR_CLOSE_2, "image/png" },
54 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" }, 60 { "images/close_2_hover.png", IDR_CLOSE_2_H, "image/png" },
55 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" }, 61 { "images/close_2_active.png", IDR_CLOSE_2_P, "image/png" },
56 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" }, 62 { "images/close_2_white.png", IDR_CLOSE_2_MASK, "image/png" },
57 { "images/close_3_mask.png", IDR_CLOSE_3_MASK, "image/png" }, 63 { "images/close_3_mask.png", IDR_CLOSE_3_MASK, "image/png" },
58 { "images/close_4_button.png", IDR_CLOSE_4_BUTTON, "image/png" }, 64 { "images/close_4_button.png", IDR_CLOSE_4_BUTTON, "image/png" },
59 { "images/google_logo.png", IDR_LOCAL_NTP_IMAGES_LOGO_PNG, "image/png" }, 65 { "images/google_logo.png", IDR_LOCAL_NTP_IMAGES_LOGO_PNG, "image/png" },
60 { "images/ntp_default_favicon.png", IDR_NTP_DEFAULT_FAVICON, "image/png" }, 66 { "images/ntp_default_favicon.png", IDR_NTP_DEFAULT_FAVICON, "image/png" },
61 }; 67 };
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 JSONStringValueSerializer serializer(&js_text); 156 JSONStringValueSerializer serializer(&js_text);
151 serializer.Serialize(config_data); 157 serializer.Serialize(config_data);
152 158
153 std::string config_data_js; 159 std::string config_data_js;
154 config_data_js.append("var configData = "); 160 config_data_js.append("var configData = ");
155 config_data_js.append(js_text); 161 config_data_js.append(js_text);
156 config_data_js.append(";"); 162 config_data_js.append(";");
157 return config_data_js; 163 return config_data_js;
158 } 164 }
159 165
166 std::string GetThemeCSS(Profile* profile) {
167 ThemeService* theme_service = ThemeServiceFactory::GetForProfile(profile);
168 SkColor background_color =
169 theme_service->GetColor(ThemeProperties::COLOR_NTP_BACKGROUND);
170
171 return base::StringPrintf("body { background-color: #%02X%02X%02X; }",
172 SkColorGetR(background_color),
Mathieu 2015/10/14 20:53:03 can you run "git cl format" I'm thinking the inden
fserb 2015/10/20 18:48:16 Done.
173 SkColorGetG(background_color),
174 SkColorGetB(background_color));
175 }
176
160 std::string GetLocalNtpPath() { 177 std::string GetLocalNtpPath() {
161 return std::string(chrome::kChromeSearchScheme) + "://" + 178 return std::string(chrome::kChromeSearchScheme) + "://" +
162 std::string(chrome::kChromeSearchLocalNtpHost) + "/"; 179 std::string(chrome::kChromeSearchLocalNtpHost) + "/";
163 } 180 }
164 181
165 } // namespace 182 } // namespace
166 183
167 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) { 184 LocalNtpSource::LocalNtpSource(Profile* profile) : profile_(profile) {
168 } 185 }
169 186
170 LocalNtpSource::~LocalNtpSource() { 187 LocalNtpSource::~LocalNtpSource() {
171 } 188 }
172 189
173 std::string LocalNtpSource::GetSource() const { 190 std::string LocalNtpSource::GetSource() const {
174 return chrome::kChromeSearchLocalNtpHost; 191 return chrome::kChromeSearchLocalNtpHost;
175 } 192 }
176 193
177 void LocalNtpSource::StartDataRequest( 194 void LocalNtpSource::StartDataRequest(
178 const std::string& path, 195 const std::string& path,
179 int render_process_id, 196 int render_process_id,
180 int render_frame_id, 197 int render_frame_id,
181 const content::URLDataSource::GotDataCallback& callback) { 198 const content::URLDataSource::GotDataCallback& callback) {
182 std::string stripped_path = StripParameters(path); 199 std::string stripped_path = StripParameters(path);
183 if (stripped_path == kConfigDataFilename) { 200 if (stripped_path == kConfigDataFilename) {
184 std::string config_data_js = GetConfigData(profile_); 201 std::string config_data_js = GetConfigData(profile_);
185 callback.Run(base::RefCountedString::TakeString(&config_data_js)); 202 callback.Run(base::RefCountedString::TakeString(&config_data_js));
186 return; 203 return;
187 } 204 }
205 if (stripped_path == kThemeCSSFilename) {
206 std::string theme_css = GetThemeCSS(profile_);
207 callback.Run(base::RefCountedString::TakeString(&theme_css));
208 return;
209 }
188 210
189 #if !defined(GOOGLE_CHROME_BUILD) 211 #if !defined(GOOGLE_CHROME_BUILD)
190 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); 212 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
191 if (command_line->HasSwitch(switches::kLocalNtpReload)) { 213 if (command_line->HasSwitch(switches::kLocalNtpReload)) {
192 if (stripped_path == "local-ntp.html" || stripped_path == "local-ntp.js" || 214 if (stripped_path == "local-ntp.html" || stripped_path == "local-ntp.js" ||
193 stripped_path == "local-ntp.css") { 215 stripped_path == "local-ntp.css") {
194 base::ReplaceChars(stripped_path, "-", "_", &stripped_path); 216 base::ReplaceChars(stripped_path, "-", "_", &stripped_path);
195 local_ntp::SendLocalFileResource(stripped_path, callback); 217 local_ntp::SendLocalFileResource(stripped_path, callback);
196 return; 218 return;
197 } 219 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 } 263 }
242 } 264 }
243 return false; 265 return false;
244 } 266 }
245 267
246 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const { 268 std::string LocalNtpSource::GetContentSecurityPolicyFrameSrc() const {
247 // Allow embedding of most visited iframes. 269 // Allow embedding of most visited iframes.
248 return base::StringPrintf("frame-src %s;", 270 return base::StringPrintf("frame-src %s;",
249 chrome::kChromeSearchMostVisitedUrl); 271 chrome::kChromeSearchMostVisitedUrl);
250 } 272 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698