Remove calls to v8::Isolate::GetCurrent

It is generally a bad thing to do since we might have multiple Isolates.
This commit is contained in:
Cheng Zhao 2015-06-23 17:22:14 +08:00
parent 1f97cee7c9
commit 197a9b4165
7 changed files with 38 additions and 46 deletions

View file

@ -46,18 +46,17 @@ bool GetCookieListFromStore(
return true; return true;
} }
void RunGetCookiesCallbackOnUIThread(const std::string& error_message, void RunGetCookiesCallbackOnUIThread(v8::Isolate* isolate,
const std::string& error_message,
const net::CookieList& cookie_list, const net::CookieList& cookie_list,
const Cookies::CookiesCallback& callback) { const Cookies::CookiesCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate); v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
if (!error_message.empty()) { if (!error_message.empty()) {
v8::Local<v8::String> error = v8::String::NewFromUtf8(isolate, v8::Local<v8::Value> error = mate::ConvertToV8(isolate, error_message);
error_message.c_str());
callback.Run(error, v8::Null(isolate)); callback.Run(error, v8::Null(isolate));
return; return;
} }
@ -65,17 +64,16 @@ void RunGetCookiesCallbackOnUIThread(const std::string& error_message,
} }
void RunRemoveCookiesCallbackOnUIThread( void RunRemoveCookiesCallbackOnUIThread(
v8::Isolate* isolate,
const std::string& error_message, const std::string& error_message,
const Cookies::CookiesCallback& callback) { const Cookies::CookiesCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate); v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
if (!error_message.empty()) { if (!error_message.empty()) {
v8::Local<v8::String> error = v8::String::NewFromUtf8(isolate, v8::Local<v8::Value> error = mate::ConvertToV8(isolate, error_message);
error_message.c_str());
callback.Run(error, v8::Null(isolate)); callback.Run(error, v8::Null(isolate));
return; return;
} }
@ -83,24 +81,23 @@ void RunRemoveCookiesCallbackOnUIThread(
callback.Run(v8::Null(isolate), v8::Null(isolate)); callback.Run(v8::Null(isolate), v8::Null(isolate));
} }
void RunSetCookiesCallbackOnUIThread(const std::string& error_message, void RunSetCookiesCallbackOnUIThread(v8::Isolate* isolate,
const std::string& error_message,
bool set_success, bool set_success,
const Cookies::CookiesCallback& callback) { const Cookies::CookiesCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate); v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
if (!error_message.empty()) { if (!error_message.empty()) {
v8::Local<v8::String> error = v8::String::NewFromUtf8(isolate, v8::Local<v8::Value> error = mate::ConvertToV8(isolate, error_message);
error_message.c_str());
callback.Run(error, v8::Null(isolate)); callback.Run(error, v8::Null(isolate));
return; return;
} }
if (!set_success) { if (!set_success) {
v8::Local<v8::String> error = v8::String::NewFromUtf8(isolate, v8::Local<v8::Value> error = mate::ConvertToV8(
"Failed to set cookies"); isolate, "Failed to set cookies");
callback.Run(error, v8::Null(isolate)); callback.Run(error, v8::Null(isolate));
} }
@ -209,8 +206,8 @@ void Cookies::GetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> filter,
base::Bind(&Cookies::OnGetCookies, base::Unretained(this), base::Bind(&Cookies::OnGetCookies, base::Unretained(this),
Passed(&filter), callback))) { Passed(&filter), callback))) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&RunGetCookiesCallbackOnUIThread, "Url is not valid", base::Bind(&RunGetCookiesCallbackOnUIThread, isolate(),
net::CookieList(), callback)); "Url is not valid", net::CookieList(), callback));
} }
} }
@ -223,7 +220,7 @@ void Cookies::OnGetCookies(scoped_ptr<base::DictionaryValue> filter,
result.push_back(cookie); result.push_back(cookie);
} }
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
&RunGetCookiesCallbackOnUIThread, "", result, callback)); &RunGetCookiesCallbackOnUIThread, isolate(), "", result, callback));
} }
void Cookies::Remove(const mate::Dictionary& details, void Cookies::Remove(const mate::Dictionary& details,
@ -238,7 +235,7 @@ void Cookies::Remove(const mate::Dictionary& details,
error_message = "Url is not valid."; error_message = "Url is not valid.";
} }
if (!error_message.empty()) { if (!error_message.empty()) {
RunRemoveCookiesCallbackOnUIThread(error_message, callback); RunRemoveCookiesCallbackOnUIThread(isolate(), error_message, callback);
return; return;
} }
content::BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, content::BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
@ -256,7 +253,7 @@ void Cookies::RemoveCookiesOnIOThread(const GURL& url, const std::string& name,
void Cookies::OnRemoveCookies(const CookiesCallback& callback) { void Cookies::OnRemoveCookies(const CookiesCallback& callback) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&RunRemoveCookiesCallbackOnUIThread, "", callback)); base::Bind(&RunRemoveCookiesCallbackOnUIThread, isolate(), "", callback));
} }
void Cookies::Set(const base::DictionaryValue& options, void Cookies::Set(const base::DictionaryValue& options,
@ -273,7 +270,7 @@ void Cookies::Set(const base::DictionaryValue& options,
} }
if (!error_message.empty()) { if (!error_message.empty()) {
RunSetCookiesCallbackOnUIThread(error_message, false, callback); RunSetCookiesCallbackOnUIThread(isolate(), error_message, false, callback);
return; return;
} }
@ -328,7 +325,8 @@ void Cookies::SetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> details,
void Cookies::OnSetCookies(const CookiesCallback& callback, void Cookies::OnSetCookies(const CookiesCallback& callback,
bool set_success) { bool set_success) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&RunSetCookiesCallbackOnUIThread, "", set_success, callback)); base::Bind(&RunSetCookiesCallbackOnUIThread, isolate(), "", set_success,
callback));
} }
mate::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder( mate::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder(

View file

@ -55,11 +55,10 @@ bool Menu::IsCommandIdVisible(int command_id) const {
bool Menu::GetAcceleratorForCommandId(int command_id, bool Menu::GetAcceleratorForCommandId(int command_id,
ui::Accelerator* accelerator) { ui::Accelerator* accelerator) {
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Locker locker(isolate());
v8::Locker locker(isolate); v8::HandleScope handle_scope(isolate());
v8::HandleScope handle_scope(isolate);
v8::Local<v8::Value> val = get_accelerator_.Run(command_id); v8::Local<v8::Value> val = get_accelerator_.Run(command_id);
return mate::ConvertFromV8(isolate, val, accelerator); return mate::ConvertFromV8(isolate(), val, accelerator);
} }
void Menu::ExecuteCommand(int command_id, int event_flags) { void Menu::ExecuteCommand(int command_id, int event_flags) {

View file

@ -67,9 +67,8 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
void GetJobTypeInUI() override { void GetJobTypeInUI() override {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Locker locker(registry_->isolate());
v8::Locker locker(isolate); v8::HandleScope handle_scope(registry_->isolate());
v8::HandleScope handle_scope(isolate);
// Call the JS handler. // Call the JS handler.
Protocol::JsProtocolHandler callback = Protocol::JsProtocolHandler callback =
@ -85,7 +84,7 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
return; return;
} else if (result->IsObject()) { } else if (result->IsObject()) {
v8::Local<v8::Object> obj = result->ToObject(); v8::Local<v8::Object> obj = result->ToObject();
mate::Dictionary dict(isolate, obj); mate::Dictionary dict(registry_->isolate(), obj);
std::string name = mate::V8ToString(obj->GetConstructorName()); std::string name = mate::V8ToString(obj->GetConstructorName());
if (name == "RequestStringJob") { if (name == "RequestStringJob") {
std::string mime_type, charset, data; std::string mime_type, charset, data;

View file

@ -310,9 +310,8 @@ void WebContents::DidStopLoading() {
void WebContents::DidGetResourceResponseStart( void WebContents::DidGetResourceResponseStart(
const content::ResourceRequestDetails& details) { const content::ResourceRequestDetails& details) {
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Locker locker(isolate());
v8::Locker locker(isolate); v8::HandleScope handle_scope(isolate());
v8::HandleScope handle_scope(isolate);
base::DictionaryValue response_headers; base::DictionaryValue response_headers;
net::HttpResponseHeaders* headers = details.headers.get(); net::HttpResponseHeaders* headers = details.headers.get();

View file

@ -142,20 +142,18 @@ void Window::OnDevToolsFocus() {
void Window::OnDevToolsOpened() { void Window::OnDevToolsOpened() {
Emit("devtools-opened"); Emit("devtools-opened");
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Locker locker(isolate());
v8::Locker locker(isolate); v8::HandleScope handle_scope(isolate());
v8::HandleScope handle_scope(isolate); auto handle = WebContents::CreateFrom(isolate(),
auto handle = window_->GetDevToolsWebContents());
WebContents::CreateFrom(isolate, window_->GetDevToolsWebContents()); devtools_web_contents_.Reset(isolate(), handle.ToV8());
devtools_web_contents_.Reset(isolate, handle.ToV8());
} }
void Window::OnDevToolsClosed() { void Window::OnDevToolsClosed() {
Emit("devtools-closed"); Emit("devtools-closed");
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Locker locker(isolate());
v8::Locker locker(isolate); v8::HandleScope handle_scope(isolate());
v8::HandleScope handle_scope(isolate);
devtools_web_contents_.Reset(); devtools_web_contents_.Reset();
} }

View file

@ -39,12 +39,11 @@ class EventEmitter : public Wrappable {
content::WebContents* sender, content::WebContents* sender,
IPC::Message* message, IPC::Message* message,
const Args&... args) { const Args&... args) {
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Locker locker(isolate());
v8::Locker locker(isolate); v8::HandleScope handle_scope(isolate());
v8::HandleScope handle_scope(isolate);
ValueArray converted = { ConvertToV8(isolate, args)... }; ValueArray converted = { ConvertToV8(isolate(), args)... };
return CallEmit(isolate, name, sender, message, converted); return CallEmit(isolate(), name, sender, message, converted);
} }
private: private:

2
vendor/native_mate vendored

@ -1 +1 @@
Subproject commit ad207eeabb0185f74c017e70ca3411d522627ff0 Subproject commit cad1fa50a95ca4185c435846e4868d5bd6cc94df