summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/cookies/cookies_api.cc
blob: 7446484e6512d5c23d1ff5baa7d2ca7662f1f217 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Implements the Chrome Extensions Cookies API.

#include "chrome/browser/extensions/api/cookies/cookies_api.h"

#include <vector>

#include "base/bind.h"
#include "base/json/json_writer.h"
#include "base/lazy_instance.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/extensions/api/cookies/cookies_api_constants.h"
#include "chrome/browser/extensions/api/cookies/cookies_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_iterator.h"
#include "chrome/common/extensions/api/cookies.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/permissions/permissions_data.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_constants.h"
#include "net/cookies/cookie_monster.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"

using content::BrowserThread;
using extensions::api::cookies::Cookie;
using extensions::api::cookies::CookieStore;

namespace Get = extensions::api::cookies::Get;
namespace GetAll = extensions::api::cookies::GetAll;
namespace GetAllCookieStores = extensions::api::cookies::GetAllCookieStores;
namespace Remove = extensions::api::cookies::Remove;
namespace Set = extensions::api::cookies::Set;

namespace extensions {
namespace cookies = api::cookies;
namespace keys = cookies_api_constants;

CookiesEventRouter::CookiesEventRouter(Profile* profile)
    : profile_(profile) {
  CHECK(registrar_.IsEmpty());
  registrar_.Add(this,
                 chrome::NOTIFICATION_COOKIE_CHANGED,
                 content::NotificationService::AllBrowserContextsAndSources());
}

CookiesEventRouter::~CookiesEventRouter() {
}

void CookiesEventRouter::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  Profile* profile = content::Source<Profile>(source).ptr();
  if (!profile_->IsSameProfile(profile))
    return;

  switch (type) {
    case chrome::NOTIFICATION_COOKIE_CHANGED:
      CookieChanged(
          profile,
          content::Details<ChromeCookieDetails>(details).ptr());
      break;

    default:
      NOTREACHED();
  }
}

void CookiesEventRouter::CookieChanged(
    Profile* profile,
    ChromeCookieDetails* details) {
  scoped_ptr<base::ListValue> args(new base::ListValue());
  base::DictionaryValue* dict = new base::DictionaryValue();
  dict->SetBoolean(keys::kRemovedKey, details->removed);

  scoped_ptr<Cookie> cookie(
      cookies_helpers::CreateCookie(*details->cookie,
          cookies_helpers::GetStoreIdFromProfile(profile)));
  dict->Set(keys::kCookieKey, cookie->ToValue().release());

  // Map the internal cause to an external string.
  std::string cause;
  switch (details->cause) {
    case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT:
      cause = keys::kExplicitChangeCause;
      break;

    case net::CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE:
      cause = keys::kOverwriteChangeCause;
      break;

    case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED:
      cause = keys::kExpiredChangeCause;
      break;

    case net::CookieMonster::Delegate::CHANGE_COOKIE_EVICTED:
      cause = keys::kEvictedChangeCause;
      break;

    case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED_OVERWRITE:
      cause = keys::kExpiredOverwriteChangeCause;
      break;

    default:
      NOTREACHED();
  }
  dict->SetString(keys::kCauseKey, cause);

  args->Append(dict);

  GURL cookie_domain =
      cookies_helpers::GetURLFromCanonicalCookie(*details->cookie);
  DispatchEvent(profile,
                cookies::OnChanged::kEventName,
                args.Pass(),
                cookie_domain);
}

void CookiesEventRouter::DispatchEvent(
    Profile* profile,
    const std::string& event_name,
    scoped_ptr<base::ListValue> event_args,
    GURL& cookie_domain) {
  EventRouter* router = profile ?
      extensions::ExtensionSystem::Get(profile)->event_router() : NULL;
  if (!router)
    return;
  scoped_ptr<Event> event(new Event(event_name, event_args.Pass()));
  event->restrict_to_browser_context = profile;
  event->event_url = cookie_domain;
  router->BroadcastEvent(event.Pass());
}

