From 1e1c2ad8d7ff3c908e8923558e6ef4f9d99d93a5 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Thu, 18 Nov 2010 21:05:25 +0000 Subject: [PATCH] Add global and per-browser settings (issue #145). This exposes the following new capabilities: - Ability to disable drag & drop from other windows. - Ability to specify additional plugin search paths. - Ability to customize WebPreferences values, including enabling cross-site scripting. - Ability to set User-Agent or product version. - Ability to set default locale. git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@144 5089003a-bbd8-11dd-ad1f-f1f9622dbc98 --- cef.gyp | 2 + include/cef.h | 355 +++++++++++++++++++---- include/cef_capi.h | 22 +- include/cef_linux.h | 40 ++- include/cef_mac.h | 46 ++- include/cef_string_list.h | 2 + include/cef_types.h | 156 ++++++++++ include/cef_win.h | 50 +++- libcef/browser_impl.cc | 28 +- libcef/browser_impl.h | 7 +- libcef/browser_impl_mac.mm | 7 +- libcef/browser_impl_win.cc | 13 +- libcef/browser_settings.cc | 214 ++++++++++++++ libcef/browser_settings.h | 14 + libcef/browser_webkit_glue.cc | 10 +- libcef/browser_webview_delegate.cc | 20 +- libcef/browser_webview_delegate.h | 5 - libcef/cef_context.cc | 79 ++--- libcef/cef_context.h | 18 +- libcef/cef_process_ui_thread.cc | 23 +- libcef/cef_string_list.cc | 7 + libcef_dll/cpptoc/handler_cpptoc.cc | 34 ++- libcef_dll/ctocpp/handler_ctocpp.cc | 7 +- libcef_dll/ctocpp/handler_ctocpp.h | 5 +- libcef_dll/libcef_dll.cc | 24 +- libcef_dll/wrapper/libcef_dll_wrapper.cc | 7 +- tests/cefclient/cefclient.h | 3 +- tests/cefclient/cefclient_mac.mm | 4 +- tests/cefclient/cefclient_win.cpp | 9 +- tests/unittests/test_handler.h | 3 +- tests/unittests/test_suite.h | 6 +- tools/cef_parser.py | 4 +- 32 files changed, 978 insertions(+), 246 deletions(-) create mode 100644 libcef/browser_settings.cc create mode 100644 libcef/browser_settings.h diff --git a/cef.gyp b/cef.gyp index 3f202ae12..d95b0da4c 100644 --- a/cef.gyp +++ b/cef.gyp @@ -483,6 +483,8 @@ 'libcef/browser_request_context.h', 'libcef/browser_resource_loader_bridge.cc', 'libcef/browser_resource_loader_bridge.h', + 'libcef/browser_settings.cc', + 'libcef/browser_settings.h', 'libcef/browser_socket_stream_bridge.cc', 'libcef/browser_socket_stream_bridge.h', 'libcef/browser_web_worker.h', diff --git a/include/cef.h b/include/cef.h index facb59c97..8462a94d1 100644 --- a/include/cef.h +++ b/include/cef.h @@ -45,37 +45,33 @@ #include "cef_types.h" class CefBrowser; +class CefBrowserSettings; class CefDownloadHandler; class CefFrame; class CefHandler; +class CefPopupFeatures; class CefPostData; class CefPostDataElement; class CefRequest; class CefSchemeHandler; class CefSchemeHandlerFactory; +class CefSettings; class CefStreamReader; class CefStreamWriter; class CefTask; class CefV8Handler; class CefV8Value; -class CefPopupFeatures; -// This function should only be called once when the application is started. -// Create the thread to host the UI message loop. A return value of true -// indicates that it succeeded and false indicates that it failed. Set -// |multi_threaded_message_loop| to true to have the message loop run in -// a separate thread. If |multi_threaded_message_loop| is false than -// the CefDoMessageLoopWork() function must be called from your message loop. -// Set |cache_path| to the location where cache data will be stored on disk. -// If |cache_path| is empty an in-memory cache will be used for cache data. +// This function should be called once when the application is started to +// initialize CEF. A return value of true indicates that it succeeded and +// false indicates that it failed. /*--cef()--*/ -bool CefInitialize(bool multi_threaded_message_loop, - const std::wstring& cache_path); +bool CefInitialize(const CefSettings& settings, + const CefBrowserSettings& browser_defaults); -// This function should only be called once before the application exits. -// Shut down the thread hosting the UI message loop and destroy any created -// windows. +// This function should be called once before the application exits to shut down +// CEF. /*--cef()--*/ void CefShutdown(); @@ -559,9 +555,10 @@ public: /*--cef()--*/ virtual RetVal HandleBeforeCreated(CefRefPtr parentBrowser, CefWindowInfo& windowInfo, bool popup, + const CefPopupFeatures& popupFeatures, CefRefPtr& handler, std::wstring& url, - const CefPopupFeatures& popupFeatures) =0; + CefBrowserSettings& settings) =0; // Event called after a new window is created. The return value is currently // ignored. @@ -1495,10 +1492,9 @@ public: { Init(); } - ~CefPopupFeatures() + virtual ~CefPopupFeatures() { - if(additionalFeatures) - cef_string_list_free(additionalFeatures); + Reset(); } CefPopupFeatures(const CefPopupFeatures& r) @@ -1512,6 +1508,55 @@ public: *this = r; } + void Reset() + { + if(additionalFeatures) + cef_string_list_free(additionalFeatures); + } + + void Attach(const cef_popup_features_t& r) + { + Reset(); + *static_cast(this) = r; + } + + void Detach() + { + Init(); + } + + CefPopupFeatures& operator=(const CefPopupFeatures& r) + { + return operator=(static_cast(r)); + } + + CefPopupFeatures& operator=(const cef_popup_features_t& r) + { + if(additionalFeatures) + cef_string_list_free(additionalFeatures); + additionalFeatures = r.additionalFeatures ? + cef_string_list_copy(r.additionalFeatures) : NULL; + + x = r.x; + xSet = r.xSet; + y = r.y; + ySet = r.ySet; + width = r.width; + widthSet = r.widthSet; + height = r.height; + heightSet = r.heightSet; + menuBarVisible = r.menuBarVisible; + statusBarVisible = r.statusBarVisible; + toolBarVisible = r.toolBarVisible; + locationBarVisible = r.locationBarVisible; + scrollbarsVisible = r.scrollbarsVisible; + resizable = r.resizable; + fullscreen = r.fullscreen; + dialog = r.dialog; + return *this; + } + +protected: void Init() { x = 0; @@ -1534,47 +1579,255 @@ public: dialog = false; additionalFeatures = NULL; } +}; - CefPopupFeatures& operator=(const CefPopupFeatures& r) + +// Class representing initialization settings. +class CefSettings : public cef_settings_t +{ +public: + CefSettings() { - return operator=(static_cast(r)); + Init(); + } + virtual ~CefSettings() + { + Reset(); } - CefPopupFeatures& operator=(const cef_popup_features_t& r) + CefSettings(const CefSettings& r) { - if(additionalFeatures) - cef_string_list_free(additionalFeatures); - if(r.additionalFeatures) { - additionalFeatures = cef_string_list_alloc(); - unsigned int size = cef_string_list_size(r.additionalFeatures); - for(unsigned int i = 0; i < size; ++i) { - cef_string_t feature = cef_string_list_value(r.additionalFeatures, i); - cef_string_list_append(additionalFeatures, feature); - cef_string_free(feature); - } - } - else { - additionalFeatures = NULL; - } + Init(); + *this = r; + } + CefSettings(const cef_settings_t& r) + { + Init(); + *this = r; + } + + void Reset() + { + if(cache_path) + cef_string_free(cache_path); + if(user_agent) + cef_string_free(user_agent); + if(product_version) + cef_string_free(product_version); + if(locale) + cef_string_free(locale); + if(extra_plugin_paths) + cef_string_list_free(extra_plugin_paths); + Init(); + } + + void Attach(const cef_settings_t& r) + { + Reset(); + *static_cast(this) = r; + } + + void Detach() + { + Init(); + } + + CefSettings& operator=(const CefSettings& r) + { + return operator=(static_cast(r)); + } + + CefSettings& operator=(const cef_settings_t& r) + { + multi_threaded_message_loop = r.multi_threaded_message_loop; + + if(cache_path) + cef_string_free(cache_path); + cache_path = r.cache_path ? cef_string_alloc(r.cache_path) : NULL; + + if(user_agent) + cef_string_free(user_agent); + user_agent = r.user_agent ? cef_string_alloc(r.user_agent) : NULL; + + if(product_version) + cef_string_free(product_version); + product_version = r.product_version ? + cef_string_alloc(r.product_version) : NULL; + + if(locale) + cef_string_free(locale); + locale = r.locale ? cef_string_alloc(r.locale) : NULL; + + if(extra_plugin_paths) + cef_string_list_free(extra_plugin_paths); + extra_plugin_paths = r.extra_plugin_paths ? + cef_string_list_copy(r.extra_plugin_paths) : NULL; - x = r.x; - xSet = r.xSet; - y = r.y; - ySet = r.ySet; - width = r.width; - widthSet = r.widthSet; - height = r.height; - heightSet = r.heightSet; - menuBarVisible = r.menuBarVisible; - statusBarVisible = r.statusBarVisible; - toolBarVisible = r.toolBarVisible; - locationBarVisible = r.locationBarVisible; - scrollbarsVisible = r.scrollbarsVisible; - resizable = r.resizable; - fullscreen = r.fullscreen; - dialog = r.dialog; return *this; } + +protected: + void Init() + { + memset(static_cast(this), 0, sizeof(cef_settings_t)); + size = sizeof(cef_settings_t); + } +}; + + +// Class representing browser initialization settings. +class CefBrowserSettings : public cef_browser_settings_t +{ +public: + CefBrowserSettings() + { + Init(); + } + virtual ~CefBrowserSettings() + { + Reset(); + } + + CefBrowserSettings(const CefBrowserSettings& r) + { + Init(); + *this = r; + } + CefBrowserSettings(const cef_browser_settings_t& r) + { + Init(); + *this = r; + } + + void Reset() + { + if(standard_font_family) + cef_string_free(standard_font_family); + if(fixed_font_family) + cef_string_free(fixed_font_family); + if(serif_font_family) + cef_string_free(serif_font_family); + if(sans_serif_font_family) + cef_string_free(sans_serif_font_family); + if(cursive_font_family) + cef_string_free(cursive_font_family); + if(fantasy_font_family) + cef_string_free(fantasy_font_family); + if(default_encoding) + cef_string_free(default_encoding); + if(user_style_sheet_location) + cef_string_free(user_style_sheet_location); + Init(); + } + + void Attach(const cef_browser_settings_t& r) + { + Reset(); + *static_cast(this) = r; + } + + void Detach() + { + Init(); + } + + CefBrowserSettings& operator=(const CefBrowserSettings& r) + { + return operator=(static_cast(r)); + } + + CefBrowserSettings& operator=(const cef_browser_settings_t& r) + { + drag_drop_disabled = r.drag_drop_disabled; + + if(standard_font_family) + cef_string_free(standard_font_family); + standard_font_family = r.standard_font_family ? + cef_string_alloc(r.standard_font_family) : NULL; + + if(fixed_font_family) + cef_string_free(fixed_font_family); + fixed_font_family = r.fixed_font_family ? + cef_string_alloc(r.fixed_font_family) : NULL; + + if(serif_font_family) + cef_string_free(serif_font_family); + serif_font_family = r.serif_font_family ? + cef_string_alloc(r.serif_font_family) : NULL; + + if(sans_serif_font_family) + cef_string_free(sans_serif_font_family); + serif_font_family = r.sans_serif_font_family ? + cef_string_alloc(r.sans_serif_font_family) : NULL; + + if(cursive_font_family) + cef_string_free(cursive_font_family); + cursive_font_family = r.cursive_font_family ? + cef_string_alloc(r.cursive_font_family) : NULL; + + if(fantasy_font_family) + cef_string_free(fantasy_font_family); + fantasy_font_family = r.fantasy_font_family ? + cef_string_alloc(r.fantasy_font_family) : NULL; + + default_font_size = r.default_font_size; + default_fixed_font_size = r.default_fixed_font_size; + minimum_font_size = r.minimum_font_size; + minimum_logical_font_size = r.minimum_logical_font_size; + remote_fonts_disabled = r.remote_fonts_disabled; + + if(default_encoding) + cef_string_free(default_encoding); + default_encoding = r.default_encoding ? + cef_string_alloc(r.default_encoding) : NULL; + + encoding_detector_enabled = r.encoding_detector_enabled; + javascript_disabled = r.javascript_disabled; + javascript_open_windows_disallowed = r.javascript_open_windows_disallowed; + javascript_close_windows_disallowed = r.javascript_close_windows_disallowed; + javascript_access_clipboard_disallowed = + r.javascript_access_clipboard_disallowed; + dom_paste_disabled = r.dom_paste_disabled; + caret_browsing_enabled = r.caret_browsing_enabled; + java_disabled = r.java_disabled; + plugins_disabled = r.plugins_disabled; + universal_access_from_file_urls_allowed = + r.universal_access_from_file_urls_allowed; + file_access_from_file_urls_allowed = r.file_access_from_file_urls_allowed; + web_security_disabled = r.web_security_disabled; + xss_auditor_enabled = r.xss_auditor_enabled; + image_load_disabled = r.image_load_disabled; + shrink_standalone_images_to_fit = r.shrink_standalone_images_to_fit; + site_specific_quirks_disabled = r.site_specific_quirks_disabled; + text_area_resize_disabled = r.text_area_resize_disabled; + page_cache_disabled = r.page_cache_disabled; + tab_to_links_disabled = r.tab_to_links_disabled; + hyperlink_auditing_disabled = r.hyperlink_auditing_disabled; + user_style_sheet_enabled = r.user_style_sheet_enabled; + + if(user_style_sheet_location) + cef_string_free(user_style_sheet_location); + user_style_sheet_location = r.user_style_sheet_location ? + cef_string_alloc(r.user_style_sheet_location) : NULL; + + author_and_user_styles_disabled = r.author_and_user_styles_disabled; + local_storage_disabled = r.local_storage_disabled; + databases_disabled = r.databases_disabled; + application_cache_disabled = r.application_cache_disabled; + experimental_webgl_enabled = r.experimental_webgl_enabled; + accelerated_compositing_disabled = r.accelerated_compositing_disabled; + accelerated_2d_canvas_disabled = r.accelerated_2d_canvas_disabled; + + return *this; + } + +protected: + void Init() + { + memset(static_cast(this), 0, + sizeof(cef_browser_settings_t)); + size = sizeof(cef_browser_settings_t); + } }; #endif // _CEF_H diff --git a/include/cef_capi.h b/include/cef_capi.h index 222c4d035..112811d8c 100644 --- a/include/cef_capi.h +++ b/include/cef_capi.h @@ -48,19 +48,14 @@ extern "C" { #include "cef_types.h" -// This function should only be called once when the application is started. -// Create the thread to host the UI message loop. A return value of true (1) -// indicates that it succeeded and false (0) indicates that it failed. Set -// |multi_threaded_message_loop| to true (1) to have the message loop run in a -// separate thread. If |multi_threaded_message_loop| is false (0) than the -// cef_do_message_loop_work() function must be called from your message loop. -// Set |cache_path| to the location where cache data will be stored on disk. If -// |cache_path| is NULL an in-memory cache will be used for cache data. -CEF_EXPORT int cef_initialize(int multi_threaded_message_loop, - const wchar_t* cache_path); +// This function should be called once when the application is started to +// initialize CEF. A return value of true (1) indicates that it succeeded and +// false (0) indicates that it failed. +CEF_EXPORT int cef_initialize(const struct _cef_settings_t* settings, + const struct _cef_browser_settings_t* browser_defaults); -// This function should only be called once before the application exits. Shut -// down the thread hosting the UI message loop and destroy any created windows. +// This function should be called once before the application exits to shut down +// CEF. CEF_EXPORT void cef_shutdown(); // Perform message loop processing. Has no affect if the browser UI loop is @@ -382,8 +377,9 @@ typedef struct _cef_handler_t enum cef_retval_t (CEF_CALLBACK *handle_before_created)( struct _cef_handler_t* self, struct _cef_browser_t* parentBrowser, struct _cef_window_info_t* windowInfo, int popup, + const struct _cef_popup_features_t* popupFeatures, struct _cef_handler_t** handler, cef_string_t* url, - const struct _cef_popup_features_t* popupFeatures); + struct _cef_browser_settings_t* settings); // Event called after a new window is created. The return value is currently // ignored. diff --git a/include/cef_linux.h b/include/cef_linux.h index d1b4bef3c..42b3988f0 100644 --- a/include/cef_linux.h +++ b/include/cef_linux.h @@ -55,7 +55,7 @@ public: pthread_mutexattr_settype(&attr_, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&lock_, &attr_); } - ~CefCriticalSection() + virtual ~CefCriticalSection() { pthread_mutex_destroy(&lock_); pthread_mutexattr_destroy(&attr_); @@ -81,10 +81,9 @@ public: { Init(); } - ~CefWindowInfo() + virtual ~CefWindowInfo() { - if(m_windowName) - cef_string_free(m_windowName); + Reset(); } CefWindowInfo(const CefWindowInfo& r) @@ -98,13 +97,22 @@ public: *this = r; } - void Init() + void Reset() { - m_windowName = NULL; - m_x = 0; - m_y = 0; - m_nWidth = 0; - m_nHeight = 0; + if(m_windowName) + cef_string_free(m_windowName); + Init(); + } + + void Attach(const cef_window_info_t& r) + { + Reset(); + *static_cast(this) = r; + } + + void Detach() + { + Init(); } CefWindowInfo& operator=(const CefWindowInfo& r) @@ -125,6 +133,16 @@ public: m_nHeight = r.m_nHeight; return *this; } + +protected: + void Init() + { + m_windowName = NULL; + m_x = 0; + m_y = 0; + m_nWidth = 0; + m_nHeight = 0; + } }; // Class representing print context information. @@ -135,7 +153,7 @@ public: { Init(); } - ~CefPrintInfo() + virtual ~CefPrintInfo() { } diff --git a/include/cef_mac.h b/include/cef_mac.h index be7ef45d4..dc41683b6 100644 --- a/include/cef_mac.h +++ b/include/cef_mac.h @@ -58,7 +58,7 @@ public: pthread_mutexattr_settype(&attr_, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init(&lock_, &attr_); } - ~CefCriticalSection() + virtual ~CefCriticalSection() { pthread_mutex_destroy(&lock_); pthread_mutexattr_destroy(&attr_); @@ -84,10 +84,9 @@ public: { Init(); } - ~CefWindowInfo() + virtual ~CefWindowInfo() { - if(m_windowName) - cef_string_free(m_windowName); + Reset(); } CefWindowInfo(const CefWindowInfo& r) @@ -101,17 +100,24 @@ public: *this = r; } - void Init() + void Reset() { - m_View = NULL; - m_ParentView = NULL; - m_windowName = NULL; - m_x = 0; - m_y = 0; - m_nWidth = 0; - m_nHeight = 0; + if(m_windowName) + cef_string_free(m_windowName); + Init(); } - + + void Attach(const cef_window_info_t& r) + { + Reset(); + *static_cast(this) = r; + } + + void Detach() + { + Init(); + } + void SetAsChild(CefWindowHandle ParentView, int x, int y, int width, int height) { @@ -142,6 +148,18 @@ public: m_nHeight = r.m_nHeight; return *this; } + +protected: + void Init() + { + m_View = NULL; + m_ParentView = NULL; + m_windowName = NULL; + m_x = 0; + m_y = 0; + m_nWidth = 0; + m_nHeight = 0; + } }; // Class representing print context information. @@ -152,7 +170,7 @@ public: { Init(); } - ~CefPrintInfo() + virtual ~CefPrintInfo() { } diff --git a/include/cef_string_list.h b/include/cef_string_list.h index 4d80ecff3..40c40b3be 100644 --- a/include/cef_string_list.h +++ b/include/cef_string_list.h @@ -59,6 +59,8 @@ CEF_EXPORT void cef_string_list_clear(cef_string_list_t list); // Free the string list. CEF_EXPORT void cef_string_list_free(cef_string_list_t list); +// Creates a copy of an existing string list. +CEF_EXPORT cef_string_list_t cef_string_list_copy(cef_string_list_t list); #ifdef __cplusplus } diff --git a/include/cef_types.h b/include/cef_types.h index 7574cf804..a5428bdba 100644 --- a/include/cef_types.h +++ b/include/cef_types.h @@ -54,6 +54,162 @@ typedef long int64; typedef long long int64; #endif +// Initialization settings. Specify NULL or 0 to get the recommended default +// values. +typedef struct _cef_settings_t +{ + // Size of this structure. + size_t size; + + // Set to true (1) to have the message loop run in a separate thread. If + // false (0) than the CefDoMessageLoopWork() function must be called from + // your application message loop. + bool multi_threaded_message_loop; + + // The location where cache data will be stored on disk. If empty an + // in-memory cache will be used. HTML5 databases such as localStorage will + // only persist across sessions if a cache path is specified. + cef_string_t cache_path; + + // Value that will be returned as the User-Agent HTTP header. If empty the + // default User-Agent string will be used. + cef_string_t user_agent; + + // Value that will be inserted as the product portion of the default + // User-Agent string. If empty the Chromium product version will be used. If + // |userAgent| is specified this value will be ignored. + cef_string_t product_version; + + // The locale string that will be passed to WebKit. If empty the default + // locale of "en-US" will be used. + cef_string_t locale; + + // List of file system paths that will be searched by the browser to locate + // plugins. This is in addition to the default search paths. + cef_string_list_t extra_plugin_paths; +} cef_settings_t; + +// Browser initialization settings. Specify NULL or 0 to get the recommended +// default values. The consequences of using custom values may not be well +// tested. +typedef struct _cef_browser_settings_t +{ + // Size of this structure. + size_t size; + + // Disable drag & drop of URLs from other windows. + bool drag_drop_disabled; + + // The below values map to WebPreferences settings. + + // Font settings. + cef_string_t standard_font_family; + cef_string_t fixed_font_family; + cef_string_t serif_font_family; + cef_string_t sans_serif_font_family; + cef_string_t cursive_font_family; + cef_string_t fantasy_font_family; + int default_font_size; + int default_fixed_font_size; + int minimum_font_size; + int minimum_logical_font_size; + + // Set to true (1) to disable loading of fonts from remote sources. + bool remote_fonts_disabled; + + // Default encoding for Web content. If empty "ISO-8859-1" will be used. + cef_string_t default_encoding; + + // Set to true (1) to attempt automatic detection of content encoding. + bool encoding_detector_enabled; + + // Set to true (1) to disable JavaScript. + bool javascript_disabled; + + // Set to true (1) to disallow JavaScript from opening windows. + bool javascript_open_windows_disallowed; + + // Set to true (1) to disallow JavaScript from closing windows. + bool javascript_close_windows_disallowed; + + // Set to true (1) to disallow JavaScript from accessing the clipboard. + bool javascript_access_clipboard_disallowed; + + // Set to true (1) to disable DOM pasting in the editor. DOM pasting also + // depends on |javascript_cannot_access_clipboard| being false (0). + bool dom_paste_disabled; + + // Set to true (1) to enable drawing of the caret position. + bool caret_browsing_enabled; + + // Set to true (1) to disable Java. + bool java_disabled; + + // Set to true (1) to disable plugins. + bool plugins_disabled; + + // Set to true (1) to allow access to all URLs from file URLs. + bool universal_access_from_file_urls_allowed; + + // Set to true (1) to allow access to file URLs from other file URLs. + bool file_access_from_file_urls_allowed; + + // Set to true (1) to allow risky security behavior such as cross-site + // scripting (XSS). Use with extreme care. + bool web_security_disabled; + + // Set to true (1) to enable console warnings about XSS attempts. + bool xss_auditor_enabled; + + // Set to true (1) to suppress the network load of image URLs. A cached + // image will still be rendered if requested. + bool image_load_disabled; + + // Set to true (1) to shrink standalone images to fit the page. + bool shrink_standalone_images_to_fit; + + // Set to true (1) to disable browser backwards compatibility features. + bool site_specific_quirks_disabled; + + // Set to true (1) to disable resize of text areas. + bool text_area_resize_disabled; + + // Set to true (1) to disable use of the page cache. + bool page_cache_disabled; + + // Set to true (1) to not have the tab key advance focus to links. + bool tab_to_links_disabled; + + // Set to true (1) to disable hyperlink pings ( and window.sendPing). + bool hyperlink_auditing_disabled; + + // Set to true (1) to enable the user style sheet for all pages. + // |user_style_sheet_location| must be set to the style sheet URL. + bool user_style_sheet_enabled; + cef_string_t user_style_sheet_location; + + // Set to true (1) to disable style sheets. + bool author_and_user_styles_disabled; + + // Set to true (1) to disable local storage. + bool local_storage_disabled; + + // Set to true (1) to disable databases. + bool databases_disabled; + + // Set to true (1) to disable application cache. + bool application_cache_disabled; + + // Set to true (1) to enable experimental WebGL features. + bool experimental_webgl_enabled; + + // Set to true (1) to disable accelerated compositing. + bool accelerated_compositing_disabled; + + // Set to true (1) to disable accelerated 2d canvas. + bool accelerated_2d_canvas_disabled; +} cef_browser_settings_t; + // Define handler return value types. Returning RV_HANDLED indicates // that the implementation completely handled the method and that no further // processing is required. Returning RV_CONTINUE indicates that the diff --git a/include/cef_win.h b/include/cef_win.h index f6d33d5dc..5dfb19471 100644 --- a/include/cef_win.h +++ b/include/cef_win.h @@ -48,7 +48,7 @@ public: memset(&m_sec, 0, sizeof(CRITICAL_SECTION)); InitializeCriticalSection(&m_sec); } - ~CefCriticalSection() + virtual ~CefCriticalSection() { DeleteCriticalSection(&m_sec); } @@ -72,10 +72,9 @@ public: { Init(); } - ~CefWindowInfo() + virtual ~CefWindowInfo() { - if(m_windowName) - cef_string_free(m_windowName); + Reset(); } CefWindowInfo(const CefWindowInfo& r) @@ -89,18 +88,22 @@ public: *this = r; } - void Init() + void Reset() { - m_dwExStyle = 0; - m_windowName = NULL; - m_dwStyle = 0; - m_x = 0; - m_y = 0; - m_nWidth = 0; - m_nHeight = 0; - m_hWndParent = NULL; - m_hMenu = 0; - m_hWnd = NULL; + if(m_windowName) + cef_string_free(m_windowName); + Init(); + } + + void Attach(const cef_window_info_t& r) + { + Reset(); + *static_cast(this) = r; + } + + void Detach() + { + Init(); } CefWindowInfo& operator=(const CefWindowInfo& r) @@ -154,6 +157,21 @@ public: else m_windowName = NULL; } + +protected: + void Init() + { + m_dwExStyle = 0; + m_windowName = NULL; + m_dwStyle = 0; + m_x = 0; + m_y = 0; + m_nWidth = 0; + m_nHeight = 0; + m_hWndParent = NULL; + m_hMenu = 0; + m_hWnd = NULL; + } }; // Class representing print context information. @@ -164,7 +182,7 @@ public: { Init(); } - ~CefPrintInfo() + virtual ~CefPrintInfo() { } diff --git a/libcef/browser_impl.cc b/libcef/browser_impl.cc index 28a6d8596..123996b41 100644 --- a/libcef/browser_impl.cc +++ b/libcef/browser_impl.cc @@ -46,10 +46,11 @@ using webkit_glue::WebStringToStdString; using webkit_glue::WebStringToStdWString; -CefBrowserImpl::CefBrowserImpl(CefWindowInfo& windowInfo, bool popup, +CefBrowserImpl::CefBrowserImpl(const CefWindowInfo& windowInfo, + const CefBrowserSettings& settings, bool popup, CefRefPtr handler) - : window_info_(windowInfo), is_popup_(popup), is_modal_(false), - handler_(handler), webviewhost_(NULL), popuphost_(NULL), + : window_info_(windowInfo), settings_(settings), is_popup_(popup), + is_modal_(false), handler_(handler), webviewhost_(NULL), popuphost_(NULL), frame_main_(NULL), unique_id_(0) { delegate_.reset(new BrowserWebViewDelegate(this)); @@ -486,6 +487,7 @@ std::wstring CefBrowserImpl::GetURL(CefRefPtr frame) return std::wstring(); } +// static bool CefBrowser::CreateBrowser(CefWindowInfo& windowInfo, bool popup, CefRefPtr handler, const std::wstring& url) @@ -494,24 +496,26 @@ bool CefBrowser::CreateBrowser(CefWindowInfo& windowInfo, bool popup, return false; std::wstring newUrl = url; + CefBrowserSettings settings(_Context->browser_defaults()); if(handler.get()) { // Give the handler an opportunity to modify window attributes, handler, // or cancel the window creation. CefHandler::RetVal rv = handler->HandleBeforeCreated(NULL, windowInfo, - popup, handler, newUrl, CefPopupFeatures()); + popup, CefPopupFeatures(), handler, newUrl, settings); if(rv == RV_HANDLED) return false; } CefRefPtr browser( - new CefBrowserImpl(windowInfo, popup, handler)); + new CefBrowserImpl(windowInfo, settings, popup, handler)); CefThread::PostTask(CefThread::UI, FROM_HERE, NewRunnableMethod(browser.get(), &CefBrowserImpl::UIT_CreateBrowser, newUrl)); return true; } +// static CefRefPtr CefBrowser::CreateBrowserSync(CefWindowInfo& windowInfo, bool popup, CefRefPtr handler, const std::wstring& url) { @@ -520,18 +524,20 @@ CefRefPtr CefBrowser::CreateBrowserSync(CefWindowInfo& windowInfo, std::wstring newUrl = url; CefRefPtr alternateBrowser; + CefBrowserSettings settings(_Context->browser_defaults()); if(handler.get()) { // Give the handler an opportunity to modify window attributes, handler, // or cancel the window creation. CefHandler::RetVal rv = handler->HandleBeforeCreated(NULL, windowInfo, - popup, handler, newUrl, CefPopupFeatures()); + popup, CefPopupFeatures(), handler, newUrl, settings); if(rv == RV_HANDLED) return false; } - CefRefPtr browser(new CefBrowserImpl(windowInfo, popup, handler)); + CefRefPtr browser( + new CefBrowserImpl(windowInfo, settings, popup, handler)); static_cast(browser.get())->UIT_CreateBrowser(newUrl); return browser; @@ -802,17 +808,21 @@ CefRefPtr CefBrowserImpl::UIT_CreatePopupWindow( CefRefPtr handler = handler_; std::wstring newUrl = url; + // Start with the current browser window's settings. + CefBrowserSettings settings(settings_); + if(handler_.get()) { // Give the handler an opportunity to modify window attributes, handler, // or cancel the window creation. CefHandler::RetVal rv = handler_->HandleBeforeCreated(this, info, true, - handler, newUrl, features); + features, handler, newUrl, settings); if(rv == RV_HANDLED) return NULL; } - CefRefPtr browser(new CefBrowserImpl(info, true, handler)); + CefRefPtr browser( + new CefBrowserImpl(info, settings, true, handler)); browser->UIT_CreateBrowser(newUrl); return browser; diff --git a/libcef/browser_impl.h b/libcef/browser_impl.h index fdf2d330e..78599037e 100644 --- a/libcef/browser_impl.h +++ b/libcef/browser_impl.h @@ -33,7 +33,8 @@ class WebView; class CefBrowserImpl : public CefThreadSafeBase { public: - CefBrowserImpl(CefWindowInfo& windowInfo, bool popup, + CefBrowserImpl(const CefWindowInfo& windowInfo, + const CefBrowserSettings& settings, bool popup, CefRefPtr handler); virtual ~CefBrowserImpl() {} @@ -102,7 +103,7 @@ public: const std::wstring& scriptUrl, int startLine); std::wstring GetURL(CefRefPtr frame); - + WebKit::WebView* GetWebView() const { return webviewhost_.get() ? webviewhost_->webview() : NULL; } @@ -231,10 +232,12 @@ public: static bool ImplementsThreadSafeReferenceCounting() { return true; } + const CefBrowserSettings& settings() const { return settings_; } const FilePath& file_system_root() const { return file_system_root_.path(); } protected: CefWindowInfo window_info_; + CefBrowserSettings settings_; bool is_popup_; bool is_modal_; CefRefPtr handler_; diff --git a/libcef/browser_impl_mac.mm b/libcef/browser_impl_mac.mm index 5b8f20e67..70849df7f 100644 --- a/libcef/browser_impl_mac.mm +++ b/libcef/browser_impl_mac.mm @@ -5,6 +5,7 @@ #include "cef_context.h" #include "browser_impl.h" +#include "browser_settings.h" #include "browser_webview_mac.h" #import @@ -14,6 +15,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebRect.h" #include "third_party/WebKit/WebKit/chromium/public/WebSize.h" #include "third_party/WebKit/WebKit/chromium/public/WebView.h" +#include "webkit/glue/webpreferences.h" using WebKit::WebRect; using WebKit::WebSize; @@ -44,10 +46,13 @@ void CefBrowserImpl::UIT_CreateBrowser(const std::wstring& url) gfx::Rect contentRect(window_info_.m_x, window_info_.m_y, window_info_.m_nWidth, window_info_.m_nHeight); + WebPreferences prefs; + BrowserToWebSettings(settings_, prefs); + // Create the webview host object webviewhost_.reset( WebViewHost::Create(parentView, contentRect, delegate_.get(), - NULL, *_Context->web_preferences())); + NULL, prefs)); delegate_->RegisterDragDrop(); BrowserWebView* browserView = (BrowserWebView*)webviewhost_->view_handle(); diff --git a/libcef/browser_impl_win.cc b/libcef/browser_impl_win.cc index f346b6411..f58e4fec0 100644 --- a/libcef/browser_impl_win.cc +++ b/libcef/browser_impl_win.cc @@ -5,6 +5,7 @@ #include "cef_context.h" #include "browser_impl.h" +#include "browser_settings.h" #include "printing/units.h" #include "base/utf_string_conversions.h" @@ -14,6 +15,7 @@ #include "third_party/WebKit/WebKit/chromium/public/WebRect.h" #include "third_party/WebKit/WebKit/chromium/public/WebSize.h" #include "third_party/WebKit/WebKit/chromium/public/WebView.h" +#include "webkit/glue/webpreferences.h" #include #include @@ -116,12 +118,17 @@ void CefBrowserImpl::UIT_CreateBrowser(const std::wstring& url) // Add the new browser to the list maintained by the context _Context->AddBrowser(this); + WebPreferences prefs; + BrowserToWebSettings(settings_, prefs); + // Create the webview host object webviewhost_.reset( WebViewHost::Create(window_info_.m_hWnd, gfx::Rect(), delegate_.get(), - NULL, *_Context->web_preferences())); - delegate_->RegisterDragDrop(); - + NULL, prefs)); + + if (!settings_.drag_drop_disabled) + delegate_->RegisterDragDrop(); + // Size the web view window to the browser window RECT cr; GetClientRect(window_info_.m_hWnd, &cr); diff --git a/libcef/browser_settings.cc b/libcef/browser_settings.cc new file mode 100644 index 000000000..767c6910a --- /dev/null +++ b/libcef/browser_settings.cc @@ -0,0 +1,214 @@ +// Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that can +// be found in the LICENSE file. + +#include "../include/cef.h" +#include "base/utf_string_conversions.h" +#include "webkit/glue/webpreferences.h" + +void BrowserToWebSettings(const CefBrowserSettings& cef, WebPreferences& web) +{ + if (cef.standard_font_family) + web.standard_font_family = cef.standard_font_family; + else + web.standard_font_family = L"Times"; + + if (cef.fixed_font_family) + web.fixed_font_family = cef.fixed_font_family; + else + web.fixed_font_family = L"Courier"; + + if (cef.serif_font_family) + web.serif_font_family = cef.serif_font_family; + else + web.serif_font_family = L"Times"; + + if (cef.sans_serif_font_family) + web.sans_serif_font_family = cef.sans_serif_font_family; + else + web.sans_serif_font_family = L"Helvetica"; + + // These two fonts below are picked from the intersection of + // Win XP font list and Vista font list : + // http://www.microsoft.com/typography/fonts/winxp.htm + // http://blogs.msdn.com/michkap/archive/2006/04/04/567881.aspx + // Some of them are installed only with CJK and complex script + // support enabled on Windows XP and are out of consideration here. + // (although we enabled both on our buildbots.) + // They (especially Impact for fantasy) are not typical cursive + // and fantasy fonts, but it should not matter for layout tests + // as long as they're available. + + if (cef.cursive_font_family) { + web.cursive_font_family = cef.cursive_font_family; + } else { +#if defined(OS_MACOSX) + web.cursive_font_family = L"Apple Chancery"; +#else + web.cursive_font_family = L"Comic Sans MS"; +#endif + } + + if (cef.fantasy_font_family) { + web.fantasy_font_family = cef.fantasy_font_family; + } else { +#if defined(OS_MACOSX) + web.fantasy_font_family = L"Papyrus"; +#else + web.fantasy_font_family = L"Impact"; +#endif + } + + if (cef.default_font_size > 0) + web.default_font_size = cef.default_font_size; + else + web.default_font_size = 16; + + if (cef.default_fixed_font_size > 0) + web.default_fixed_font_size = cef.default_fixed_font_size; + else + web.default_fixed_font_size = 13; + + if (cef.minimum_font_size > 0) + web.minimum_font_size = cef.minimum_font_size; + else + web.minimum_font_size = 1; + + if (cef.minimum_logical_font_size > 0) + web.minimum_logical_font_size = cef.minimum_logical_font_size; + else + web.minimum_logical_font_size = 9; + + if (cef.default_encoding) + web.default_encoding = WideToUTF8(cef.default_encoding); + else + web.default_encoding = "ISO-8859-1"; + + web.javascript_enabled = !cef.javascript_disabled; + web.web_security_enabled = !cef.web_security_disabled; + web.javascript_can_open_windows_automatically = + !cef.javascript_open_windows_disallowed; + web.loads_images_automatically = !cef.image_load_disabled; + web.plugins_enabled = !cef.plugins_disabled; + web.dom_paste_enabled = !cef.dom_paste_disabled; + web.developer_extras_enabled = false; + web.inspector_settings.clear(); + web.site_specific_quirks_enabled = !cef.site_specific_quirks_disabled; + web.shrinks_standalone_images_to_fit = cef.shrink_standalone_images_to_fit; + web.uses_universal_detector = cef.encoding_detector_enabled; + web.text_areas_are_resizable = !cef.text_area_resize_disabled; + web.java_enabled = !cef.java_disabled; + web.allow_scripts_to_close_windows = !cef.javascript_close_windows_disallowed; + web.uses_page_cache = !cef.page_cache_disabled; + web.remote_fonts_enabled = !cef.remote_fonts_disabled; + web.javascript_can_access_clipboard = + !cef.javascript_access_clipboard_disallowed; + web.xss_auditor_enabled = cef.xss_auditor_enabled; + web.local_storage_enabled = !cef.local_storage_disabled; + web.databases_enabled = !cef.databases_disabled; + web.application_cache_enabled = !cef.application_cache_disabled; + web.tabs_to_links = !cef.tab_to_links_disabled; + web.caret_browsing_enabled = cef.caret_browsing_enabled; + web.hyperlink_auditing_enabled = !cef.hyperlink_auditing_disabled; + + web.user_style_sheet_enabled = cef.user_style_sheet_enabled; + + if (cef.user_style_sheet_location) { + web.user_style_sheet_location = + GURL(WideToUTF8(cef.user_style_sheet_location)); + } + + web.author_and_user_styles_enabled = !cef.author_and_user_styles_disabled; + web.allow_universal_access_from_file_urls = + cef.universal_access_from_file_urls_allowed; + web.allow_file_access_from_file_urls = cef.file_access_from_file_urls_allowed; + web.experimental_webgl_enabled = cef.experimental_webgl_enabled; + web.show_composited_layer_borders = false; + web.accelerated_compositing_enabled = !cef.accelerated_compositing_disabled; + web.accelerated_2d_canvas_enabled = !cef.accelerated_2d_canvas_disabled; + web.memory_info_enabled = false; +} + +void WebToBrowserSettings(const WebPreferences& web, CefBrowserSettings& cef) +{ + cef.Reset(); + + if (!web.standard_font_family.empty()) { + cef.standard_font_family = + cef_string_alloc(web.standard_font_family.c_str()); + } + + if (!web.fixed_font_family.empty()) { + cef.fixed_font_family = + cef_string_alloc(web.fixed_font_family.c_str()); + } + + if (!web.serif_font_family.empty()) { + cef.serif_font_family = + cef_string_alloc(web.serif_font_family.c_str()); + } + + if (!web.cursive_font_family.empty()) { + cef.cursive_font_family = + cef_string_alloc(web.cursive_font_family.c_str()); + } + + if (!web.fantasy_font_family.empty()) { + cef.fantasy_font_family = + cef_string_alloc(web.fantasy_font_family.c_str()); + } + + cef.default_font_size = web.default_font_size; + cef.default_fixed_font_size = web.default_fixed_font_size; + cef.minimum_font_size = web.minimum_font_size; + cef.minimum_logical_font_size = web.minimum_logical_font_size; + cef.remote_fonts_disabled = !web.remote_fonts_enabled; + + if (!web.default_encoding.empty()) { + std::wstring wstr; + UTF8ToWide(web.default_encoding.c_str(), web.default_encoding.length(), + &wstr); + cef.default_encoding = cef_string_alloc(wstr.c_str()); + } + + cef.encoding_detector_enabled = web.uses_universal_detector; + cef.javascript_disabled = !web.java_enabled; + cef.javascript_open_windows_disallowed = + !web.javascript_can_open_windows_automatically; + cef.javascript_close_windows_disallowed = + !web.allow_scripts_to_close_windows; + cef.javascript_access_clipboard_disallowed = + !web.javascript_can_access_clipboard; + cef.dom_paste_disabled = !web.dom_paste_enabled; + cef.caret_browsing_enabled = web.caret_browsing_enabled; + cef.java_disabled = !web.java_enabled; + cef.plugins_disabled = !web.plugins_enabled; + cef.universal_access_from_file_urls_allowed = + web.allow_universal_access_from_file_urls; + cef.file_access_from_file_urls_allowed = web.allow_file_access_from_file_urls; + cef.web_security_disabled = !web.web_security_enabled; + cef.xss_auditor_enabled = web.xss_auditor_enabled; + cef.image_load_disabled = !web.loads_images_automatically; + cef.shrink_standalone_images_to_fit = web.shrinks_standalone_images_to_fit; + cef.site_specific_quirks_disabled = !web.site_specific_quirks_enabled; + cef.text_area_resize_disabled = !web.text_areas_are_resizable; + cef.page_cache_disabled = !web.uses_page_cache; + cef.tab_to_links_disabled = !web.tabs_to_links; + cef.hyperlink_auditing_disabled = !web.hyperlink_auditing_enabled; + cef.user_style_sheet_enabled = web.user_style_sheet_enabled; + + if (!web.user_style_sheet_location.is_empty()) { + std::string str = web.user_style_sheet_location.spec(); + std::wstring wstr; + UTF8ToWide(str.c_str(), str.length(), &wstr); + cef.user_style_sheet_location = cef_string_alloc(wstr.c_str()); + } + + cef.author_and_user_styles_disabled = !web.author_and_user_styles_enabled; + cef.local_storage_disabled = !web.local_storage_enabled; + cef.databases_disabled = !web.databases_enabled; + cef.application_cache_disabled = !web.application_cache_enabled; + cef.experimental_webgl_enabled = web.experimental_webgl_enabled; + cef.accelerated_compositing_disabled = !web.accelerated_compositing_enabled; + cef.accelerated_2d_canvas_disabled = !web.accelerated_2d_canvas_enabled; +} diff --git a/libcef/browser_settings.h b/libcef/browser_settings.h new file mode 100644 index 000000000..77b880d99 --- /dev/null +++ b/libcef/browser_settings.h @@ -0,0 +1,14 @@ +// Copyright (c) 2010 The Chromium Embedded Framework Authors. All rights +// reserved. Use of this source code is governed by a BSD-style license that can +// be found in the LICENSE file. + +#ifndef _CEF_BROWSER_SETTINGS_H +#define _CEF_BROWSER_SETTINGS_H + +class CefBrowserSettings; +struct WebPreferences; + +void BrowserToWebSettings(const CefBrowserSettings& cef, WebPreferences& web); +void WebToBrowserSettings(const WebPreferences& web, CefBrowserSettings& cef); + +#endif // _CEF_BROWSER_SETTINGS_H diff --git a/libcef/browser_webkit_glue.cc b/libcef/browser_webkit_glue.cc index c93d44a8a..89d4880f1 100644 --- a/libcef/browser_webkit_glue.cc +++ b/libcef/browser_webkit_glue.cc @@ -16,6 +16,8 @@ MSVC_POP_WARNING(); #include "browser_webkit_glue.h" #undef LOG +#include "cef_context.h" + #include "base/logging.h" #include "base/path_service.h" #include "base/scoped_ptr.h" @@ -70,6 +72,9 @@ bool IsProtocolSupportedForMedia(const GURL& url) { } std::string GetWebKitLocale() { + const CefSettings& settings = _Context->settings(); + if (settings.locale) + return WideToUTF8(settings.locale); return "en-US"; } @@ -119,7 +124,10 @@ std::wstring WebStringToStdWString(const WebKit::WebString& str) { } std::string GetProductVersion() { - return std::string("Chrome/7.0.517.0"); + const CefSettings& settings = _Context->settings(); + if (settings.product_version) + return WideToUTF8(settings.product_version); + return "Chrome/7.0.517.0"; } bool IsSingleProcess() { diff --git a/libcef/browser_webview_delegate.cc b/libcef/browser_webview_delegate.cc index 46379fe31..2bae3217f 100644 --- a/libcef/browser_webview_delegate.cc +++ b/libcef/browser_webview_delegate.cc @@ -161,23 +161,6 @@ void TranslatePopupFeatures(const WebWindowFeatures& webKitFeatures, } // namespace -// WebViewDelegate ----------------------------------------------------------- - -void BrowserWebViewDelegate::SetUserStyleSheetEnabled(bool is_enabled) { - WebPreferences* prefs = _Context->web_preferences(); - prefs->user_style_sheet_enabled = is_enabled; - if (browser_->GetWebView()) - prefs->Apply(browser_->GetWebView()); -} - -void BrowserWebViewDelegate::SetUserStyleSheetLocation(const GURL& location) { - WebPreferences* prefs = _Context->web_preferences(); - prefs->user_style_sheet_enabled = true; - prefs->user_style_sheet_location = location; - if (browser_->GetWebView()) - prefs->Apply(browser_->GetWebView()); -} - // WebViewClient ------------------------------------------------------------- WebView* BrowserWebViewDelegate::createView(WebFrame* creator, @@ -912,7 +895,8 @@ void BrowserWebViewDelegate::RegisterDragDrop() { void BrowserWebViewDelegate::RevokeDragDrop() { #if defined(OS_WIN) - ::RevokeDragDrop(browser_->GetWebViewWndHandle()); + if (drop_delegate_.get()) + ::RevokeDragDrop(browser_->GetWebViewWndHandle()); #endif } diff --git a/libcef/browser_webview_delegate.h b/libcef/browser_webview_delegate.h index 45a428f32..7812bb80b 100644 --- a/libcef/browser_webview_delegate.h +++ b/libcef/browser_webview_delegate.h @@ -39,7 +39,6 @@ #include "browser_navigation_controller.h" class CefBrowserImpl; -struct WebPreferences; class GURL; class WebWidgetHost; class FilePath; @@ -218,10 +217,6 @@ class BrowserWebViewDelegate : public WebKit::WebViewClient, pending_extra_data_.reset(extra_data); } - // Methods for modifying WebPreferences - void SetUserStyleSheetEnabled(bool is_enabled); - void SetUserStyleSheetLocation(const GURL& location); - // Sets the webview as a drop target. void RegisterDragDrop(); void RevokeDragDrop(); diff --git a/libcef/cef_context.cc b/libcef/cef_context.cc index 76193e008..8c7132e38 100644 --- a/libcef/cef_context.cc +++ b/libcef/cef_context.cc @@ -15,28 +15,28 @@ #endif #include "base/utf_string_conversions.h" #include "webkit/glue/plugins/plugin_list.h" -#include "webkit/glue/webpreferences.h" // Global CefContext pointer CefRefPtr _Context; -bool CefInitialize(bool multi_threaded_message_loop, - const std::wstring& cache_path) +bool CefInitialize(const CefSettings& settings, + const CefBrowserSettings& browser_defaults) { // Return true if the context is already initialized if(_Context.get()) return true; + if(settings.size != sizeof(cef_settings_t) || + browser_defaults.size != sizeof(cef_browser_settings_t)) { + NOTREACHED(); + return false; + } + // Create the new global context object _Context = new CefContext(); // Initialize the global context -#if defined(OS_WIN) - FilePath cachePath(cache_path); -#else - FilePath cachePath(WideToUTF8(_Context->cache_path())); -#endif - return _Context->Initialize(multi_threaded_message_loop, cachePath); + return _Context->Initialize(settings, browser_defaults); } void CefShutdown() @@ -176,7 +176,7 @@ bool CefPostDelayedTask(CefThreadId threadId, CefRefPtr task, // CefContext -CefContext::CefContext() : process_(NULL), webprefs_(NULL) +CefContext::CefContext() : process_(NULL) { } @@ -187,64 +187,27 @@ CefContext::~CefContext() Shutdown(); } -bool CefContext::Initialize(bool multi_threaded_message_loop, - const FilePath& cache_path) +bool CefContext::Initialize(const CefSettings& settings, + const CefBrowserSettings& browser_defaults) { - cache_path_ = cache_path; + settings_ = settings; + browser_defaults_ = browser_defaults; - // Initialize web preferences - webprefs_ = new WebPreferences; - *webprefs_ = WebPreferences(); - webprefs_->standard_font_family = L"Times"; - webprefs_->fixed_font_family = L"Courier"; - webprefs_->serif_font_family = L"Times"; - webprefs_->sans_serif_font_family = L"Helvetica"; - // These two fonts are picked from the intersection of - // Win XP font list and Vista font list : - // http://www.microsoft.com/typography/fonts/winxp.htm - // http://blogs.msdn.com/michkap/archive/2006/04/04/567881.aspx - // Some of them are installed only with CJK and complex script - // support enabled on Windows XP and are out of consideration here. - // (although we enabled both on our buildbots.) - // They (especially Impact for fantasy) are not typical cursive - // and fantasy fonts, but it should not matter for layout tests - // as long as they're available. -#if defined(OS_MACOSX) - webprefs_->cursive_font_family = L"Apple Chancery"; - webprefs_->fantasy_font_family = L"Papyrus"; + std::wstring cachePathStr; + if(settings.cache_path) + cachePathStr = settings.cache_path; +#if defined(OS_WIN) + cache_path_ = FilePath(cachePathStr); #else - webprefs_->cursive_font_family = L"Comic Sans MS"; - webprefs_->fantasy_font_family = L"Impact"; + cache_path_ = FilePath(WideToUTF8(cachePathStr)); #endif - webprefs_->default_encoding = "ISO-8859-1"; - webprefs_->default_font_size = 16; - webprefs_->default_fixed_font_size = 13; - webprefs_->minimum_font_size = 1; - webprefs_->minimum_logical_font_size = 9; - webprefs_->javascript_can_open_windows_automatically = true; - webprefs_->dom_paste_enabled = true; - webprefs_->developer_extras_enabled = true; - webprefs_->site_specific_quirks_enabled = true; - webprefs_->shrinks_standalone_images_to_fit = false; - webprefs_->uses_universal_detector = false; - webprefs_->text_areas_are_resizable = true; - webprefs_->java_enabled = true; - webprefs_->allow_scripts_to_close_windows = false; - webprefs_->xss_auditor_enabled = false; - webprefs_->remote_fonts_enabled = true; - webprefs_->local_storage_enabled = true; - webprefs_->application_cache_enabled = true; - webprefs_->databases_enabled = true; - webprefs_->allow_file_access_from_file_urls = true; - webprefs_->accelerated_2d_canvas_enabled = true; - webprefs_->accelerated_compositing_enabled = true; #if defined(OS_MACOSX) || defined(OS_WIN) // We want to be sure to init NSPR on the main thread. base::EnsureNSPRInit(); #endif - process_ = new CefProcess(multi_threaded_message_loop); + process_ = new CefProcess(settings_.multi_threaded_message_loop); process_->CreateChildThreads(); return true; diff --git a/libcef/cef_context.h b/libcef/cef_context.h index 09427a4b3..b7287408f 100644 --- a/libcef/cef_context.h +++ b/libcef/cef_context.h @@ -19,7 +19,6 @@ class BrowserRequestContext; class CefBrowserImpl; -struct WebPreferences; class CefContext : public CefThreadSafeBase { @@ -30,8 +29,8 @@ public: ~CefContext(); // These methods will be called on the main application thread. - bool Initialize(bool multi_threaded_message_loop, - const FilePath& cache_path); + bool Initialize(const CefSettings& settings, + const CefBrowserSettings& browser_defaults); void Shutdown(); scoped_refptr process() { return process_; } @@ -43,13 +42,11 @@ public: // Retrieve the path at which cache data will be stored on disk. If empty, // cache data will be stored in-memory. - const FilePath& cache_path() { return cache_path_; } + const FilePath& cache_path() const { return cache_path_; } - WebPreferences* web_preferences() - { - REQUIRE_UIT(); - return webprefs_; - } + const CefSettings& settings() const { return settings_; } + const CefBrowserSettings& browser_defaults() const + { return browser_defaults_; } // The BrowserRequestContext object is managed by CefProcessIOThread. void set_request_context(BrowserRequestContext* request_context) @@ -70,8 +67,9 @@ private: // asserts and possible memory leaks. base::AtExitManager at_exit_manager_; + CefSettings settings_; + CefBrowserSettings browser_defaults_; FilePath cache_path_; - WebPreferences* webprefs_; scoped_refptr request_context_; scoped_ptr storage_context_; diff --git a/libcef/cef_process_ui_thread.cc b/libcef/cef_process_ui_thread.cc index 7f4ace82f..3b42c454a 100644 --- a/libcef/cef_process_ui_thread.cc +++ b/libcef/cef_process_ui_thread.cc @@ -21,6 +21,7 @@ #endif #include "webkit/blob/blob_storage_controller.h" #include "webkit/blob/blob_url_request_job.h" +#include "webkit/glue/plugins/plugin_list.h" #include "webkit/extensions/v8/gc_extension.h" #include "net/url_request/url_request.h" @@ -117,10 +118,30 @@ void CefProcessUIThread::Init() { URLRequest::RegisterProtocolFactory("blob", &BlobURLRequestJobFactory); - if(!_Context->cache_path().empty()) { + if (!_Context->cache_path().empty()) { // Create the storage context object. _Context->set_storage_context(new DOMStorageContext()); } + + const CefSettings& settings = _Context->settings(); + + if (settings.user_agent) + webkit_glue::SetUserAgent(WideToUTF8(settings.user_agent)); + + if (settings.extra_plugin_paths) { + cef_string_t str; + FilePath path; + int size = cef_string_list_size(settings.extra_plugin_paths); + for(int i = 0; i < size; ++i) { + str = cef_string_list_value(settings.extra_plugin_paths, i); +#if defined(OS_WIN) + path = FilePath(str); +#else + path = FilePath(WideToUTF8(str)); +#endif + NPAPI::PluginList::Singleton()->AddExtraPluginPath(path); + } + } } void CefProcessUIThread::CleanUp() { diff --git a/libcef/cef_string_list.cc b/libcef/cef_string_list.cc index acd4c78fe..76051e65a 100644 --- a/libcef/cef_string_list.cc +++ b/libcef/cef_string_list.cc @@ -53,3 +53,10 @@ CEF_EXPORT void cef_string_list_free(cef_string_list_t list) DCHECK(list); delete (StringList*)list; } + +CEF_EXPORT cef_string_list_t cef_string_list_copy(cef_string_list_t list) +{ + DCHECK(list); + StringList* impl = (StringList*)list; + return new StringList(*impl); +} diff --git a/libcef_dll/cpptoc/handler_cpptoc.cc b/libcef_dll/cpptoc/handler_cpptoc.cc index 7ee5c0d10..06ec607fd 100644 --- a/libcef_dll/cpptoc/handler_cpptoc.cc +++ b/libcef_dll/cpptoc/handler_cpptoc.cc @@ -24,8 +24,10 @@ enum cef_retval_t CEF_CALLBACK handler_handle_before_created( struct _cef_handler_t* self, cef_browser_t* parentBrowser, - cef_window_info_t* windowInfo, int popup, struct _cef_handler_t** handler, - cef_string_t* url, const struct _cef_popup_features_t* popupFeatures) + cef_window_info_t* windowInfo, int popup, + const struct _cef_popup_features_t* popupFeatures, + struct _cef_handler_t** handler, cef_string_t* url, + struct _cef_browser_settings_t* settings) { DCHECK(self); DCHECK(windowInfo); @@ -34,8 +36,14 @@ enum cef_retval_t CEF_CALLBACK handler_handle_before_created( if(!self || !windowInfo || !handler || !*handler || !url) return RV_CONTINUE; - CefWindowInfo wndInfo(*windowInfo); - CefPopupFeatures features(*popupFeatures); + CefWindowInfo wndInfo; + CefBrowserSettings browserSettings; + CefPopupFeatures features; + + // Take ownership of the pointers instead of copying. + wndInfo.Attach(*windowInfo); + browserSettings.Attach(*settings); + features.Attach(*popupFeatures); // |newHandler| will start off pointing to the current handler. CefRefPtr handlerPtr = CefHandlerCppToC::Unwrap(*handler); @@ -51,7 +59,8 @@ enum cef_retval_t CEF_CALLBACK handler_handle_before_created( urlStr = *url; enum cef_retval_t rv = CefHandlerCppToC::Get(self)->HandleBeforeCreated( - browserPtr, wndInfo, popup?true:false, handlerPtr, urlStr, features); + browserPtr, wndInfo, popup?true:false, features, handlerPtr, urlStr, + browserSettings); transfer_string_contents(urlStr, url); @@ -60,13 +69,16 @@ enum cef_retval_t CEF_CALLBACK handler_handle_before_created( *handler = CefHandlerCppToC::Wrap(handlerPtr); } - // WindowInfo may or may not have changed. + // Window info may or may not have changed. *windowInfo = wndInfo; -#ifdef WIN32 - // The m_windowName must be duplicated since it's a cef_string_t - if(windowInfo->m_windowName) - windowInfo->m_windowName = cef_string_alloc(windowInfo->m_windowName); -#endif + + // Browser settings may or may not have changed. + *settings = browserSettings; + + // Don't free the pointers. + features.Detach(); + wndInfo.Detach(); + browserSettings.Detach(); return rv; } diff --git a/libcef_dll/ctocpp/handler_ctocpp.cc b/libcef_dll/ctocpp/handler_ctocpp.cc index ef4f5cea5..b64bcc498 100644 --- a/libcef_dll/ctocpp/handler_ctocpp.cc +++ b/libcef_dll/ctocpp/handler_ctocpp.cc @@ -24,8 +24,8 @@ CefHandler::RetVal CefHandlerCToCpp::HandleBeforeCreated( CefRefPtr parentBrowser, CefWindowInfo& windowInfo, bool popup, - CefRefPtr& handler, std::wstring& url, - const CefPopupFeatures& popupFeatures) + const CefPopupFeatures& popupFeatures, CefRefPtr& handler, + std::wstring& url, CefBrowserSettings& settings) { if(CEF_MEMBER_MISSING(struct_, handle_before_created)) return RV_CONTINUE; @@ -44,7 +44,8 @@ CefHandler::RetVal CefHandlerCToCpp::HandleBeforeCreated( urlRet = cef_string_alloc(url.c_str()); cef_retval_t rv = struct_->handle_before_created(struct_, - browserStruct, &windowInfo, popup, &handlerStruct, &urlRet, &popupFeatures); + browserStruct, &windowInfo, popup, &popupFeatures, &handlerStruct, + &urlRet, &settings); if(handlerStruct && handlerStruct != origHandlerStruct) { // The handler was changed. diff --git a/libcef_dll/ctocpp/handler_ctocpp.h b/libcef_dll/ctocpp/handler_ctocpp.h index c9c408c3e..ea409cbf4 100644 --- a/libcef_dll/ctocpp/handler_ctocpp.h +++ b/libcef_dll/ctocpp/handler_ctocpp.h @@ -32,8 +32,9 @@ public: // CefHandler methods virtual RetVal HandleBeforeCreated(CefRefPtr parentBrowser, - CefWindowInfo& windowInfo, bool popup, CefRefPtr& handler, - std::wstring& url, const CefPopupFeatures& popupFeatures); + CefWindowInfo& windowInfo, bool popup, + const CefPopupFeatures& popupFeatures, CefRefPtr& handler, + std::wstring& url, CefBrowserSettings& settings); virtual RetVal HandleAfterCreated(CefRefPtr browser); virtual RetVal HandleAddressChange(CefRefPtr browser, CefRefPtr frame, const std::wstring& url); diff --git a/libcef_dll/libcef_dll.cc b/libcef_dll/libcef_dll.cc index bda41fe7a..74e047e56 100644 --- a/libcef_dll/libcef_dll.cc +++ b/libcef_dll/libcef_dll.cc @@ -27,13 +27,25 @@ #include "base/string_split.h" -CEF_EXPORT int cef_initialize(int multi_threaded_message_loop, - const wchar_t* cache_path) +CEF_EXPORT int cef_initialize(const struct _cef_settings_t* settings, + const struct _cef_browser_settings_t* browser_defaults) { - std::wstring cachePath; - if(cache_path) - cachePath = cache_path; - return CefInitialize(multi_threaded_message_loop?true:false, cachePath); + CefSettings settingsObj; + CefBrowserSettings browserDefaultsObj; + + // Take ownership of the pointers instead of copying. + if (settings) + settingsObj.Attach(*settings); + if (browser_defaults) + browserDefaultsObj.Attach(*browser_defaults); + + int ret = CefInitialize(settingsObj, browserDefaultsObj); + + // Don't free the pointers. + settingsObj.Detach(); + browserDefaultsObj.Detach(); + + return ret; } CEF_EXPORT void cef_shutdown() diff --git a/libcef_dll/wrapper/libcef_dll_wrapper.cc b/libcef_dll/wrapper/libcef_dll_wrapper.cc index 6262f89aa..fbeeb4c47 100644 --- a/libcef_dll/wrapper/libcef_dll_wrapper.cc +++ b/libcef_dll/wrapper/libcef_dll_wrapper.cc @@ -25,11 +25,10 @@ #include "libcef_dll/ctocpp/zip_reader_ctocpp.h" -bool CefInitialize(bool multi_threaded_message_loop, - const std::wstring& cache_path) +bool CefInitialize(const CefSettings& settings, + const CefBrowserSettings& browser_defaults) { - return cef_initialize(multi_threaded_message_loop, cache_path.c_str()) - ?true:false; + return cef_initialize(&settings, &browser_defaults)?true:false; } void CefShutdown() diff --git a/tests/cefclient/cefclient.h b/tests/cefclient/cefclient.h index 45b21a10c..2d5307288 100644 --- a/tests/cefclient/cefclient.h +++ b/tests/cefclient/cefclient.h @@ -42,9 +42,10 @@ public: // window modify the object that |handler| points to. virtual RetVal HandleBeforeCreated(CefRefPtr parentBrowser, CefWindowInfo& createInfo, bool popup, + const CefPopupFeatures& popupFeatures, CefRefPtr& handler, std::wstring& url, - const CefPopupFeatures& popupFeatures) + CefBrowserSettings& settings) { return RV_CONTINUE; } diff --git a/tests/cefclient/cefclient_mac.mm b/tests/cefclient/cefclient_mac.mm index 2c978d02b..ba2f61b9b 100644 --- a/tests/cefclient/cefclient_mac.mm +++ b/tests/cefclient/cefclient_mac.mm @@ -348,7 +348,9 @@ int main(int argc, char* argv[]) getcwd(szWorkingDir, sizeof(szWorkingDir)); // Initialize CEF. This will also create the NSApplication instance. - CefInitialize(false, std::wstring()); + CefSettings settings; + CefBrowserSettings browserDefaults; + CefInitialize(settings, browserDefaults); // Initialize tests. InitExtensionTest(); diff --git a/tests/cefclient/cefclient_win.cpp b/tests/cefclient/cefclient_win.cpp index 51fe3458a..b6eea4512 100644 --- a/tests/cefclient/cefclient_win.cpp +++ b/tests/cefclient/cefclient_win.cpp @@ -62,15 +62,20 @@ int APIENTRY wWinMain(HINSTANCE hInstance, if(_wgetcwd(szWorkingDir, MAX_PATH) == NULL) szWorkingDir[0] = 0; + CefSettings settings; + CefBrowserSettings browserDefaults; + #ifdef TEST_SINGLE_THREADED_MESSAGE_LOOP // Initialize the CEF with messages processed using the current application's // message loop. - CefInitialize(false, std::wstring()); + settings.multi_threaded_message_loop = false; #else // Initialize the CEF with messages processed using a separate UI thread. - CefInitialize(true, std::wstring()); + settings.multi_threaded_message_loop = true; #endif + CefInitialize(settings, browserDefaults); + // Register the internal client plugin. InitPluginTest(); diff --git a/tests/unittests/test_handler.h b/tests/unittests/test_handler.h index 95c80c972..0e20cdc57 100644 --- a/tests/unittests/test_handler.h +++ b/tests/unittests/test_handler.h @@ -26,9 +26,10 @@ public: virtual RetVal HandleBeforeCreated(CefRefPtr parentBrowser, CefWindowInfo& createInfo, bool popup, + const CefPopupFeatures& popupFeatures, CefRefPtr& handler, std::wstring& url, - const CefPopupFeatures& popupFeatures) + CefBrowserSettings& settings) { return RV_CONTINUE; } diff --git a/tests/unittests/test_suite.h b/tests/unittests/test_suite.h index 3439d9d4d..1d6477a36 100644 --- a/tests/unittests/test_suite.h +++ b/tests/unittests/test_suite.h @@ -19,7 +19,11 @@ class CefTestSuite : public TestSuite { virtual void Initialize() { TestSuite::Initialize(); - CefInitialize(true, std::wstring()); + + CefSettings settings; + CefBrowserSettings browserDefaults; + settings.multi_threaded_message_loop = true; + CefInitialize(settings, browserDefaults); } virtual void Shutdown() { diff --git a/tools/cef_parser.py b/tools/cef_parser.py index bf6b52cad..63e4e1566 100644 --- a/tools/cef_parser.py +++ b/tools/cef_parser.py @@ -1057,7 +1057,9 @@ class obj_analysis: structuretypes = { 'CefPrintInfo' : 'cef_print_info_t', 'CefWindowInfo' : 'cef_window_info_t', - 'CefPopupFeatures' : 'cef_popup_features_t' + 'CefPopupFeatures' : 'cef_popup_features_t', + 'CefSettings' : 'cef_settings_t', + 'CefBrowserSettings' : 'cef_browser_settings_t', } if value in structuretypes.keys(): return {