refactor: native_mate => gin (cookies API) (#18036)

* convert cookie converters to gin

* event_emitter GetWrapper
This commit is contained in:
Jeremy Apthorp 2019-04-30 06:45:05 -07:00 committed by John Kleinschmidt
parent e9d88e965e
commit fdf5f838f4
5 changed files with 36 additions and 21 deletions

View file

@ -18,8 +18,8 @@
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "gin/dictionary.h"
#include "gin/object_template_builder.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_store.h"
#include "net/cookies/cookie_util.h"
@ -28,7 +28,7 @@
using content::BrowserThread;
namespace mate {
namespace gin {
template <>
struct Converter<atom::api::Cookies::Error> {
@ -45,7 +45,7 @@ template <>
struct Converter<net::CanonicalCookie> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const net::CanonicalCookie& val) {
mate::Dictionary dict(isolate, v8::Object::New(isolate));
gin::Dictionary dict(isolate, v8::Object::New(isolate));
dict.Set("name", val.Name());
dict.Set("value", val.Value());
dict.Set("domain", val.Domain());
@ -56,7 +56,7 @@ struct Converter<net::CanonicalCookie> {
dict.Set("session", !val.IsPersistent());
if (val.IsPersistent())
dict.Set("expirationDate", val.ExpiryDate().ToDoubleT());
return dict.GetHandle();
return ConvertToV8(isolate, dict).As<v8::Object>();
}
};
@ -68,22 +68,22 @@ struct Converter<network::mojom::CookieChangeCause> {
switch (val) {
case network::mojom::CookieChangeCause::INSERTED:
case network::mojom::CookieChangeCause::EXPLICIT:
return mate::StringToV8(isolate, "explicit");
return gin::StringToV8(isolate, "explicit");
case network::mojom::CookieChangeCause::OVERWRITE:
return mate::StringToV8(isolate, "overwrite");
return gin::StringToV8(isolate, "overwrite");
case network::mojom::CookieChangeCause::EXPIRED:
return mate::StringToV8(isolate, "expired");
return gin::StringToV8(isolate, "expired");
case network::mojom::CookieChangeCause::EVICTED:
return mate::StringToV8(isolate, "evicted");
return gin::StringToV8(isolate, "evicted");
case network::mojom::CookieChangeCause::EXPIRED_OVERWRITE:
return mate::StringToV8(isolate, "expired-overwrite");
return gin::StringToV8(isolate, "expired-overwrite");
default:
return mate::StringToV8(isolate, "unknown");
return gin::StringToV8(isolate, "unknown");
}
}
};
} // namespace mate
} // namespace gin
namespace atom {
@ -143,7 +143,7 @@ void FilterCookies(const base::Value& filter,
result.push_back(cookie);
}
promise.Resolve(result);
promise.Resolve(gin::ConvertToV8(promise.isolate(), result));
}
std::string InclusionStatusToString(
@ -330,19 +330,21 @@ v8::Local<v8::Promise> Cookies::FlushStore() {
}
void Cookies::OnCookieChanged(const CookieDetails* details) {
Emit("changed", *(details->cookie), details->cause, details->removed);
Emit("changed", gin::ConvertToV8(isolate(), *(details->cookie)),
gin::ConvertToV8(isolate(), details->cause),
gin::ConvertToV8(isolate(), details->removed));
}
// static
mate::Handle<Cookies> Cookies::Create(v8::Isolate* isolate,
gin::Handle<Cookies> Cookies::Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context) {
return mate::CreateHandle(isolate, new Cookies(isolate, browser_context));
return gin::CreateHandle(isolate, new Cookies(isolate, browser_context));
}
// static
void Cookies::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Cookies"));
prototype->SetClassName(gin::StringToV8(isolate, "Cookies"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("get", &Cookies::Get)
.SetMethod("remove", &Cookies::Remove)

View file

@ -12,7 +12,7 @@
#include "atom/browser/net/cookie_details.h"
#include "atom/common/promise_util.h"
#include "base/callback_list.h"
#include "native_mate/handle.h"
#include "gin/handle.h"
#include "net/cookies/canonical_cookie.h"
namespace base {
@ -36,7 +36,7 @@ class Cookies : public mate::TrackableObject<Cookies> {
FAILED,
};
static mate::Handle<Cookies> Create(v8::Isolate* isolate,
static gin::Handle<Cookies> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context);
// mate::TrackableObject:

View file

@ -46,6 +46,9 @@ class EventEmitter : public Wrappable<T> {
v8::Local<v8::Object> GetWrapper() const {
return Wrappable<T>::GetWrapper();
}
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate) const {
return Wrappable<T>::GetWrapper(isolate);
}
// this.emit(name, event, args...);
template <typename... Args>

View file

@ -28,6 +28,15 @@ v8::Local<v8::Object> WrappableBase::GetWrapper() const {
return v8::Local<v8::Object>();
}
v8::MaybeLocal<v8::Object> WrappableBase::GetWrapper(
v8::Isolate* isolate) const {
if (!wrapper_.IsEmpty())
return v8::MaybeLocal<v8::Object>(
v8::Local<v8::Object>::New(isolate, wrapper_));
else
return v8::MaybeLocal<v8::Object>();
}
void WrappableBase::InitWith(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper) {
CHECK(wrapper_.IsEmpty());

View file

@ -30,6 +30,7 @@ class WrappableBase {
// Retrieve the v8 wrapper object cooresponding to this object.
v8::Local<v8::Object> GetWrapper() const;
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate) const;
// Returns the Isolate this object is created in.
v8::Isolate* isolate() const { return isolate_; }