bool CookiesFunction::ParseUrl(const std::string& url_string, GURL* url,
                               bool check_host_permissions) {
  *url = GURL(url_string);
  if (!url->is_valid()) {
    error_ = ErrorUtils::FormatErrorMessage(
        keys::kInvalidUrlError, url_string);
    return false;
  }
  // Check against host permissions if needed.
  if (check_host_permissions &&
      !PermissionsData::HasHostPermission(GetExtension(), *url)) {
    error_ = ErrorUtils::FormatErrorMessage(
        keys::kNoHostPermissionsError, url->spec());
    return false;
  }
  return true;
}

bool CookiesFunction::ParseStoreContext(
    std::string* store_id,
    net::URLRequestContextGetter** context) {
  DCHECK((context || store_id->empty()));
  Profile* store_profile = NULL;
  if (!store_id->empty()) {
    store_profile = cookies_helpers::ChooseProfileFromStoreId(
        *store_id, GetProfile(), include_incognito());
    if (!store_profile) {
      error_ = ErrorUtils::FormatErrorMessage(
          keys::kInvalidStoreIdError, *store_id);
      return false;
    }
  } else {
    // The store ID was not specified; use the current execution context's
    // cookie store by default.
    // GetCurrentBrowser() already takes into account incognito settings.
    Browser* current_browser = GetCurrentBrowser();
    if (!current_browser) {
      error_ = keys::kNoCookieStoreFoundError;
      return false;
    }
    store_profile = current_browser->profile();
    *store_id = cookies_helpers::GetStoreIdFromProfile(store_profile);
  }

  if (context)
    *context = store_profile->GetRequestContext();
  DCHECK(context);

  return true;
}

CookiesGetFunction::CookiesGetFunction() {
}

CookiesGetFunction::~CookiesGetFunction() {
}

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

  // Read/validate input parameters.
  if (!ParseUrl(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(&store_id, &store_context))
    return false;
  store_context_ = store_context;
  if (!parsed_args_->details.store_id.get())
    parsed_args_->details.store_id.reset(new std::string(store_id));

  store_context_ = store_context;

  bool rv = BrowserThread::PostTask(
      BrowserThread::IO, FROM_HERE,
      base::Bind(&CookiesGetFunction::GetCookieOnIOThread, this));
  DCHECK(rv);

  // Will finish asynchronously.
  return true;
}

void CookiesGetFunction::GetCookieOnIOThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  net::CookieStore* cookie_store =
      store_context_->GetURLRequestContext()->cookie_store();
  cookies_helpers::GetCookieListFromStore(
      cookie_store, url_,
      base::Bind(&CookiesGetFunction::GetCookieCallback, this));
}

void CookiesGetFunction::GetCookieCallback(const net::CookieList& cookie_list) {
  net::CookieList::const_iterator it;
  for (it = cookie_list.begin(); it != cookie_list.end(); ++it) {
    // Return the first matching cookie. Relies on the fact that the
    // CookieMonster returns them in canonical order (longest path, then
    // earliest creation time).
    if (it->Name() == parsed_args_->details.name) {
      scoped_ptr<Cookie> cookie(
          cookies_helpers::CreateCookie(*it, *parsed_args_->details.store_id));
      results_ = Get::Results::Create(*cookie);
      break;
    }
  }

  // The cookie doesn't exist; return null.
  if (it == cookie_list.end())
    SetResult(base::Value::CreateNullValue());

  bool rv = BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&CookiesGetFunction::RespondOnUIThread, this));
  DCHECK(rv);
}

void CookiesGetFunction::RespondOnUIThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  SendResponse(true);
}

CookiesGetAllFunction::CookiesGetAllFunction() {
}

CookiesGetAllFunction::~CookiesGetAllFunction() {
}

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

  if (parsed_args_->details.url.get() &&
      !ParseUrl(*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(&store_id, &store_context))
    return false;
  store_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,
      base::Bind(&CookiesGetAllFunction::GetAllCookiesOnIOThread, this));
  DCHECK(rv);

  // Will finish asynchronously.
  return true;
}

void CookiesGetAllFunction::GetAllCookiesOnIOThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  net::CookieStore* cookie_store =
      store_context_->GetURLRequestContext()->cookie_store();
  cookies_helpers::GetCookieListFromStore(
      cookie_store, url_,
      base::Bind(&CookiesGetAllFunction::GetAllCookiesCallback, this));
}

