Use webContents BrowserContext.

Also fix some code styles.
This commit is contained in:
Haojian Wu 2015-06-18 18:38:32 +08:00
parent 99bfc9b7f5
commit 969916442f
6 changed files with 78 additions and 66 deletions

View file

@ -4,11 +4,13 @@
#include "atom/browser/api/atom_api_cookies.h" #include "atom/browser/api/atom_api_cookies.h"
#include "atom/browser/atom_browser_context.h" #include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/value_converter.h" #include "atom/common/native_mate_converters/value_converter.h"
#include "base/bind.h" #include "base/bind.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "content/public/browser/web_contents.h"
#include "native_mate/callback.h" #include "native_mate/callback.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h" #include "native_mate/object_template_builder.h"
@ -16,6 +18,7 @@
#include "net/cookies/cookie_store.h" #include "net/cookies/cookie_store.h"
#include "net/cookies/cookie_util.h" #include "net/cookies/cookie_util.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "atom/common/node_includes.h" #include "atom/common/node_includes.h"
@ -42,14 +45,11 @@ bool GetCookieListFromStore(net::CookieStore* cookie_store,
return true; return true;
} }
void RunGetCookiesCallbackOnUIThread(const base::DictionaryValue* filter, void RunGetCookiesCallbackOnUIThread(const std::string& error_message,
const std::string& error_message, const net::CookieList& cookie_list, const net::CookieList& cookie_list,
const atom::api::Cookies::CookiesCallback& callback) { const atom::api::Cookies::CookiesCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
// Should release filter here.
delete filter;
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate); v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
@ -60,8 +60,7 @@ void RunGetCookiesCallbackOnUIThread(const base::DictionaryValue* filter,
callback.Run(error, v8::Null(isolate)); callback.Run(error, v8::Null(isolate));
return; return;
} }
callback.Run(v8::Null(isolate), callback.Run(v8::Null(isolate), mate::ConvertToV8(isolate, cookie_list));
mate::Converter<net::CookieList>::ToV8(isolate, cookie_list));
} }
void RunRemoveCookiesCallbackOnUIThread(const std::string& error_message, void RunRemoveCookiesCallbackOnUIThread(const std::string& error_message,
@ -82,13 +81,10 @@ void RunRemoveCookiesCallbackOnUIThread(const std::string& error_message,
callback.Run(v8::Null(isolate), v8::Null(isolate)); callback.Run(v8::Null(isolate), v8::Null(isolate));
} }
void RunSetCookiesCallbackOnUIThread(const base::DictionaryValue* details, void RunSetCookiesCallbackOnUIThread(const std::string& error_message,
const std::string& error_message, bool set_success, bool set_success, const atom::api::Cookies::CookiesCallback& callback) {
const atom::api::Cookies::CookiesCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI); DCHECK_CURRENTLY_ON(BrowserThread::UI);
delete details;
v8::Isolate* isolate = v8::Isolate::GetCurrent(); v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Locker locker(isolate); v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate); v8::HandleScope handle_scope(isolate);
@ -101,7 +97,7 @@ void RunSetCookiesCallbackOnUIThread(const base::DictionaryValue* details,
} }
if (!set_success) { if (!set_success) {
v8::Local<v8::String> error = v8::String::NewFromUtf8(isolate, v8::Local<v8::String> error = v8::String::NewFromUtf8(isolate,
"Failed to set cookies"); "Failed to set cookies");
callback.Run(error, v8::Null(isolate)); callback.Run(error, v8::Null(isolate));
} }
@ -109,7 +105,7 @@ void RunSetCookiesCallbackOnUIThread(const base::DictionaryValue* details,
} }
bool MatchesDomain(const base::DictionaryValue* filter, bool MatchesDomain(const base::DictionaryValue* filter,
const std::string& cookie_domain) { const std::string& cookie_domain) {
std::string filter_domain; std::string filter_domain;
if (!filter->GetString("domain", &filter_domain)) if (!filter->GetString("domain", &filter_domain))
return true; return true;
@ -183,7 +179,8 @@ namespace atom {
namespace api { namespace api {
Cookies::Cookies() { Cookies::Cookies(content::WebContents* web_contents):
web_contents_(web_contents) {
} }
Cookies::~Cookies() { Cookies::~Cookies() {
@ -191,51 +188,51 @@ Cookies::~Cookies() {
void Cookies::Get(const base::DictionaryValue& options, void Cookies::Get(const base::DictionaryValue& options,
const CookiesCallback& callback) { const CookiesCallback& callback) {
// The filter will be deleted manually after callback function finishes. scoped_ptr<base::DictionaryValue> filter(
base::DictionaryValue* filter = options.DeepCopyWithoutEmptyChildren(); options.DeepCopyWithoutEmptyChildren());
content::BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, content::BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&Cookies::GetCookiesOnIOThread, base::Unretained(this), base::Bind(&Cookies::GetCookiesOnIOThread, base::Unretained(this),
filter, callback)); Passed(&filter), callback));
} }
void Cookies::GetCookiesOnIOThread(const base::DictionaryValue* filter, void Cookies::GetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> filter,
const CookiesCallback& callback) { const CookiesCallback& callback) {
net::CookieStore* cookie_store = net::CookieStore* cookie_store =
AtomBrowserContext::Get()->url_request_context_getter() web_contents_->GetBrowserContext()->GetRequestContext()
->GetURLRequestContext()->cookie_store(); ->GetURLRequestContext()->cookie_store();
std::string url; std::string url;
filter->GetString("url", &url); filter->GetString("url", &url);
if (!GetCookieListFromStore(cookie_store, url, if (!GetCookieListFromStore(cookie_store, url,
base::Bind(&Cookies::OnGetCookies, base::Unretained(this), filter, base::Bind(&Cookies::OnGetCookies, base::Unretained(this),
callback))) { Passed(&filter), callback))) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&RunGetCookiesCallbackOnUIThread, filter, "Url is not valid", base::Bind(&RunGetCookiesCallbackOnUIThread, "Url is not valid",
net::CookieList(), callback)); net::CookieList(), callback));
} }
} }
void Cookies::OnGetCookies(const base::DictionaryValue* filter, void Cookies::OnGetCookies(scoped_ptr<base::DictionaryValue> filter,
const CookiesCallback& callback, const CookiesCallback& callback,
const net::CookieList& cookie_list) { const net::CookieList& cookie_list) {
net::CookieList result; net::CookieList result;
for (const auto& cookie : cookie_list) { for (const auto& cookie : cookie_list) {
if (MatchesCookie(filter, cookie)) if (MatchesCookie(filter.get(), cookie))
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, filter, "", result, callback)); &RunGetCookiesCallbackOnUIThread, "", result, callback));
} }
void Cookies::Remove(const base::DictionaryValue& details, void Cookies::Remove(const mate::Dictionary& details,
const CookiesCallback& callback) { const CookiesCallback& callback) {
std::string url, name; GURL url;
std::string name;
std::string error_message; std::string error_message;
if (!details.GetString("url", &url) || !details.GetString("name", &name)) { if (!details.Get("url", &url) || !details.Get("name", &name)) {
error_message = "Details(url, name) of removing cookie are required."; error_message = "Details(url, name) of removing cookie are required.";
} }
GURL gurl(url); if (error_message.empty() && !url.is_valid()) {
if (error_message.empty() && !gurl.is_valid()) {
error_message = "Url is not valid."; error_message = "Url is not valid.";
} }
if (!error_message.empty()) { if (!error_message.empty()) {
@ -244,13 +241,13 @@ void Cookies::Remove(const base::DictionaryValue& details,
} }
content::BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, content::BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&Cookies::RemoveCookiesOnIOThread, base::Unretained(this), base::Bind(&Cookies::RemoveCookiesOnIOThread, base::Unretained(this),
gurl, name, callback)); url, name, callback));
} }
void Cookies::RemoveCookiesOnIOThread(const GURL& url, const std::string& name, void Cookies::RemoveCookiesOnIOThread(const GURL& url, const std::string& name,
const CookiesCallback& callback) { const CookiesCallback& callback) {
net::CookieStore* cookie_store = net::CookieStore* cookie_store =
AtomBrowserContext::Get()->url_request_context_getter() web_contents_->GetBrowserContext()->GetRequestContext()
->GetURLRequestContext()->cookie_store(); ->GetURLRequestContext()->cookie_store();
cookie_store->DeleteCookieAsync(url, name, cookie_store->DeleteCookieAsync(url, name,
base::Bind(&Cookies::OnRemoveCookies, base::Unretained(this), callback)); base::Bind(&Cookies::OnRemoveCookies, base::Unretained(this), callback));
@ -275,24 +272,24 @@ void Cookies::Set(const base::DictionaryValue& options,
} }
if (!error_message.empty()) { if (!error_message.empty()) {
RunSetCookiesCallbackOnUIThread(nullptr, error_message, false, callback); RunSetCookiesCallbackOnUIThread(error_message, false, callback);
return; return;
} }
// The filter will be deleted manually after callback function finishes. scoped_ptr<base::DictionaryValue> details(
base::DictionaryValue* details = options.DeepCopyWithoutEmptyChildren(); options.DeepCopyWithoutEmptyChildren());
content::BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, content::BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&Cookies::SetCookiesOnIOThread, base::Unretained(this), base::Bind(&Cookies::SetCookiesOnIOThread, base::Unretained(this),
details, gurl, callback)); Passed(&details), gurl, callback));
} }
void Cookies::SetCookiesOnIOThread(const base::DictionaryValue* details, void Cookies::SetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> details,
const GURL& url, const GURL& url,
const CookiesCallback& callback) { const CookiesCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
net::CookieStore* cookie_store = net::CookieStore* cookie_store =
AtomBrowserContext::Get()->url_request_context_getter() web_contents_->GetBrowserContext()->GetRequestContext()
->GetURLRequestContext()->cookie_store(); ->GetURLRequestContext()->cookie_store();
std::string name, value, domain, path; std::string name, value, domain, path;
@ -325,16 +322,13 @@ void Cookies::SetCookiesOnIOThread(const base::DictionaryValue* details,
http_only, http_only,
false, false,
net::COOKIE_PRIORITY_DEFAULT, net::COOKIE_PRIORITY_DEFAULT,
base::Bind(&Cookies::OnSetCookies, base::Unretained(this), details, base::Bind(&Cookies::OnSetCookies, base::Unretained(this), callback));
callback));
} }
void Cookies::OnSetCookies(const base::DictionaryValue* details, void Cookies::OnSetCookies(const CookiesCallback& callback,
const CookiesCallback& callback,
bool set_success) { bool set_success) {
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&RunSetCookiesCallbackOnUIThread, details, "", set_success, base::Bind(&RunSetCookiesCallbackOnUIThread, "", set_success, callback));
callback));
} }
mate::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder( mate::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder(
@ -346,8 +340,9 @@ mate::ObjectTemplateBuilder Cookies::GetObjectTemplateBuilder(
} }
// static // static
mate::Handle<Cookies> Cookies::Create(v8::Isolate* isolate) { mate::Handle<Cookies> Cookies::Create(v8::Isolate* isolate,
return CreateHandle(isolate, new Cookies); content::WebContents* web_contents) {
return CreateHandle(isolate, new Cookies(web_contents));
} }
} // namespace api } // namespace api

