refactor: migrates util::Promise to gin (#20871)

* refactor: use gin in Promise

* refactor: separate Promise impl that returns nothing

* refactor: use Promise<void> for promise that returns nothing

* fix: methods should be able to run on both browser and renderer process

* fix: should not pass base::StringPiece across threads

* refactor: no more need to use different ResolvePromise for empty Promise

* refactor: move Promise to gin_helper
This commit is contained in:
Cheng Zhao 2019-11-01 15:10:32 +09:00 committed by GitHub
parent bff113760a
commit eaf2c61bef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
48 changed files with 483 additions and 479 deletions

View file

@ -144,7 +144,7 @@ void PrintPreviewMessageHandler::OnPrintPreviewCancelled(
void PrintPreviewMessageHandler::PrintToPDF(
base::DictionaryValue options,
electron::util::Promise<v8::Local<v8::Value>> promise) {
gin_helper::Promise<v8::Local<v8::Value>> promise) {
int request_id;
options.GetInteger(printing::kPreviewRequestID, &request_id);
promise_map_.emplace(request_id, std::move(promise));
@ -156,12 +156,12 @@ void PrintPreviewMessageHandler::PrintToPDF(
rfh->Send(new PrintMsg_PrintPreview(rfh->GetRoutingID(), options));
}
util::Promise<v8::Local<v8::Value>> PrintPreviewMessageHandler::GetPromise(
int request_id) {
gin_helper::Promise<v8::Local<v8::Value>>
PrintPreviewMessageHandler::GetPromise(int request_id) {
auto it = promise_map_.find(request_id);
DCHECK(it != promise_map_.end());
util::Promise<v8::Local<v8::Value>> promise = std::move(it->second);
gin_helper::Promise<v8::Local<v8::Value>> promise = std::move(it->second);
promise_map_.erase(it);
return promise;
@ -172,7 +172,7 @@ void PrintPreviewMessageHandler::ResolvePromise(
scoped_refptr<base::RefCountedMemory> data_bytes) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
util::Promise<v8::Local<v8::Value>> promise = GetPromise(request_id);
gin_helper::Promise<v8::Local<v8::Value>> promise = GetPromise(request_id);
v8::Isolate* isolate = promise.isolate();
gin_helper::Locker locker(isolate);
@ -192,7 +192,7 @@ void PrintPreviewMessageHandler::ResolvePromise(
void PrintPreviewMessageHandler::RejectPromise(int request_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
util::Promise<v8::Local<v8::Value>> promise = GetPromise(request_id);
gin_helper::Promise<v8::Local<v8::Value>> promise = GetPromise(request_id);
promise.RejectWithErrorMessage("Failed to generate PDF");
}

View file

@ -12,7 +12,7 @@
#include "components/services/pdf_compositor/public/mojom/pdf_compositor.mojom.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"
#include "shell/common/promise_util.h"
#include "shell/common/gin_helper/promise.h"
#include "v8/include/v8.h"
struct PrintHostMsg_DidPreviewDocument_Params;
@ -32,7 +32,7 @@ class PrintPreviewMessageHandler
~PrintPreviewMessageHandler() override;
void PrintToPDF(base::DictionaryValue options,
util::Promise<v8::Local<v8::Value>> promise);
gin_helper::Promise<v8::Local<v8::Value>> promise);
protected:
// content::WebContentsObserver implementation.
@ -56,14 +56,13 @@ class PrintPreviewMessageHandler
void OnPrintPreviewCancelled(int document_cookie,
const PrintHostMsg_PreviewIds& ids);
util::Promise<v8::Local<v8::Value>> GetPromise(int request_id);
gin_helper::Promise<v8::Local<v8::Value>> GetPromise(int request_id);
void ResolvePromise(int request_id,
scoped_refptr<base::RefCountedMemory> data_bytes);
void RejectPromise(int request_id);
using PromiseMap =
std::map<int, electron::util::Promise<v8::Local<v8::Value>>>;
using PromiseMap = std::map<int, gin_helper::Promise<v8::Local<v8::Value>>>;
PromiseMap promise_map_;
base::WeakPtrFactory<PrintPreviewMessageHandler> weak_ptr_factory_;