void CookiesGetAllFunction::GetAllCookiesCallback(
    const net::CookieList& cookie_list) {
  const extensions::Extension* extension = GetExtension();
  if (extension) {
    std::vector<linked_ptr<Cookie> > match_vector;
    cookies_helpers::AppendMatchingCookiesToVector(
        cookie_list, url_, &parsed_args_->details,
        GetExtension(), &match_vector);

    results_ = GetAll::Results::Create(match_vector);
  }
  bool rv = BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&CookiesGetAllFunction::RespondOnUIThread, this));
  DCHECK(rv);
}

void CookiesGetAllFunction::RespondOnUIThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  SendResponse(true);
}

CookiesSetFunction::CookiesSetFunction() : success_(false) {
}

CookiesSetFunction::~CookiesSetFunction() {
}

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

  // Read/validate input parameters.
  if (!ParseUrl(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(&store_id, &store_context))
    return false;
  store_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,
      base::Bind(&CookiesSetFunction::SetCookieOnIOThread, this));
  DCHECK(rv);

  // Will finish asynchronously.
  return true;
}

void CookiesSetFunction::SetCookieOnIOThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  net::CookieMonster* cookie_monster =
      store_context_->GetURLRequestContext()->cookie_store()->
      GetCookieMonster();

  base::Time expiration_time;
  if (parsed_args_->details.expiration_date.get()) {
    // Time::FromDoubleT converts double time 0 to empty Time object. So we need
    // to do special handling here.
    expiration_time = (*parsed_args_->details.expiration_date == 0) ?
        base::Time::UnixEpoch() :
        base::Time::FromDoubleT(*parsed_args_->details.expiration_date);
  }

  cookie_monster->SetCookieWithDetailsAsync(
      url_,
      parsed_args_->details.name.get() ? *parsed_args_->details.name
                                       : std::string(),
      parsed_args_->details.value.get() ? *parsed_args_->details.value
                                        : std::string(),
      parsed_args_->details.domain.get() ? *parsed_args_->details.domain
                                         : std::string(),
      parsed_args_->details.path.get() ? *parsed_args_->details.path
                                       : std::string(),
      expiration_time,
      parsed_args_->details.secure.get() ? *parsed_args_->details.secure.get()
                                         : false,
      parsed_args_->details.http_only.get() ? *parsed_args_->details.http_only
                                            : false,
      net::COOKIE_PRIORITY_DEFAULT,
      base::Bind(&CookiesSetFunction::PullCookie, this));
}

void CookiesSetFunction::PullCookie(bool set_cookie_result) {
  // Pull the newly set cookie.
  net::CookieMonster* cookie_monster =
      store_context_->GetURLRequestContext()->cookie_store()->
      GetCookieMonster();
  success_ = set_cookie_result;
  cookies_helpers::GetCookieListFromStore(
      cookie_monster, url_,
      base::Bind(&CookiesSetFunction::PullCookieCallback, this));
}

void CookiesSetFunction::PullCookieCallback(
    const net::CookieList& cookie_list) {
  net::CookieList::const_iterator it;
  for (it = cookie_list.begin(); it != cookie_list.end(); ++it) {
    // Return the first matching cookie. Relies on the fact that the
    // CookieMonster returns them in canonical order (longest path, then
    // earliest creation time).
    std::string name =
        parsed_args_->details.name.get() ? *parsed_args_->details.name
                                         : std::string();
    if (it->Name() == name) {
      scoped_ptr<Cookie> cookie(
          cookies_helpers::CreateCookie(*it, *parsed_args_->details.store_id));
      results_ = Set::Results::Create(*cookie);
      break;
    }
  }

  bool rv = BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&CookiesSetFunction::RespondOnUIThread, this));
  DCHECK(rv);
}

void CookiesSetFunction::RespondOnUIThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  if (!success_) {
    std::string name =
        parsed_args_->details.name.get() ? *parsed_args_->details.name
                                         : std::string();
    error_ = ErrorUtils::FormatErrorMessage(keys::kCookieSetFailedError, name);
  }
  SendResponse(success_);
}