View file

@ -15,6 +15,10 @@
#include "net/cookies/canonical_cookie.h" #include "net/cookies/canonical_cookie.h"
namespace content {
class WebContents;
}
namespace atom { namespace atom {
namespace api { namespace api {
@ -25,22 +29,23 @@ class Cookies : public mate::Wrappable {
typedef base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)> typedef base::Callback<void(v8::Local<v8::Value>, v8::Local<v8::Value>)>
CookiesCallback; CookiesCallback;
static mate::Handle<Cookies> Create(v8::Isolate* isolate); static mate::Handle<Cookies> Create(v8::Isolate* isolate,
content::WebContents* web_contents);
protected: protected:
Cookies(); explicit Cookies(content::WebContents* web_contents);
~Cookies(); ~Cookies();
void Get(const base::DictionaryValue& options, void Get(const base::DictionaryValue& options,
const CookiesCallback& callback); const CookiesCallback& callback);
void Remove(const base::DictionaryValue& details, void Remove(const mate::Dictionary& details,
const CookiesCallback& callback); const CookiesCallback& callback);
void Set(const base::DictionaryValue& details, void Set(const base::DictionaryValue& details,
const CookiesCallback& callback); const CookiesCallback& callback);
void GetCookiesOnIOThread(const base::DictionaryValue* filter, void GetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> filter,
const CookiesCallback& callback); const CookiesCallback& callback);
void OnGetCookies(const base::DictionaryValue* filter, void OnGetCookies(scoped_ptr<base::DictionaryValue> filter,
const CookiesCallback& callback, const CookiesCallback& callback,
const net::CookieList& cookie_list); const net::CookieList& cookie_list);
@ -49,11 +54,10 @@ class Cookies : public mate::Wrappable {
const CookiesCallback& callback); const CookiesCallback& callback);
void OnRemoveCookies(const CookiesCallback& callback); void OnRemoveCookies(const CookiesCallback& callback);
void SetCookiesOnIOThread(const base::DictionaryValue* details, void SetCookiesOnIOThread(scoped_ptr<base::DictionaryValue> details,
const GURL& url, const GURL& url,
const CookiesCallback& callback); const CookiesCallback& callback);
void OnSetCookies(const base::DictionaryValue* details, void OnSetCookies(const CookiesCallback& callback,
const CookiesCallback& callback,
bool set_success); bool set_success);
@ -62,6 +66,8 @@ class Cookies : public mate::Wrappable {
v8::Isolate* isolate) override; v8::Isolate* isolate) override;
private: private:
content::WebContents* web_contents_;
DISALLOW_COPY_AND_ASSIGN(Cookies); DISALLOW_COPY_AND_ASSIGN(Cookies);
}; };

