Skip to content

Commit

Permalink
Cookies API on webview.
Browse files Browse the repository at this point in the history
Basically duplicate of chrome.cookies API via following API function on Webview:
'cookiesGet'
'cookiesGetAll'
'cookiesSet'
'cookiesRemove'

CookieStore or storeId currently not supported.
  • Loading branch information
janRucka committed Dec 13, 2016
1 parent 655eaea commit dbc007e
Show file tree
Hide file tree
Showing 8 changed files with 677 additions and 48 deletions.
103 changes: 64 additions & 39 deletions chrome/browser/extensions/api/cookies/cookies_api.cc
Expand Up @@ -106,6 +106,24 @@ bool ParseStoreContext(ChromeAsyncExtensionFunction* function,
return true;
}

template <typename TParams>
bool SetStoreBrowserContext(ChromeAsyncExtensionFunction* function,
std::unique_ptr<TParams>& parsedArgs,
scoped_refptr<net::URLRequestContextGetter>& storeBrowserContext) {

std::string store_id =
parsedArgs->details.store_id.get() ? *parsedArgs->details.store_id
: std::string();
net::URLRequestContextGetter* store_context = NULL;
if (!ParseStoreContext(function, &store_id, &store_context))
return false;
storeBrowserContext = store_context;
if (!parsedArgs->details.store_id.get())
parsedArgs->details.store_id.reset(new std::string(store_id));

return true;
}

} // namespace

