|
23 | 23 | #include "base/values.h" |
24 | 24 | #include "base/strings/stringprintf.h" |
25 | 25 | #include "base/strings/utf_string_conversions.h" |
| 26 | +#include "chrome/browser/chrome_notification_types.h" |
26 | 27 | #include "content/nw/src/api/dispatcher_host.h" |
27 | 28 | #include "content/nw/src/api/menu/menu.h" |
28 | 29 | #include "content/nw/src/browser/native_window.h" |
29 | 30 | #include "content/nw/src/nw_shell.h" |
30 | 31 | #include "content/nw/src/shell_browser_context.h" |
31 | 32 | #include "content/public/browser/browser_thread.h" |
| 33 | +#include "content/public/browser/notification_service.h" |
32 | 34 | #include "content/public/browser/render_view_host.h" |
| 35 | +#include "content/public/common/url_constants.h" |
33 | 36 | #include "net/cookies/canonical_cookie.h" |
34 | 37 | #include "net/cookies/cookie_constants.h" |
35 | 38 | #include "net/cookies/cookie_monster.h" |
|
38 | 41 | #include "url/gurl.h" |
39 | 42 |
|
40 | 43 | using content::BrowserThread; |
| 44 | +using content::ShellBrowserContext; |
41 | 45 |
|
42 | 46 | namespace { |
43 | 47 |
|
| 48 | +const char kCauseKey[] = "cause"; |
| 49 | +const char kCookieKey[] = "cookie"; |
| 50 | +const char kDomainKey[] = "domain"; |
| 51 | +const char kIdKey[] = "id"; |
| 52 | +const char kRemovedKey[] = "removed"; |
| 53 | +const char kTabIdsKey[] = "tabIds"; |
| 54 | + |
| 55 | +// Cause Constants |
| 56 | +const char kEvictedChangeCause[] = "evicted"; |
| 57 | +const char kExpiredChangeCause[] = "expired"; |
| 58 | +const char kExpiredOverwriteChangeCause[] = "expired_overwrite"; |
| 59 | +const char kExplicitChangeCause[] = "explicit"; |
| 60 | +const char kOverwriteChangeCause[] = "overwrite"; |
| 61 | + |
| 62 | +GURL GetURLFromCanonicalCookie(const net::CanonicalCookie& cookie) { |
| 63 | + const std::string& domain_key = cookie.Domain(); |
| 64 | + const std::string scheme = |
| 65 | + cookie.IsSecure() ? "https" : "http"; |
| 66 | + const std::string host = |
| 67 | + domain_key.find('.') != 0 ? domain_key : domain_key.substr(1); |
| 68 | + return GURL(scheme + content::kStandardSchemeSeparator + host + "/"); |
| 69 | +} |
| 70 | + |
44 | 71 | void GetCookieListFromStore( |
45 | 72 | net::CookieStore* cookie_store, const GURL& url, |
46 | 73 | const net::CookieMonster::GetCookieListCallback& callback) { |
@@ -143,7 +170,10 @@ Window::Window(int id, |
143 | 170 | DVLOG(1) << "Window::Window(" << id << ")"; |
144 | 171 | // Set ID for Shell |
145 | 172 | shell_->set_id(id); |
146 | | - |
| 173 | + CHECK(registrar_.IsEmpty()); |
| 174 | + registrar_.Add(this, |
| 175 | + chrome::NOTIFICATION_COOKIE_CHANGED, |
| 176 | + content::NotificationService::AllBrowserContextsAndSources()); |
147 | 177 | } |
148 | 178 |
|
149 | 179 | Window::~Window() { |
@@ -512,4 +542,70 @@ void Window::PullCookieCallback(CookieAPIContext* api_context, |
512 | 542 | DCHECK(rv); |
513 | 543 | } |
514 | 544 |
|
| 545 | +void Window::Observe( |
| 546 | + int type, |
| 547 | + const content::NotificationSource& source, |
| 548 | + const content::NotificationDetails& details) { |
| 549 | + |
| 550 | + ShellBrowserContext* browser_context = |
| 551 | + content::Source<ShellBrowserContext>(source).ptr(); |
| 552 | + |
| 553 | + switch (type) { |
| 554 | + case chrome::NOTIFICATION_COOKIE_CHANGED: |
| 555 | + CookieChanged( |
| 556 | + browser_context, |
| 557 | + content::Details<ChromeCookieDetails>(details).ptr()); |
| 558 | + break; |
| 559 | + |
| 560 | + default: |
| 561 | + NOTREACHED(); |
| 562 | + } |
| 563 | +} |
| 564 | + |
| 565 | +void Window::CookieChanged( |
| 566 | + ShellBrowserContext* browser_context, |
| 567 | + ChromeCookieDetails* details) { |
| 568 | + scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 569 | + base::DictionaryValue* dict = new base::DictionaryValue(); |
| 570 | + dict->SetBoolean(kRemovedKey, details->removed); |
| 571 | + dict->Set(kCookieKey, PopulateCookieObject(*details->cookie)); |
| 572 | + |
| 573 | + // Map the internal cause to an external string. |
| 574 | + std::string cause; |
| 575 | + switch (details->cause) { |
| 576 | + case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPLICIT: |
| 577 | + cause = kExplicitChangeCause; |
| 578 | + break; |
| 579 | + |
| 580 | + case net::CookieMonster::Delegate::CHANGE_COOKIE_OVERWRITE: |
| 581 | + cause = kOverwriteChangeCause; |
| 582 | + break; |
| 583 | + |
| 584 | + case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED: |
| 585 | + cause = kExpiredChangeCause; |
| 586 | + break; |
| 587 | + |
| 588 | + case net::CookieMonster::Delegate::CHANGE_COOKIE_EVICTED: |
| 589 | + cause = kEvictedChangeCause; |
| 590 | + break; |
| 591 | + |
| 592 | + case net::CookieMonster::Delegate::CHANGE_COOKIE_EXPIRED_OVERWRITE: |
| 593 | + cause = kExpiredOverwriteChangeCause; |
| 594 | + break; |
| 595 | + |
| 596 | + default: |
| 597 | + NOTREACHED(); |
| 598 | + } |
| 599 | + dict->SetString(kCauseKey, cause); |
| 600 | + |
| 601 | + args->Append(dict); |
| 602 | + |
| 603 | + GURL cookie_domain = |
| 604 | + GetURLFromCanonicalCookie(*details->cookie); |
| 605 | + |
| 606 | + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 607 | + dispatcher_host()->SendEvent(this, "__nw_cookie_changed", *args); |
| 608 | + |
| 609 | +} |
| 610 | + |
515 | 611 | } // namespace api |
0 commit comments