Merge pull request #4829 from atom/deprecated-browser-window-options
Report deprecated BrowserWindow options
This commit is contained in:
commit
e05804848f
7 changed files with 108 additions and 8 deletions
|
@ -52,6 +52,11 @@ namespace api {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
// This function is implemented in JavaScript
|
||||||
|
using DeprecatedOptionsCheckCallback =
|
||||||
|
base::Callback<std::string(v8::Local<v8::Value>)>;
|
||||||
|
DeprecatedOptionsCheckCallback g_deprecated_options_check;
|
||||||
|
|
||||||
void OnCapturePageDone(
|
void OnCapturePageDone(
|
||||||
v8::Isolate* isolate,
|
v8::Isolate* isolate,
|
||||||
const base::Callback<void(const gfx::Image&)>& callback,
|
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);
|
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);
|
return new Window(isolate, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -797,6 +809,10 @@ v8::Local<v8::Value> Window::From(v8::Isolate* isolate,
|
||||||
return v8::Null(isolate);
|
return v8::Null(isolate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetDeprecatedOptionsCheck(const DeprecatedOptionsCheckCallback& callback) {
|
||||||
|
g_deprecated_options_check = callback;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace api
|
} // namespace api
|
||||||
|
|
||||||
} // namespace atom
|
} // namespace atom
|
||||||
|
@ -819,6 +835,8 @@ void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
|
||||||
|
|
||||||
mate::Dictionary dict(isolate, exports);
|
mate::Dictionary dict(isolate, exports);
|
||||||
dict.Set("BrowserWindow", browser_window);
|
dict.Set("BrowserWindow", browser_window);
|
||||||
|
dict.SetMethod("_setDeprecatedOptionsCheck",
|
||||||
|
&atom::api::SetDeprecatedOptionsCheck);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
|
@ -3,12 +3,11 @@
|
||||||
const ipcMain = require('electron').ipcMain;
|
const ipcMain = require('electron').ipcMain;
|
||||||
const deprecate = require('electron').deprecate;
|
const deprecate = require('electron').deprecate;
|
||||||
const EventEmitter = require('events').EventEmitter;
|
const EventEmitter = require('events').EventEmitter;
|
||||||
const BrowserWindow = process.atomBinding('window').BrowserWindow;
|
const {BrowserWindow, _setDeprecatedOptionsCheck} = process.atomBinding('window');
|
||||||
|
|
||||||
BrowserWindow.prototype.__proto__ = EventEmitter.prototype;
|
BrowserWindow.prototype.__proto__ = EventEmitter.prototype;
|
||||||
|
|
||||||
BrowserWindow.prototype._init = function() {
|
BrowserWindow.prototype._init = function() {
|
||||||
|
|
||||||
// avoid recursive require.
|
// avoid recursive require.
|
||||||
var app, menu;
|
var app, menu;
|
||||||
app = require('electron').app;
|
app = require('electron').app;
|
||||||
|
@ -240,4 +239,56 @@ BrowserWindow.prototype.getPageTitle = deprecate('getPageTitle', 'webContents.ge
|
||||||
return (ref1 = this.webContents) != null ? ref1.getTitle() : void 0;
|
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;
|
module.exports = BrowserWindow;
|
||||||
|
|
|
@ -132,7 +132,7 @@ app.once('ready', function() {
|
||||||
return delete extensionInfoMap[name];
|
return delete extensionInfoMap[name];
|
||||||
};
|
};
|
||||||
|
|
||||||
// Load persistented extensions when devtools is opened.
|
// Load persisted extensions when devtools is opened.
|
||||||
init = BrowserWindow.prototype._init;
|
init = BrowserWindow.prototype._init;
|
||||||
return BrowserWindow.prototype._init = function() {
|
return BrowserWindow.prototype._init = function() {
|
||||||
init.call(this);
|
init.call(this);
|
||||||
|
|
|
@ -100,7 +100,10 @@ window.open = function(url, frameName, features) {
|
||||||
features = '';
|
features = '';
|
||||||
}
|
}
|
||||||
options = {};
|
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
|
// Make sure to get rid of excessive whitespace in the property name
|
||||||
ref1 = features.split(/,\s*/);
|
ref1 = features.split(/,\s*/);
|
||||||
|
@ -109,7 +112,15 @@ window.open = function(url, frameName, features) {
|
||||||
ref2 = feature.split(/\s*=/);
|
ref2 = feature.split(/\s*=/);
|
||||||
name = ref2[0];
|
name = ref2[0];
|
||||||
value = ref2[1];
|
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.left) {
|
||||||
if (options.x == null) {
|
if (options.x == null) {
|
||||||
|
|
|
@ -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.');
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -155,7 +155,7 @@ describe('chromium feature', function() {
|
||||||
b.close();
|
b.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('accepts "node-integration" as feature', function(done) {
|
it('accepts "nodeIntegration" as feature', function(done) {
|
||||||
var b;
|
var b;
|
||||||
listener = function(event) {
|
listener = function(event) {
|
||||||
assert.equal(event.data, 'undefined');
|
assert.equal(event.data, 'undefined');
|
||||||
|
|
|
@ -82,8 +82,8 @@ app.on('ready', function() {
|
||||||
show: false,
|
show: false,
|
||||||
width: 800,
|
width: 800,
|
||||||
height: 600,
|
height: 600,
|
||||||
'web-preferences': {
|
webPreferences: {
|
||||||
javascript: true // Test whether web-preferences crashes.
|
javascript: true // Test whether web preferences crashes.
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
window.loadURL(url.format({
|
window.loadURL(url.format({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue