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

Side by Side Diff: extensions/renderer/dispatcher.cc

Issue 537773003: Add a user agent stylesheet for extension options pages (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Remove Data URI hardcoding, remove deprecated WebKit CSS declarations Created 6 years, 3 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "extensions/renderer/dispatcher.h" 5 #include "extensions/renderer/dispatcher.h"
6 6
7 #include "base/base64.h"
7 #include "base/bind.h" 8 #include "base/bind.h"
8 #include "base/callback.h" 9 #include "base/callback.h"
9 #include "base/command_line.h" 10 #include "base/command_line.h"
10 #include "base/debug/alias.h" 11 #include "base/debug/alias.h"
11 #include "base/memory/scoped_ptr.h" 12 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/user_metrics_action.h" 13 #include "base/metrics/user_metrics_action.h"
13 #include "base/strings/string_piece.h" 14 #include "base/strings/string_piece.h"
14 #include "base/strings/string_split.h" 15 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "base/values.h" 17 #include "base/values.h"
17 #include "content/grit/content_resources.h" 18 #include "content/grit/content_resources.h"
18 #include "content/public/common/content_switches.h" 19 #include "content/public/common/content_switches.h"
19 #include "content/public/common/url_constants.h" 20 #include "content/public/common/url_constants.h"
20 #include "content/public/renderer/render_thread.h" 21 #include "content/public/renderer/render_thread.h"
21 #include "content/public/renderer/render_view.h" 22 #include "content/public/renderer/render_view.h"
22 #include "content/public/renderer/v8_value_converter.h" 23 #include "content/public/renderer/v8_value_converter.h"
23 #include "extensions/common/api/messaging/message.h" 24 #include "extensions/common/api/messaging/message.h"
24 #include "extensions/common/constants.h" 25 #include "extensions/common/constants.h"
25 #include "extensions/common/extension.h" 26 #include "extensions/common/extension.h"
26 #include "extensions/common/extension_api.h" 27 #include "extensions/common/extension_api.h"
27 #include "extensions/common/extension_messages.h" 28 #include "extensions/common/extension_messages.h"
28 #include "extensions/common/extension_urls.h" 29 #include "extensions/common/extension_urls.h"
29 #include "extensions/common/features/feature.h" 30 #include "extensions/common/features/feature.h"
30 #include "extensions/common/features/feature_provider.h" 31 #include "extensions/common/features/feature_provider.h"
31 #include "extensions/common/manifest.h" 32 #include "extensions/common/manifest.h"
32 #include "extensions/common/manifest_constants.h" 33 #include "extensions/common/manifest_constants.h"
33 #include "extensions/common/manifest_handlers/background_info.h" 34 #include "extensions/common/manifest_handlers/background_info.h"
34 #include "extensions/common/manifest_handlers/externally_connectable.h" 35 #include "extensions/common/manifest_handlers/externally_connectable.h"
36 #include "extensions/common/manifest_handlers/options_page_info.h"
35 #include "extensions/common/manifest_handlers/sandboxed_page_info.h" 37 #include "extensions/common/manifest_handlers/sandboxed_page_info.h"
36 #include "extensions/common/message_bundle.h" 38 #include "extensions/common/message_bundle.h"
37 #include "extensions/common/permissions/permission_set.h" 39 #include "extensions/common/permissions/permission_set.h"
38 #include "extensions/common/permissions/permissions_data.h" 40 #include "extensions/common/permissions/permissions_data.h"
39 #include "extensions/common/switches.h" 41 #include "extensions/common/switches.h"
40 #include "extensions/common/view_type.h" 42 #include "extensions/common/view_type.h"
41 #include "extensions/renderer/api_activity_logger.h" 43 #include "extensions/renderer/api_activity_logger.h"
42 #include "extensions/renderer/api_definitions_natives.h" 44 #include "extensions/renderer/api_definitions_natives.h"
43 #include "extensions/renderer/app_runtime_custom_bindings.h" 45 #include "extensions/renderer/app_runtime_custom_bindings.h"
44 #include "extensions/renderer/app_window_custom_bindings.h" 46 #include "extensions/renderer/app_window_custom_bindings.h"
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 #include "third_party/WebKit/public/web/WebCustomElement.h" 87 #include "third_party/WebKit/public/web/WebCustomElement.h"
86 #include "third_party/WebKit/public/web/WebDataSource.h" 88 #include "third_party/WebKit/public/web/WebDataSource.h"
87 #include "third_party/WebKit/public/web/WebDocument.h" 89 #include "third_party/WebKit/public/web/WebDocument.h"
88 #include "third_party/WebKit/public/web/WebFrame.h" 90 #include "third_party/WebKit/public/web/WebFrame.h"
89 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" 91 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
90 #include "third_party/WebKit/public/web/WebScopedUserGesture.h" 92 #include "third_party/WebKit/public/web/WebScopedUserGesture.h"
91 #include "third_party/WebKit/public/web/WebSecurityPolicy.h" 93 #include "third_party/WebKit/public/web/WebSecurityPolicy.h"
92 #include "third_party/WebKit/public/web/WebView.h" 94 #include "third_party/WebKit/public/web/WebView.h"
93 #include "ui/base/layout.h" 95 #include "ui/base/layout.h"
94 #include "ui/base/resource/resource_bundle.h" 96 #include "ui/base/resource/resource_bundle.h"
97 #include "ui/resources/grit/webui_resources.h"
95 #include "v8/include/v8.h" 98 #include "v8/include/v8.h"
96 99
97 using base::UserMetricsAction; 100 using base::UserMetricsAction;
98 using blink::WebDataSource; 101 using blink::WebDataSource;
99 using blink::WebDocument; 102 using blink::WebDocument;
100 using blink::WebFrame; 103 using blink::WebFrame;
101 using blink::WebScopedUserGesture; 104 using blink::WebScopedUserGesture;
102 using blink::WebSecurityPolicy; 105 using blink::WebSecurityPolicy;
103 using blink::WebString; 106 using blink::WebString;
104 using blink::WebVector; 107 using blink::WebVector;
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 RouteFunction( 172 RouteFunction(
170 "GetChrome", 173 "GetChrome",
171 base::Bind(&ChromeNativeHandler::GetChrome, base::Unretained(this))); 174 base::Bind(&ChromeNativeHandler::GetChrome, base::Unretained(this)));
172 } 175 }
173 176
174 void GetChrome(const v8::FunctionCallbackInfo<v8::Value>& args) { 177 void GetChrome(const v8::FunctionCallbackInfo<v8::Value>& args) {
175 args.GetReturnValue().Set(GetOrCreateChrome(context())); 178 args.GetReturnValue().Set(GetOrCreateChrome(context()));
176 } 179 }
177 }; 180 };
178 181
182 // Converts the PNG image with the given resource_id into a Data URI. This is a
183 // workaround for including WebUI widget images in the user-agent stylesheet for
184 // extension options pages.
185 std::string GetPNGAsDataURI(int resource_id) {
Marijn Kruisselbrink 2014/09/10 20:46:30 Is this the same as webui::GetBitmapDataUrlFromRes
ericzeng 2014/09/10 21:29:30 Yes, done.
186 std::string str64;
187 base::Base64Encode(
188 ResourceBundle::GetSharedInstance().GetRawDataResource(resource_id),
189 &str64);
190 str64.insert(0, "data:image/png;base64,");
191 return str64;
192 }
193
179 } // namespace 194 } // namespace
180 195
181 Dispatcher::Dispatcher(DispatcherDelegate* delegate) 196 Dispatcher::Dispatcher(DispatcherDelegate* delegate)
182 : delegate_(delegate), 197 : delegate_(delegate),
183 content_watcher_(new ContentWatcher()), 198 content_watcher_(new ContentWatcher()),
184 source_map_(&ResourceBundle::GetSharedInstance()), 199 source_map_(&ResourceBundle::GetSharedInstance()),
185 v8_schema_registry_(new V8SchemaRegistry), 200 v8_schema_registry_(new V8SchemaRegistry),
186 is_webkit_initialized_(false), 201 is_webkit_initialized_(false),
187 user_script_set_manager_observer_(this) { 202 user_script_set_manager_observer_(this) {
188 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); 203 const CommandLine& command_line = *(CommandLine::ForCurrentProcess());
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after
342 script_context_set_.Remove(context); 357 script_context_set_.Remove(context);
343 VLOG(1) << "Num tracked contexts: " << script_context_set_.size(); 358 VLOG(1) << "Num tracked contexts: " << script_context_set_.size();
344 } 359 }
345 360
346 void Dispatcher::DidCreateDocumentElement(blink::WebFrame* frame) { 361 void Dispatcher::DidCreateDocumentElement(blink::WebFrame* frame) {
347 // Note: use GetEffectiveDocumentURL not just frame->document()->url() 362 // Note: use GetEffectiveDocumentURL not just frame->document()->url()
348 // so that this also injects the stylesheet on about:blank frames that 363 // so that this also injects the stylesheet on about:blank frames that
349 // are hosted in the extension process. 364 // are hosted in the extension process.
350 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL( 365 GURL effective_document_url = ScriptContext::GetEffectiveDocumentURL(
351 frame, frame->document().url(), true /* match_about_blank */); 366 frame, frame->document().url(), true /* match_about_blank */);
367
352 const Extension* extension = 368 const Extension* extension =
353 extensions_.GetExtensionOrAppByURL(effective_document_url); 369 extensions_.GetExtensionOrAppByURL(effective_document_url);
354 370
355 if (extension && 371 if (extension &&
356 (extension->is_extension() || extension->is_platform_app())) { 372 (extension->is_extension() || extension->is_platform_app())) {
357 int resource_id = 373 int resource_id = extension->is_platform_app() ? IDR_PLATFORM_APP_CSS
358 extension->is_platform_app() ? IDR_PLATFORM_APP_CSS : IDR_EXTENSION_CSS; 374 : IDR_EXTENSION_FONTS_CSS;
359 std::string stylesheet = ResourceBundle::GetSharedInstance() 375 std::string stylesheet = ResourceBundle::GetSharedInstance()
360 .GetRawDataResource(resource_id) 376 .GetRawDataResource(resource_id)
361 .as_string(); 377 .as_string();
362 ReplaceFirstSubstringAfterOffset( 378 ReplaceFirstSubstringAfterOffset(
363 &stylesheet, 0, "$FONTFAMILY", system_font_family_); 379 &stylesheet, 0, "$FONTFAMILY", system_font_family_);
364 ReplaceFirstSubstringAfterOffset( 380 ReplaceFirstSubstringAfterOffset(
365 &stylesheet, 0, "$FONTSIZE", system_font_size_); 381 &stylesheet, 0, "$FONTSIZE", system_font_size_);
366 382
367 // Blink doesn't let us define an additional user agent stylesheet, so 383 // Blink doesn't let us define an additional user agent stylesheet, so
368 // we insert the default platform app or extension stylesheet into all 384 // we insert the default platform app or extension stylesheet into all
369 // documents that are loaded in each app or extension. 385 // documents that are loaded in each app or extension.
370 frame->document().insertStyleSheet(WebString::fromUTF8(stylesheet)); 386 frame->document().insertStyleSheet(WebString::fromUTF8(stylesheet));
371 } 387 }
388
389 // If this is an extension options page, and the extension has opted into
390 // using Chrome styles, then insert the Chrome extension stylesheet.
391 if (extension && extension->is_extension() &&
392 OptionsPageInfo::ShouldUseChromeStyle(extension) &&
393 effective_document_url == OptionsPageInfo::GetOptionsPage(extension)) {
394 std::string stylesheet = ResourceBundle::GetSharedInstance()
395 .GetRawDataResource(IDR_EXTENSION_CSS)
396 .as_string();
397 // Checkboxes and drop down menus use the background-image declaration.
398 // Since extensions will not be able to load the image resource from the
399 // WebUI directory, load the images here and convert them to a data URI.
400 ReplaceSubstringsAfterOffset(
401 &stylesheet, 0, "$CHECK", GetPNGAsDataURI(IDR_WEBUI_IMAGES_CHECK));
402 ReplaceSubstringsAfterOffset(
403 &stylesheet,
404 0,
405 "$DISABLED_SELECT",
406 GetPNGAsDataURI(IDR_WEBUI_IMAGES_DISABLED_SELECT));
407 ReplaceSubstringsAfterOffset(
408 &stylesheet, 0, "$SELECT", GetPNGAsDataURI(IDR_WEBUI_IMAGES_SELECT));
409
410 frame->document().insertStyleSheet(WebString::fromUTF8(stylesheet));
Marijn Kruisselbrink 2014/09/10 20:46:30 Would it make sense to cache the preprocessed styl
ericzeng 2014/09/10 21:29:30 Yeah, I don't think options pages are going to be
411 }
372 content_watcher_->DidCreateDocumentElement(frame); 412 content_watcher_->DidCreateDocumentElement(frame);
373 } 413 }
374 414
375 void Dispatcher::DidMatchCSS( 415 void Dispatcher::DidMatchCSS(
376 blink::WebFrame* frame, 416 blink::WebFrame* frame,
377 const blink::WebVector<blink::WebString>& newly_matching_selectors, 417 const blink::WebVector<blink::WebString>& newly_matching_selectors,
378 const blink::WebVector<blink::WebString>& stopped_matching_selectors) { 418 const blink::WebVector<blink::WebString>& stopped_matching_selectors) {
379 content_watcher_->DidMatchCSS( 419 content_watcher_->DidMatchCSS(
380 frame, newly_matching_selectors, stopped_matching_selectors); 420 frame, newly_matching_selectors, stopped_matching_selectors);
381 } 421 }
(...skipping 942 matching lines...) Expand 10 before | Expand all | Expand 10 after
1324 return v8::Handle<v8::Object>(); 1364 return v8::Handle<v8::Object>();
1325 1365
1326 if (bind_name) 1366 if (bind_name)
1327 *bind_name = split.back(); 1367 *bind_name = split.back();
1328 1368
1329 return bind_object.IsEmpty() ? AsObjectOrEmpty(GetOrCreateChrome(context)) 1369 return bind_object.IsEmpty() ? AsObjectOrEmpty(GetOrCreateChrome(context))
1330 : bind_object; 1370 : bind_object;
1331 } 1371 }
1332 1372
1333 } // namespace extensions 1373 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698