Skip to content

Commit

Permalink
Fix #7592: nw2 Window.capturePage captures wrong window
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerwang committed Oct 15, 2020
1 parent 2ff01ab commit 663cde0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/api/nw_current_window_internal.idl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace nw.currentWindowInternal {
static void leaveKioskModeInternal(optional long id);
static void toggleKioskModeInternal(optional long id);
static bool isKioskInternal(optional long id);
static void capturePageInternal(optional CapturePageOptions options, optional CapturePageCallback callback);
static void capturePageInternal(long id, optional CapturePageOptions options, optional CapturePageCallback callback);
static void clearMenu(optional long win);
static MenuPatch[] setMenu(long id, optional long winId);
static void reloadIgnoringCache();
Expand Down
20 changes: 16 additions & 4 deletions src/api/nw_window_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,26 @@ NwCurrentWindowInternalCapturePageInternalFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(args_);

std::unique_ptr<ImageDetails> image_details;
if (args_->GetSize() > 0) {
WebContents* contents = nullptr;

if (args_->GetSize() > 1) {
base::Value* spec = NULL;
EXTENSION_FUNCTION_VALIDATE(args_->Get(0, &spec) && spec);
EXTENSION_FUNCTION_VALIDATE(args_->Get(1, &spec) && spec);
image_details = ImageDetails::FromValue(*spec);
}

content::RenderFrameHost* rfh = render_frame_host();
WebContents* contents = content::WebContents::FromRenderFrameHost(rfh);
if (base::FeatureList::IsEnabled(::features::kNWNewWin)) {
int id = 0;
args_->GetInteger(0, &id);
Browser* browser = getBrowser(this, id);
if (!browser) {
return RespondNow(Error("no browser window found"));
}
contents = browser->tab_strip_model()->GetActiveWebContents();
} else {
content::RenderFrameHost* rfh = render_frame_host();
contents = content::WebContents::FromRenderFrameHost(rfh);
}
if (!contents)
return RespondNow(Error("contents is null"));

Expand Down
4 changes: 3 additions & 1 deletion src/resources/api_nw_newwin.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ NWWindow.prototype.showDevTools = function(frm, callback) {
nwNatives.setDevToolsJail(f);
currentNWWindowInternal.showDevTools2Internal(this.cWindow.id, callback);
};

NWWindow.prototype.capturePage = function (callback, options) {
var cb = callback;
if (!options)
Expand All @@ -383,7 +384,8 @@ NWWindow.prototype.capturePage = function (callback, options) {
};
cb = cb.bind(undefined, options.datatype);
}
currentNWWindowInternal.capturePageInternal(options, cb);
this.cWindow = currentNWWindowInternal.getCurrent(this.cWindow.id, {'populate': true});
currentNWWindowInternal.capturePageInternal(this.cWindow.id, options, cb);
};

function sendCommand(tabId, name, options) {
Expand Down
2 changes: 1 addition & 1 deletion src/resources/api_nw_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ apiBridge.registerCustomHook(function(bindingsAPI) {
};
cb = cb.bind(undefined, options.datatype);
}
currentNWWindowInternal.capturePageInternal(options, cb);
currentNWWindowInternal.capturePageInternal(0, options, cb);
};
NWWindow.prototype.reload = function () {
this.appWindow.contentWindow.location.reload();
Expand Down

0 comments on commit 663cde0

Please sign in to comment.