Merge pull request #4829 from atom/deprecated-browser-window-options

Report deprecated BrowserWindow options
This commit is contained in:
Kevin Sawicki 2016-03-18 09:36:49 -07:00
commit e05804848f
7 changed files with 108 additions and 8 deletions

View file

@ -52,6 +52,11 @@ namespace api {
namespace {
// This function is implemented in JavaScript
using DeprecatedOptionsCheckCallback =
base::Callback<std::string(v8::Local<v8::Value>)>;
DeprecatedOptionsCheckCallback g_deprecated_options_check;
void OnCapturePageDone(
v8::Isolate* isolate,
const base::Callback<void(const gfx::Image&)>& 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<v8::Value> Window::From(v8::Isolate* isolate,
return v8::Null(isolate);
}
void SetDeprecatedOptionsCheck(const DeprecatedOptionsCheckCallback& callback) {
g_deprecated_options_check = callback;
}
} // namespace api
} // namespace atom
@ -819,6 +835,8 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
mate::Dictionary dict(isolate, exports);
dict.Set("BrowserWindow", browser_window);
dict.SetMethod("_setDeprecatedOptionsCheck",
&atom::api::SetDeprecatedOptionsCheck);
}
} // namespace

View file

@ -3,12 +3,11 @@
const ipcMain = require('electron').ipcMain;
const deprecate = require('electron').deprecate;
const EventEmitter = require('events').EventEmitter;
const BrowserWindow = process.atomBinding('window').BrowserWindow;
const {BrowserWindow, _setDeprecatedOptionsCheck} = process.atomBinding('window');
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 hyphens 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.warn(`options.${webPreferenceOption}`, `options.webPreferences.${webPreferenceOption}`);
} catch (error) {
// Return error message so it can be rethrown via C++
return error.message;
}
}
return '';
};
_setDeprecatedOptionsCheck(checkForDeprecatedOptions);
module.exports = BrowserWindow;

View file

@ -132,7 +132,7 @@ app.once('ready', function() {
return delete extensionInfoMap[name];
};
// Load persistented extensions when devtools is opened.
// Load persisted extensions when devtools is opened.
init = BrowserWindow.prototype._init;
return BrowserWindow.prototype._init = function() {
init.call(this);

View file

@ -100,7 +100,10 @@ window.open = function(url, frameName, features) {
features = '';
}
options = {};
ints = ['x', 'y', 'width', 'height', 'min-width', 'max-width', 'min-height', 'max-height', 'zoom-factor'];
// TODO remove hyphenated options in both of the following arrays for 1.0
ints = ['x', 'y', 'width', 'height', 'min-width', 'max-width', 'min-height', 'minHeight', 'max-height', 'maxHeight', 'zoom-factor', 'zoomFactor'];
const webPreferences = ['zoom-factor', 'zoomFactor', 'node-integration', 'nodeIntegration', 'preload'];
// Make sure to get rid of excessive whitespace in the property name
ref1 = features.split(/,\s*/);
@ -109,7 +112,15 @@ window.open = function(url, frameName, features) {
ref2 = feature.split(/\s*=/);
name = ref2[0];
value = ref2[1];
options[name] = value === 'yes' || value === '1' ? true : value === 'no' || value === '0' ? false : value;
value = value === 'yes' || value === '1' ? true : value === 'no' || value === '0' ? false : value;
if (webPreferences.includes(name)) {
if (options.webPreferences == null) {
options.webPreferences = {};
}
options.webPreferences[name] = value;
} else {
options[name] = value;
}
}
if (options.left) {
if (options.x == null) {

View file

@ -759,4 +759,24 @@ describe('browser-window module', function() {
});
});
});
describe('deprecated options', function() {
it('throws a deprecation error for option keys using hyphens instead of camel case', function() {
assert.throws(function () {
new BrowserWindow({'min-width': 500});
}, 'min-width is deprecated. Use minWidth instead.');
});
it('throws a deprecation error for webPreference keys using hyphens instead of camel case', function() {
assert.throws(function () {
new BrowserWindow({webPreferences: {'node-integration': false}});
}, 'node-integration is deprecated. Use nodeIntegration instead.');
});
it('throws a deprecation error for option keys that should be set on webPreferences', function() {
assert.throws(function () {
new BrowserWindow({zoomFactor: 1});
}, 'options.zoomFactor is deprecated. Use options.webPreferences.zoomFactor instead.');
});
});
});

View file

@ -155,7 +155,7 @@ describe('chromium feature', function() {
b.close();
});
it('accepts "node-integration" as feature', function(done) {
it('accepts "nodeIntegration" as feature', function(done) {
var b;
listener = function(event) {
assert.equal(event.data, 'undefined');

View file

@ -82,8 +82,8 @@ app.on('ready', function() {
show: false,
width: 800,
height: 600,
'web-preferences': {
javascript: true // Test whether web-preferences crashes.
webPreferences: {
javascript: true // Test whether web preferences crashes.
},
});
window.loadURL(url.format({