View file

@ -5,6 +5,7 @@
#include "atom/browser/api/atom_api_session.h" #include "atom/browser/api/atom_api_session.h"
#include "atom/browser/api/atom_api_cookies.h" #include "atom/browser/api/atom_api_cookies.h"
#include "content/public/browser/web_contents.h"
#include "native_mate/callback.h" #include "native_mate/callback.h"
#include "native_mate/dictionary.h" #include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h" #include "native_mate/object_template_builder.h"
@ -15,7 +16,8 @@ namespace atom {
namespace api { namespace api {
Session::Session() { Session::Session(content::WebContents* web_contents):
web_contents_(web_contents) {
} }
Session::~Session() { Session::~Session() {
@ -23,7 +25,7 @@ Session::~Session() {
v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) { v8::Local<v8::Value> Session::Cookies(v8::Isolate* isolate) {
if (cookies_.IsEmpty()) { if (cookies_.IsEmpty()) {
auto handle = atom::api::Cookies::Create(isolate); auto handle = atom::api::Cookies::Create(isolate, web_contents_);
cookies_.Reset(isolate, handle.ToV8()); cookies_.Reset(isolate, handle.ToV8());
} }
return v8::Local<v8::Value>::New(isolate, cookies_); return v8::Local<v8::Value>::New(isolate, cookies_);
@ -36,8 +38,9 @@ mate::ObjectTemplateBuilder Session::GetObjectTemplateBuilder(
} }
// static // static
mate::Handle<Session> Session::Create(v8::Isolate* isolate) { mate::Handle<Session> Session::Create(v8::Isolate* isolate,
return CreateHandle(isolate, new Session); content::WebContents* web_contents) {
return CreateHandle(isolate, new Session(web_contents));
} }
} // namespace api } // namespace api

View file

@ -8,16 +8,21 @@
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "native_mate/wrappable.h" #include "native_mate/wrappable.h"
namespace content {
class WebContents;
}
namespace atom { namespace atom {
namespace api { namespace api {
class Session: public mate::Wrappable { class Session: public mate::Wrappable {
public: public:
static mate::Handle<Session> Create(v8::Isolate* isolate); static mate::Handle<Session> Create(v8::Isolate* isolate,
content::WebContents* web_contents);
protected: protected:
Session(); explicit Session(content::WebContents* web_contents);
~Session(); ~Session();
// mate::Wrappable implementations: // mate::Wrappable implementations:
@ -29,6 +34,9 @@ class Session: public mate::Wrappable {
v8::Global<v8::Value> cookies_; v8::Global<v8::Value> cookies_;
// The webContents which owns the Sesssion.
content::WebContents* web_contents_;
DISALLOW_COPY_AND_ASSIGN(Session); DISALLOW_COPY_AND_ASSIGN(Session);
}; };

View file

@ -587,7 +587,7 @@ void WebContents::InspectServiceWorker() {
v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) { v8::Local<v8::Value> WebContents::Session(v8::Isolate* isolate) {
if (session_.IsEmpty()) { if (session_.IsEmpty()) {
auto handle = Session::Create(isolate); auto handle = Session::Create(isolate, web_contents());
session_.Reset(isolate, handle.ToV8()); session_.Reset(isolate, handle.ToV8());
} }
return v8::Local<v8::Value>::New(isolate, session_); return v8::Local<v8::Value>::New(isolate, session_);

View file

@ -89,6 +89,7 @@ class WebContents : public mate::EventEmitter,
void ToggleDevTools(); void ToggleDevTools();
void InspectElement(int x, int y); void InspectElement(int x, int y);
void InspectServiceWorker(); void InspectServiceWorker();
v8::Local<v8::Value> Session(v8::Isolate* isolate);
void HasServiceWorker(const base::Callback<void(bool)>&); void HasServiceWorker(const base::Callback<void(bool)>&);
void UnregisterServiceWorker(const base::Callback<void(bool)>&); void UnregisterServiceWorker(const base::Callback<void(bool)>&);
void SetAudioMuted(bool muted); void SetAudioMuted(bool muted);
@ -239,7 +240,6 @@ class WebContents : public mate::EventEmitter,
// Returns the default size of the guestview. // Returns the default size of the guestview.
gfx::Size GetDefaultSize() const; gfx::Size GetDefaultSize() const;
v8::Local<v8::Value> Session(v8::Isolate* isolate);
v8::Global<v8::Value> session_; v8::Global<v8::Value> session_;