CookiesRemoveFunction::CookiesRemoveFunction() {
}

CookiesRemoveFunction::~CookiesRemoveFunction() {
}

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

  // Read/validate input parameters.
  if (!ParseUrl(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(&store_id, &store_context))
    return false;
  store_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));
  DCHECK(rv);

  // Will return asynchronously.
  return true;
}

void CookiesRemoveFunction::RemoveCookieOnIOThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  // Remove the cookie
  net::CookieStore* cookie_store =
      store_context_->GetURLRequestContext()->cookie_store();
  cookie_store->DeleteCookieAsync(
      url_, parsed_args_->details.name,
      base::Bind(&CookiesRemoveFunction::RemoveCookieCallback, this));
}

void CookiesRemoveFunction::RemoveCookieCallback() {
  // Build the callback result
  Remove::Results::Details details;
  details.name = parsed_args_->details.name;
  details.url = url_.spec();
  details.store_id = *parsed_args_->details.store_id;
  results_ = Remove::Results::Create(details);

  // Return to UI thread
  bool rv = BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&CookiesRemoveFunction::RespondOnUIThread, this));
  DCHECK(rv);
}

void CookiesRemoveFunction::RespondOnUIThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  SendResponse(true);
}

bool CookiesGetAllCookieStoresFunction::RunImpl() {
  Profile* original_profile = GetProfile();
  DCHECK(original_profile);
  scoped_ptr<base::ListValue> original_tab_ids(new base::ListValue());
  Profile* incognito_profile = NULL;
  scoped_ptr<base::ListValue> incognito_tab_ids;
  if (include_incognito() && GetProfile()->HasOffTheRecordProfile()) {
    incognito_profile = GetProfile()->GetOffTheRecordProfile();
    if (incognito_profile)
      incognito_tab_ids.reset(new base::ListValue());
  }
  DCHECK(original_profile != incognito_profile);

  // Iterate through all browser instances, and for each browser,
  // add its tab IDs to either the regular or incognito tab ID list depending
  // whether the browser is regular or incognito.
  for (chrome::BrowserIterator it; !it.done(); it.Next()) {
    Browser* browser = *it;
    if (browser->profile() == original_profile) {
      cookies_helpers::AppendToTabIdList(browser, original_tab_ids.get());
    } else if (incognito_tab_ids.get() &&
               browser->profile() == incognito_profile) {
      cookies_helpers::AppendToTabIdList(browser, incognito_tab_ids.get());
    }
  }
  // Return a list of all cookie stores with at least one open tab.
  std::vector<linked_ptr<CookieStore> > cookie_stores;
  if (original_tab_ids->GetSize() > 0) {
    cookie_stores.push_back(make_linked_ptr(
        cookies_helpers::CreateCookieStore(
            original_profile, original_tab_ids.release()).release()));
  }
  if (incognito_tab_ids.get() && incognito_tab_ids->GetSize() > 0 &&
      incognito_profile) {
    cookie_stores.push_back(make_linked_ptr(
        cookies_helpers::CreateCookieStore(
            incognito_profile, incognito_tab_ids.release()).release()));
  }
  results_ = GetAllCookieStores::Results::Create(cookie_stores);
  return true;
}

void CookiesGetAllCookieStoresFunction::Run() {
  SendResponse(RunImpl());
}

CookiesAPI::CookiesAPI(Profile* profile)
    : profile_(profile) {
  ExtensionSystem::Get(profile_)->event_router()->RegisterObserver(
      this, cookies::OnChanged::kEventName);
}

CookiesAPI::~CookiesAPI() {
}

void CookiesAPI::Shutdown() {
  ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
}

static base::LazyInstance<ProfileKeyedAPIFactory<CookiesAPI> >
g_factory = LAZY_INSTANCE_INITIALIZER;

// static
ProfileKeyedAPIFactory<CookiesAPI>* CookiesAPI::GetFactoryInstance() {
  return g_factory.Pointer();
}

void CookiesAPI::OnListenerAdded(
    const extensions::EventListenerInfo& details) {
  cookies_event_router_.reset(new CookiesEventRouter(profile_));
  ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this);
}

}  // namespace extensions