CookiesEventRouter::CookiesEventRouter(content::BrowserContext* context)
Expand Down Expand Up @@ -204,25 +222,25 @@ CookiesGetFunction::CookiesGetFunction() {
CookiesGetFunction::~CookiesGetFunction() {
}

std::unique_ptr<api::cookies::Get::Params> CookiesGetFunction::GetParsedArgs() {
return Get::Params::Create(*args_);
}

bool CookiesGetFunction::SetStoreBrowserContext() {
return extensions::SetStoreBrowserContext(this, parsed_args_,
store_browser_context_);
}

bool CookiesGetFunction::RunAsync() {
parsed_args_ = Get::Params::Create(*args_);
parsed_args_ = GetParsedArgs();
EXTENSION_FUNCTION_VALIDATE(parsed_args_.get());

// Read/validate input parameters.
if (!ParseUrl(this, parsed_args_->details.url, &url_, true))
return false;

std::string store_id =
parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id
: std::string();
net::URLRequestContextGetter* store_context = NULL;
if (!ParseStoreContext(this, &store_id, &store_context))
if (!SetStoreBrowserContext())
return false;
store_browser_context_ = store_context;
if (!parsed_args_->details.store_id.get())
parsed_args_->details.store_id.reset(new std::string(store_id));

store_browser_context_ = store_context;

bool rv = BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
Expand Down Expand Up @@ -276,24 +294,27 @@ CookiesGetAllFunction::CookiesGetAllFunction() {
CookiesGetAllFunction::~CookiesGetAllFunction() {
}

std::unique_ptr<api::cookies::GetAll::Params> CookiesGetAllFunction::GetParsedArgs()
{
return GetAll::Params::Create(*args_);
}

bool CookiesGetAllFunction::SetStoreBrowserContext() {
return extensions::SetStoreBrowserContext(this, parsed_args_,
store_browser_context_);
}

bool CookiesGetAllFunction::RunAsync() {
parsed_args_ = GetAll::Params::Create(*args_);
parsed_args_ = GetParsedArgs();
EXTENSION_FUNCTION_VALIDATE(parsed_args_.get());

if (parsed_args_->details.url.get() &&
!ParseUrl(this, *parsed_args_->details.url, &url_, false)) {
return false;
}

std::string store_id =
parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id
: std::string();
net::URLRequestContextGetter* store_context = NULL;
if (!ParseStoreContext(this, &store_id, &store_context))
if (!SetStoreBrowserContext())
return false;
store_browser_context_ = store_context;
if (!parsed_args_->details.store_id.get())
parsed_args_->details.store_id.reset(new std::string(store_id));

bool rv = BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
Expand Down Expand Up @@ -339,23 +360,25 @@ CookiesSetFunction::CookiesSetFunction() : success_(false) {
CookiesSetFunction::~CookiesSetFunction() {
}

std::unique_ptr<api::cookies::Set::Params> CookiesSetFunction::GetParsedArgs() {
return Set::Params::Create(*args_);
}

bool CookiesSetFunction::SetStoreBrowserContext() {
return extensions::SetStoreBrowserContext(this, parsed_args_,
store_browser_context_);
}

bool CookiesSetFunction::RunAsync() {
parsed_args_ = Set::Params::Create(*args_);
parsed_args_ = GetParsedArgs();
EXTENSION_FUNCTION_VALIDATE(parsed_args_.get());

// Read/validate input parameters.
if (!ParseUrl(this, parsed_args_->details.url, &url_, true))
return false;

std::string store_id =
parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id
: std::string();
net::URLRequestContextGetter* store_context = NULL;
if (!ParseStoreContext(this, &store_id, &store_context))
if (!SetStoreBrowserContext())
return false;
store_browser_context_ = store_context;
if (!parsed_args_->details.store_id.get())
parsed_args_->details.store_id.reset(new std::string(store_id));

bool rv = BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
Expand Down Expand Up @@ -473,25 +496,27 @@ CookiesRemoveFunction::CookiesRemoveFunction() {
CookiesRemoveFunction::~CookiesRemoveFunction() {
}

std::unique_ptr<api::cookies::Remove::Params> CookiesRemoveFunction::GetParsedArgs()
{
return Remove::Params::Create(*args_);
}

bool CookiesRemoveFunction::SetStoreBrowserContext() {
return extensions::SetStoreBrowserContext(this, parsed_args_,
store_browser_context_);
}

bool CookiesRemoveFunction::RunAsync() {
parsed_args_ = Remove::Params::Create(*args_);
parsed_args_ = GetParsedArgs();
EXTENSION_FUNCTION_VALIDATE(parsed_args_.get());

// Read/validate input parameters.
if (!ParseUrl(this, parsed_args_->details.url, &url_, true))
return false;

std::string store_id =
parsed_args_->details.store_id.get() ? *parsed_args_->details.store_id
: std::string();
net::URLRequestContextGetter* store_context = NULL;
if (!ParseStoreContext(this, &store_id, &store_context))
if (!SetStoreBrowserContext())
return false;
store_browser_context_ = store_context;
if (!parsed_args_->details.store_id.get())
parsed_args_->details.store_id.reset(new std::string(store_id));

// Pass the work off to the IO thread.
bool rv = BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&CookiesRemoveFunction::RemoveCookieOnIOThread, this));
Expand Down
30 changes: 21 additions & 9 deletions chrome/browser/extensions/api/cookies/cookies_api.h
Expand Up @@ -75,14 +75,17 @@ class CookiesGetFunction : public ChromeAsyncExtensionFunction {
// ExtensionFunction:
bool RunAsync() override;

scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
std::unique_ptr<api::cookies::Get::Params> parsed_args_;

private:
virtual std::unique_ptr<api::cookies::Get::Params> GetParsedArgs();
virtual bool SetStoreBrowserContext();
void GetCookieOnIOThread();
void RespondOnUIThread();
void GetCookieCallback(const net::CookieList& cookie_list);

GURL url_;
scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
std::unique_ptr<api::cookies::Get::Params> parsed_args_;
};

// Implements the cookies.getAll() extension function.
Expand All @@ -98,14 +101,17 @@ class CookiesGetAllFunction : public ChromeAsyncExtensionFunction {
// ExtensionFunction:
bool RunAsync() override;

scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
std::unique_ptr<api::cookies::GetAll::Params> parsed_args_;

private:
virtual std::unique_ptr<api::cookies::GetAll::Params> GetParsedArgs();
virtual bool SetStoreBrowserContext();
void GetAllCookiesOnIOThread();
void RespondOnUIThread();
void GetAllCookiesCallback(const net::CookieList& cookie_list);

GURL url_;
scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
std::unique_ptr<api::cookies::GetAll::Params> parsed_args_;
};

// Implements the cookies.set() extension function.
Expand All @@ -119,16 +125,19 @@ class CookiesSetFunction : public ChromeAsyncExtensionFunction {
~CookiesSetFunction() override;
bool RunAsync() override;

scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
std::unique_ptr<api::cookies::Set::Params> parsed_args_;

private:
virtual std::unique_ptr<api::cookies::Set::Params> GetParsedArgs();
virtual bool SetStoreBrowserContext();
void SetCookieOnIOThread();
void RespondOnUIThread();
void PullCookie(bool set_cookie_);
void PullCookieCallback(const net::CookieList& cookie_list);

GURL url_;
bool success_;
scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
std::unique_ptr<api::cookies::Set::Params> parsed_args_;
};

// Implements the cookies.remove() extension function.
Expand All @@ -144,14 +153,17 @@ class CookiesRemoveFunction : public ChromeAsyncExtensionFunction {
// ExtensionFunction:
bool RunAsync() override;

private:
scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
std::unique_ptr<api::cookies::Remove::Params> parsed_args_;

private:
virtual std::unique_ptr<api::cookies::Remove::Params> GetParsedArgs();
virtual bool SetStoreBrowserContext();
void RemoveCookieOnIOThread();
void RespondOnUIThread();
void RemoveCookieCallback();

GURL url_;
scoped_refptr<net::URLRequestContextGetter> store_browser_context_;
std::unique_ptr<api::cookies::Remove::Params> parsed_args_;
};

// Implements the cookies.getAllCookieStores() extension function.
Expand Down

0 comments on commit dbc007e

Please sign in to comment.