Skip to content

Commit b869b13

Browse files
committed
APIContext for cookies API
1 parent 6b48b5e commit b869b13

File tree

2 files changed

+53
-35
lines changed

2 files changed

+53
-35
lines changed

src/api/window/window.cc

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,6 @@ Window::Window(int id,
143143
// Set ID for Shell
144144
shell_->set_id(id);
145145

146-
result_.reset(new base::ListValue);
147146
}
148147

149148
Window::~Window() {
@@ -281,88 +280,102 @@ void Window::CookieGet(const base::ListValue& arguments, bool get_all) {
281280
const base::DictionaryValue* details = NULL;
282281
std::string url;
283282

284-
store_context_ = context_getter;
283+
CookieAPIContext* api_context = new CookieAPIContext;
284+
api_context->store_context_ = context_getter;
285+
285286
arguments.GetDictionary(0, &details);
286287
if (details) {
287-
details_.reset(details->DeepCopyWithoutEmptyChildren());
288+
api_context->details_.reset(details->DeepCopyWithoutEmptyChildren());
288289
details->GetString("url", &url);
289290
}
290-
url_ = GURL(url);
291+
292+
api_context->url_ = GURL(url);
293+
api_context->result_.reset(new base::ListValue);
291294

292295
if (get_all) {
293296
bool rv = BrowserThread::PostTask(
294297
BrowserThread::IO, FROM_HERE,
295-
base::Bind(&Window::GetAllCookieOnIOThread, base::Unretained(this)));
298+
base::Bind(&Window::GetAllCookieOnIOThread,
299+
base::Unretained(this),
300+
make_scoped_refptr(api_context)));
296301
DCHECK(rv);
297302
}else{
298303
bool rv = BrowserThread::PostTask(
299304
BrowserThread::IO, FROM_HERE,
300-
base::Bind(&Window::GetCookieOnIOThread, base::Unretained(this)));
305+
base::Bind(&Window::GetCookieOnIOThread,
306+
base::Unretained(this),
307+
make_scoped_refptr(api_context)));
301308
DCHECK(rv);
302309
}
303310
}
304311

305-
void Window::GetAllCookieOnIOThread() {
312+
void Window::GetAllCookieOnIOThread(CookieAPIContext* api_context) {
306313
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
307314
net::CookieStore* cookie_store =
308-
store_context_->GetURLRequestContext()->cookie_store();
315+
api_context->store_context_->GetURLRequestContext()->cookie_store();
309316
GetCookieListFromStore(
310-
cookie_store, url_,
311-
base::Bind(&Window::GetAllCookieCallback, base::Unretained(this)));
317+
cookie_store, api_context->url_,
318+
base::Bind(&Window::GetAllCookieCallback, base::Unretained(this),
319+
make_scoped_refptr(api_context)));
312320
}
313321

314-
void Window::GetCookieOnIOThread() {
322+
void Window::GetCookieOnIOThread(CookieAPIContext* api_context) {
315323
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
316324
net::CookieStore* cookie_store =
317-
store_context_->GetURLRequestContext()->cookie_store();
325+
api_context->store_context_->GetURLRequestContext()->cookie_store();
318326
GetCookieListFromStore(
319-
cookie_store, url_,
320-
base::Bind(&Window::GetCookieCallback, base::Unretained(this)));
327+
cookie_store, api_context->url_,
328+
base::Bind(&Window::GetCookieCallback, base::Unretained(this),
329+
make_scoped_refptr(api_context)));
321330
}
322331

