refactor: natively promisify WebContents.prototype.takeHeapSnapshot (#18000)

This commit is contained in:
Milan Burda 2019-04-29 19:11:11 +02:00 committed by Jeremy Apthorp
parent 18b77a4de6
commit 7574f91f31
3 changed files with 22 additions and 26 deletions

View file

@ -2153,21 +2153,23 @@ void WebContents::GrantOriginAccess(const GURL& url) {
url::Origin::Create(url)); url::Origin::Create(url));
} }
void WebContents::TakeHeapSnapshot(const base::FilePath& file_path, v8::Local<v8::Promise> WebContents::TakeHeapSnapshot(
base::Callback<void(bool)> callback) { const base::FilePath& file_path) {
base::ThreadRestrictions::ScopedAllowIO allow_io; util::Promise promise(isolate());
v8::Local<v8::Promise> handle = promise.GetHandle();
base::ThreadRestrictions::ScopedAllowIO allow_io;
base::File file(file_path, base::File file(file_path,
base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
if (!file.IsValid()) { if (!file.IsValid()) {
std::move(callback).Run(false); promise.RejectWithErrorMessage("takeHeapSnapshot failed");
return; return handle;
} }
auto* frame_host = web_contents()->GetMainFrame(); auto* frame_host = web_contents()->GetMainFrame();
if (!frame_host) { if (!frame_host) {
std::move(callback).Run(false); promise.RejectWithErrorMessage("takeHeapSnapshot failed");
return; return handle;
} }
// This dance with `base::Owned` is to ensure that the interface stays alive // This dance with `base::Owned` is to ensure that the interface stays alive
@ -2179,10 +2181,17 @@ void WebContents::TakeHeapSnapshot(const base::FilePath& file_path,
auto* raw_ptr = electron_ptr.get(); auto* raw_ptr = electron_ptr.get();
(*raw_ptr)->TakeHeapSnapshot( (*raw_ptr)->TakeHeapSnapshot(
mojo::WrapPlatformFile(file.TakePlatformFile()), mojo::WrapPlatformFile(file.TakePlatformFile()),
base::BindOnce([](mojom::ElectronRendererAssociatedPtr* ep, base::BindOnce(
base::Callback<void(bool)> callback, [](mojom::ElectronRendererAssociatedPtr* ep, util::Promise promise,
bool success) { callback.Run(success); }, bool success) {
base::Owned(std::move(electron_ptr)), callback)); if (success) {
promise.Resolve();
} else {
promise.RejectWithErrorMessage("takeHeapSnapshot failed");
}
},
base::Owned(std::move(electron_ptr)), std::move(promise)));
return handle;
} }
// static // static
@ -2288,7 +2297,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("getWebRTCIPHandlingPolicy", .SetMethod("getWebRTCIPHandlingPolicy",
&WebContents::GetWebRTCIPHandlingPolicy) &WebContents::GetWebRTCIPHandlingPolicy)
.SetMethod("_grantOriginAccess", &WebContents::GrantOriginAccess) .SetMethod("_grantOriginAccess", &WebContents::GrantOriginAccess)
.SetMethod("_takeHeapSnapshot", &WebContents::TakeHeapSnapshot) .SetMethod("takeHeapSnapshot", &WebContents::TakeHeapSnapshot)
.SetProperty("id", &WebContents::ID) .SetProperty("id", &WebContents::ID)
.SetProperty("session", &WebContents::Session) .SetProperty("session", &WebContents::Session)
.SetProperty("hostWebContents", &WebContents::HostWebContents) .SetProperty("hostWebContents", &WebContents::HostWebContents)

View file

@ -294,8 +294,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
// the specified URL. // the specified URL.
void GrantOriginAccess(const GURL& url); void GrantOriginAccess(const GURL& url);
void TakeHeapSnapshot(const base::FilePath& file_path, v8::Local<v8::Promise> TakeHeapSnapshot(const base::FilePath& file_path);
base::Callback<void(bool)>);
// Properties. // Properties.
int32_t ID() const; int32_t ID() const;

View file

@ -226,18 +226,6 @@ WebContents.prototype.getZoomFactor = function (callback) {
} }
} }
WebContents.prototype.takeHeapSnapshot = function (filePath) {
return new Promise((resolve, reject) => {
this._takeHeapSnapshot(filePath, (success) => {
if (success) {
resolve()
} else {
reject(new Error('takeHeapSnapshot failed'))
}
})
})
}
// Translate the options of printToPDF. // Translate the options of printToPDF.
WebContents.prototype.printToPDF = function (options) { WebContents.prototype.printToPDF = function (options) {
const printingSetting = Object.assign({}, defaultPrintingSetting) const printingSetting = Object.assign({}, defaultPrintingSetting)