From 15397bf879b26e56df4d28453a30f04ea915b984 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Wed, 16 Mar 2016 09:38:03 -0700 Subject: [PATCH] Report deprecated BrowserWindow options --- atom/browser/api/atom_api_window.cc | 19 +++++++++++++ lib/browser/api/browser-window.js | 53 ++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 6c0f0f6..87f6411 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -52,6 +52,11 @@ namespace api { namespace { +// The DeprecatedOptionsCheckCallback funtion which is implemented in JavaScript +using DeprecatedOptionsCheckCallback = + base::Callback)>; +DeprecatedOptionsCheckCallback g_deprecated_options_check; + void OnCapturePageDone( v8::Isolate* isolate, const base::Callback& callback, @@ -291,6 +296,13 @@ mate::Wrappable* Window::New(v8::Isolate* isolate, mate::Arguments* args) { options = mate::Dictionary::CreateEmpty(isolate); } + std::string deprecation_message = g_deprecated_options_check.Run( + options.GetHandle()); + if (deprecation_message.length() > 0) { + args->ThrowError(deprecation_message); + return nullptr; + } + return new Window(isolate, options); } @@ -797,6 +809,10 @@ v8::Local Window::From(v8::Isolate* isolate, return v8::Null(isolate); } +void SetDeprecatedOptionsCheck(const DeprecatedOptionsCheckCallback& callback) { + g_deprecated_options_check = callback; +} + } // namespace api } // namespace atom @@ -816,6 +832,9 @@ void Initialize(v8::Local exports, v8::Local unused, &mate::TrackableObject::FromWeakMapID); browser_window.SetMethod("getAllWindows", &mate::TrackableObject::GetAll); + browser_window.SetMethod("_setDeprecatedOptionsCheck", + &atom::api::SetDeprecatedOptionsCheck); + mate::Dictionary dict(isolate, exports); dict.Set("BrowserWindow", browser_window); diff --git a/lib/browser/api/browser-window.js b/lib/browser/api/browser-window.js index 7e2a8d3..9645f85 100644 --- a/lib/browser/api/browser-window.js +++ b/lib/browser/api/browser-window.js @@ -8,7 +8,6 @@ const BrowserWindow = process.atomBinding('window').BrowserWindow; BrowserWindow.prototype.__proto__ = EventEmitter.prototype; BrowserWindow.prototype._init = function() { - // avoid recursive require. var app, menu; app = require('electron').app; @@ -240,4 +239,56 @@ BrowserWindow.prototype.getPageTitle = deprecate('getPageTitle', 'webContents.ge return (ref1 = this.webContents) != null ? ref1.getTitle() : void 0; }); +const isDeprecatedKey = function(key) { + return key.indexOf('-') >= 0; +}; + +// Map deprecated key with hyphens to camel case key +const getNonDeprecatedKey = function(deprecatedKey) { + return deprecatedKey.replace(/-./g, function(match) { + return match[1].toUpperCase(); + }); +}; + +// TODO Remove for 1.0 +const checkForDeprecatedOptions = function(options) { + if (!options) return ''; + + let keysToCheck = Object.keys(options); + if (options.webPreferences) { + keysToCheck = keysToCheck.concat(Object.keys(options.webPreferences)); + } + + // Check options for keys with hypens in them + let deprecatedKey = keysToCheck.filter(isDeprecatedKey)[0]; + if (deprecatedKey) { + try { + deprecate.warn(deprecatedKey, getNonDeprecatedKey(deprecatedKey)); + } catch (error) { + // Return error message so it can be rethrown via C++ + return error.message; + } + } + + let webPreferenceOption; + if (options.hasOwnProperty('nodeIntegration')) { + webPreferenceOption = 'nodeIntegration'; + } else if (options.hasOwnProperty('preload')) { + webPreferenceOption = 'preload'; + } else if (options.hasOwnProperty('zoomFactor')) { + webPreferenceOption = 'zoomFactor'; + } + if (webPreferenceOption) { + try { + deprecate.log(`Setting ${webPreferenceOption} on options is deprecated. Set it on options.webPreferences instead.`); + } catch (error) { + // Return error message so it can be rethrown via C++ + return error.message; + } + } + + return ''; +}; +BrowserWindow._setDeprecatedOptionsCheck(checkForDeprecatedOptions); + module.exports = BrowserWindow; -- 2.7.4