Skip to content

Commit 663cde0

Browse files
committed
Fix nwjs#7592: nw2 Window.capturePage captures wrong window
1 parent 2ff01ab commit 663cde0

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

src/api/nw_current_window_internal.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace nw.currentWindowInternal {
3434
static void leaveKioskModeInternal(optional long id);
3535
static void toggleKioskModeInternal(optional long id);
3636
static bool isKioskInternal(optional long id);
37-
static void capturePageInternal(optional CapturePageOptions options, optional CapturePageCallback callback);
37+
static void capturePageInternal(long id, optional CapturePageOptions options, optional CapturePageCallback callback);
3838
static void clearMenu(optional long win);
3939
static MenuPatch[] setMenu(long id, optional long winId);
4040
static void reloadIgnoringCache();

src/api/nw_window_api.cc

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,14 +320,26 @@ NwCurrentWindowInternalCapturePageInternalFunction::Run() {
320320
EXTENSION_FUNCTION_VALIDATE(args_);
321321

322322
std::unique_ptr<ImageDetails> image_details;
323-
if (args_->GetSize() > 0) {
323+
WebContents* contents = nullptr;
324+
325+
if (args_->GetSize() > 1) {
324326
base::Value* spec = NULL;
325-
EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &spec) && spec);
327+
EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec);
326328
image_details = ImageDetails::FromValue(*spec);
327329
}
328330

329-
content::RenderFrameHost* rfh = render_frame_host();
330-
WebContents* contents = content::WebContents::FromRenderFrameHost(rfh);
331+
if (base::FeatureList::IsEnabled(::features::kNWNewWin)) {
332+
int id = 0;
333+
args_->GetInteger(0, &id);
334+
Browser* browser = getBrowser(this, id);
335+
if (!browser) {
336+
return RespondNow(Error("no browser window found"));
337+
}
338+
contents = browser->tab_strip_model()->GetActiveWebContents();
339+
} else {
340+
content::RenderFrameHost* rfh = render_frame_host();
341+
contents = content::WebContents::FromRenderFrameHost(rfh);
342+
}
331343
if (!contents)
332344
return RespondNow(Error("contents is null"));
333345

src/resources/api_nw_newwin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ NWWindow.prototype.showDevTools = function(frm, callback) {
363363
nwNatives.setDevToolsJail(f);
364364
currentNWWindowInternal.showDevTools2Internal(this.cWindow.id, callback);
365365
};
366+
366367
NWWindow.prototype.capturePage = function (callback, options) {
367368
var cb = callback;
368369
if (!options)
@@ -383,7 +384,8 @@ NWWindow.prototype.capturePage = function (callback, options) {
383384
};
384385
cb = cb.bind(undefined, options.datatype);
385386
}
386-
currentNWWindowInternal.capturePageInternal(options, cb);
387+
this.cWindow = currentNWWindowInternal.getCurrent(this.cWindow.id, {'populate': true});
388+
currentNWWindowInternal.capturePageInternal(this.cWindow.id, options, cb);
387389
};
388390

389391
function sendCommand(tabId, name, options) {

src/resources/api_nw_window.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ apiBridge.registerCustomHook(function(bindingsAPI) {
403403
};
404404
cb = cb.bind(undefined, options.datatype);
405405
}
406-
currentNWWindowInternal.capturePageInternal(options, cb);
406+
currentNWWindowInternal.capturePageInternal(0, options, cb);
407407
};
408408
NWWindow.prototype.reload = function () {
409409
this.appWindow.contentWindow.location.reload();

0 commit comments

Comments
 (0)