Correctly set initial visibilityState

This commit is contained in:
Cheng Zhao 2016-04-13 22:56:11 +09:00
parent 07a4c52919
commit 43c44da50b
8 changed files with 37 additions and 17 deletions

View file

@ -1127,6 +1127,12 @@ bool WebContents::IsGuest() const {
return type_ == WEB_VIEW;
}
void WebContents::MergeWebPreferences(const base::DictionaryValue& extend) {
WebContentsPreferences* web_preferences =
WebContentsPreferences::FromWebContents(web_contents());
web_preferences->Merge(extend);
}
v8::Local<v8::Value> WebContents::GetWebPreferences(v8::Isolate* isolate) {
WebContentsPreferences* web_preferences =
WebContentsPreferences::FromWebContents(web_contents());
@ -1221,6 +1227,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("endFrameSubscription", &WebContents::EndFrameSubscription)
.SetMethod("setSize", &WebContents::SetSize)
.SetMethod("isGuest", &WebContents::IsGuest)
.SetMethod("mergeWebPreferences", &WebContents::MergeWebPreferences)
.SetMethod("getWebPreferences", &WebContents::GetWebPreferences)
.SetMethod("getOwnerBrowserWindow", &WebContents::GetOwnerBrowserWindow)
.SetMethod("hasServiceWorker", &WebContents::HasServiceWorker)

View file

@ -144,6 +144,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
WindowOpenDisposition disposition);
// Returns the web preferences of current WebContents.
void MergeWebPreferences(const base::DictionaryValue& extend);
v8::Local<v8::Value> GetWebPreferences(v8::Isolate* isolate);
// Returns the owner window.

View file

@ -145,6 +145,11 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
if (web_preferences.GetString(options::kBlinkFeatures, &blink_features))
command_line->AppendSwitchASCII(::switches::kEnableBlinkFeatures,
blink_features);
// The initial visibility state.
std::string visibility;
if (web_preferences.GetString(options::kVisibilityState, &visibility))
command_line->AppendSwitchASCII(switches::kVisibilityState, visibility);
}
// static

View file

@ -106,6 +106,9 @@ const char kOpenerID[] = "openerId";
// Enable blink features.
const char kBlinkFeatures[] = "blinkFeatures";
// The initiali visibility state.
const char kVisibilityState[] = "visibilityState";
} // namespace options
namespace switches {
@ -149,6 +152,7 @@ const char kPreloadURL[] = "preload-url";
const char kNodeIntegration[] = "node-integration";
const char kGuestInstanceID[] = "guest-instance-id";
const char kOpenerID[] = "opener-id";
const char kVisibilityState[] = "visibility-state";
// Widevine options
// Path to Widevine CDM binaries.

View file

@ -56,6 +56,7 @@ extern const char kGuestInstanceID[];
extern const char kExperimentalFeatures[];
extern const char kExperimentalCanvasFeatures[];
extern const char kOpenerID[];
extern const char kVisibilityState[];
extern const char kBlinkFeatures[];
} // namespace options
@ -83,6 +84,7 @@ extern const char kPreloadURL[];
extern const char kNodeIntegration[];
extern const char kGuestInstanceID[];
extern const char kOpenerID[];
extern const char kVisibilityState[];
extern const char kWidevineCdmPath[];
extern const char kWidevineCdmVersion[];

View file

@ -79,7 +79,7 @@ BrowserWindow.prototype._init = function () {
})
// Subscribe to visibilityState changes and pass to renderer process.
let isVisible = false
let isVisible = this.isVisible() && !this.isMinimized()
let visibilityChanged = () => {
let newState = this.isVisible() && !this.isMinimized()
if (isVisible != newState) {
@ -94,7 +94,8 @@ BrowserWindow.prototype._init = function () {
this.on('maximize', visibilityChanged)
// Make renderer process have correct initial state.
visibilityChanged()
if (!isVisible)
this.webContents.mergeWebPreferences({visibilityState: 'hidden'})
// Notify the creation of the window.
app.emit('browser-window-created', {}, this)

View file

@ -47,11 +47,7 @@ electron.ipcRenderer.on('ELECTRON_INTERNAL_RENDERER_ASYNC_WEB_FRAME_METHOD', (ev
// Process command line arguments.
var nodeIntegration = 'false'
var preloadScript = null
var ref = process.argv
var i, len, arg
for (i = 0, len = ref.length; i < len; i++) {
arg = ref[i]
for (let arg of process.argv) {
if (arg.indexOf('--guest-instance-id=') === 0) {
// This is a guest web view.
process.guestInstanceId = parseInt(arg.substr(arg.indexOf('=') + 1))
@ -62,6 +58,8 @@ for (i = 0, len = ref.length; i < len; i++) {
nodeIntegration = arg.substr(arg.indexOf('=') + 1)
} else if (arg.indexOf('--preload=') === 0) {
preloadScript = arg.substr(arg.indexOf('=') + 1)
} else if (arg.indexOf('--visibility-state=') === 0) {
process.visibilityState = arg.substr(arg.indexOf('=') + 1)
}
}

View file

@ -5,7 +5,6 @@ const remote = require('electron').remote
// Helper function to resolve relative url.
var a = window.top.document.createElement('a')
var resolveURL = function (url) {
a.href = url
return a.href
@ -160,8 +159,6 @@ window.alert = function (message, title) {
title: title,
buttons: buttons
})
// Alert should always return undefined.
}
// And the confirm().
@ -227,8 +224,13 @@ Object.defineProperty(window.history, 'length', {
}
})
// The initial visibilityState.
let cachedVisibilityState = 'visible'
if (process.visibilityState) {
cachedVisibilityState = process.visibilityState
}
// Subscribe to visibilityState changes.
let cachedVisibilityState = 'hidden'
ipcRenderer.on('ELECTRON_RENDERER_WINDOW_VISIBILITY_CHANGE', function (event, visibilityState) {
if (cachedVisibilityState != visibilityState) {
cachedVisibilityState = visibilityState