323-
void Window::GetAllCookieCallback(const net::CookieList& cookie_list) {
332+
void Window::GetAllCookieCallback(CookieAPIContext* api_context,
333+
const net::CookieList& cookie_list) {
324334
net::CookieList::const_iterator it;
325-
result_->Clear();
335+
api_context->result_->Clear();
326336
for (it = cookie_list.begin(); it != cookie_list.end(); ++it) {
327-
if (MatchesCookie(details_.get(), *it)) {
328-
result_->Append(PopulateCookieObject(*it));
337+
if (MatchesCookie(api_context->details_.get(), *it)) {
338+
api_context->result_->Append(PopulateCookieObject(*it));
329339
}
330340
}
331341

332342
bool rv = BrowserThread::PostTask(
333343
BrowserThread::UI, FROM_HERE,
334-
base::Bind(&Window::RespondOnUIThread, base::Unretained(this)));
344+
base::Bind(&Window::RespondOnUIThread, base::Unretained(this),
345+
make_scoped_refptr(api_context)));
335346
DCHECK(rv);
336347
}
337348

338-
void Window::GetCookieCallback(const net::CookieList& cookie_list) {
349+
void Window::GetCookieCallback(CookieAPIContext* api_context,
350+
const net::CookieList& cookie_list) {
339351
net::CookieList::const_iterator it;
340352
std::string name;
341-
details_->GetString("name", &name);
353+
api_context->details_->GetString("name", &name);
342354

343-
result_->Clear();
355+
api_context->result_->Clear();
344356

345357
for (it = cookie_list.begin(); it != cookie_list.end(); ++it) {
346358
// Return the first matching cookie. Relies on the fact that the
347359
// CookieMonster returns them in canonical order (longest path, then
348360
// earliest creation time).
349361

350362
if (it->Name() == name) {
351-
result_->Append(PopulateCookieObject(*it));
363+
api_context->result_->Append(PopulateCookieObject(*it));
352364
break;
353365
}
354366
}
355367

356368
bool rv = BrowserThread::PostTask(
357369
BrowserThread::UI, FROM_HERE,
358-
base::Bind(&Window::RespondOnUIThread, base::Unretained(this)));
370+
base::Bind(&Window::RespondOnUIThread, base::Unretained(this),
371+
make_scoped_refptr(api_context)));
359372
DCHECK(rv);
360373
}
361374

362-
void Window::RespondOnUIThread() {
375+
void Window::RespondOnUIThread(CookieAPIContext* api_context) {
363376
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
364377
base::ListValue ret;
365-
ret.Append(result_.release());
378+
ret.Append(api_context->result_.release());
366379
dispatcher_host()->SendEvent(this, "__nw_gotcookie", ret);
367380
}
368381

src/api/window/window.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ class Shell;
3636

3737
namespace api {
3838

39+
class CookieAPIContext : public base::RefCountedThreadSafe<CookieAPIContext> {
40+
public:
41+
net::URLRequestContextGetter* store_context_;
42+
scoped_ptr<base::DictionaryValue> details_;
43+
scoped_ptr<base::ListValue> result_;
44+
GURL url_;
45+
};
46+
47+
3948
class Window : public Base {
4049
public:
4150
Window(int id,
@@ -50,19 +59,15 @@ class Window : public Base {
5059
base::ListValue* result) OVERRIDE;
5160

5261
void CookieGet(const base::ListValue& arguments, bool get_all = false);
53-
void GetCookieOnIOThread();
54-
void GetAllCookieOnIOThread();
55-
void GetCookieCallback(const net::CookieList& cookie_list);
56-
void GetAllCookieCallback(const net::CookieList& cookie_list);
57-
void RespondOnUIThread();
62+
void GetCookieOnIOThread(CookieAPIContext*);
63+
void GetAllCookieOnIOThread(CookieAPIContext*);
64+
void GetCookieCallback(CookieAPIContext*, const net::CookieList& cookie_list);
65+
void GetAllCookieCallback(CookieAPIContext*, const net::CookieList& cookie_list);
66+
void RespondOnUIThread(CookieAPIContext*);
5867

5968
private:
6069

6170
content::Shell* shell_;
62-
net::URLRequestContextGetter* store_context_;
63-
scoped_ptr<base::DictionaryValue> details_;
64-
scoped_ptr<base::ListValue> result_;
65-
GURL url_;
6671

6772
DISALLOW_COPY_AND_ASSIGN(Window);
6873
};

0 commit comments

Comments
 (0)