summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions
diff options
context:
space:
mode:
authorjochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-10 13:14:44 +0000
committerjochen@chromium.org <jochen@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-06-10 13:14:44 +0000
commit2c5e1e131287e37ab5d837a271ab3e55cea06120 (patch)
tree5c5a43169fb13dcc75039c528709a9479a761596 /chrome/browser/extensions
parent2ec79f7df2ebb464ddb187f65fae631d54408d04 (diff)
downloadchromium_src-2c5e1e131287e37ab5d837a271ab3e55cea06120.zip
chromium_src-2c5e1e131287e37ab5d837a271ab3e55cea06120.tar.gz
chromium_src-2c5e1e131287e37ab5d837a271ab3e55cea06120.tar.bz2
Send event on changes to the cookie monster.
BUG=38398 TEST=*.CookiesEvents Review URL: http://codereview.chromium.org/2703004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@49390 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
-rw-r--r--chrome/browser/extensions/extension_cookies_api.cc62
-rw-r--r--chrome/browser/extensions/extension_cookies_api.h40
-rw-r--r--chrome/browser/extensions/extension_cookies_api_constants.cc4
-rw-r--r--chrome/browser/extensions/extension_cookies_api_constants.h5
-rw-r--r--chrome/browser/extensions/extension_cookies_apitest.cc9
-rw-r--r--chrome/browser/extensions/extensions_service.cc2
6 files changed, 120 insertions, 2 deletions
diff --git a/chrome/browser/extensions/extension_cookies_api.cc b/chrome/browser/extensions/extension_cookies_api.cc
index d9b46df..0cf9b07 100644
--- a/chrome/browser/extensions/extension_cookies_api.cc
+++ b/chrome/browser/extensions/extension_cookies_api.cc
@@ -6,16 +6,78 @@
#include "chrome/browser/extensions/extension_cookies_api.h"
+#include "base/json/json_writer.h"
#include "chrome/browser/browser_list.h"
#include "chrome/browser/extensions/extension_cookies_api_constants.h"
#include "chrome/browser/extensions/extension_cookies_helpers.h"
+#include "chrome/browser/extensions/extension_message_service.h"
#include "chrome/browser/profile.h"
#include "chrome/common/extensions/extension_error_utils.h"
#include "chrome/common/net/url_request_context_getter.h"
+#include "chrome/common/notification_type.h"
+#include "chrome/common/notification_service.h"
#include "net/base/cookie_monster.h"
namespace keys = extension_cookies_api_constants;
+// static
+ExtensionCookiesEventRouter* ExtensionCookiesEventRouter::GetInstance() {
+ return Singleton<ExtensionCookiesEventRouter>::get();
+}
+
+void ExtensionCookiesEventRouter::Init() {
+ if (registrar_.IsEmpty()) {
+ registrar_.Add(this,
+ NotificationType::COOKIE_CHANGED,
+ NotificationService::AllSources());
+ }
+}
+
+void ExtensionCookiesEventRouter::Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details) {
+ switch (type.value) {
+ case NotificationType::COOKIE_CHANGED:
+ CookieChanged(
+ Source<Profile>(source).ptr(),
+ Details<ChromeCookieDetails>(details).ptr());
+ break;
+
+ default:
+ NOTREACHED();
+ }
+}
+
+void ExtensionCookiesEventRouter::CookieChanged(
+ Profile* profile,
+ ChromeCookieDetails* details) {
+ ListValue args;
+ DictionaryValue* dict = new DictionaryValue();
+ dict->SetBoolean(keys::kRemovedKey, details->removed);
+ dict->Set(
+ keys::kCookieKey,
+ extension_cookies_helpers::CreateCookieValue(*details->cookie_pair,
+ extension_cookies_helpers::GetStoreIdFromProfile(profile)));
+ args.Append(dict);
+
+ std::string json_args;
+ base::JSONWriter::Write(&args, false, &json_args);
+ GURL cookie_domain =
+ extension_cookies_helpers::GetURLFromCookiePair(*details->cookie_pair);
+ LOG(WARNING) << "Sending cookie " << json_args;
+ DispatchEvent(profile, keys::kOnChanged, json_args, cookie_domain);
+}
+
+void ExtensionCookiesEventRouter::DispatchEvent(Profile* profile,
+ const char* event_name,
+ const std::string& json_args,
+ GURL& cookie_domain) {
+ if (profile && profile->GetExtensionMessageService()) {
+ profile->GetExtensionMessageService()->DispatchEventToRenderers(
+ event_name, json_args, profile->IsOffTheRecord(), cookie_domain);
+ }
+}
+
bool CookiesFunction::ParseUrl(const DictionaryValue* details, GURL* url,
bool check_host_permissions) {
DCHECK(details && url);
diff --git a/chrome/browser/extensions/extension_cookies_api.h b/chrome/browser/extensions/extension_cookies_api.h
index 27cd626..c230b01 100644
--- a/chrome/browser/extensions/extension_cookies_api.h
+++ b/chrome/browser/extensions/extension_cookies_api.h
@@ -10,12 +10,52 @@
#include <string>
+#include "base/singleton.h"
#include "chrome/browser/extensions/extension_function.h"
+#include "chrome/browser/net/chrome_cookie_notification_details.h"
+#include "chrome/common/notification_registrar.h"
namespace net {
class CookieStore;
} // namespace net
+// Observes CookieMonster notifications and routes them as events to the
+// extension system.
+class ExtensionCookiesEventRouter : public NotificationObserver {
+ public:
+ // Single instance of the event router.
+ static ExtensionCookiesEventRouter* GetInstance();
+
+ void Init();
+
+ private:
+ friend struct DefaultSingletonTraits<ExtensionCookiesEventRouter>;
+
+ ExtensionCookiesEventRouter() {}
+ virtual ~ExtensionCookiesEventRouter() {}
+
+ // NotificationObserver implementation.
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details);
+
+ // Handler for the COOKIE_CHANGED event. The method takes the details of such
+ // an event and constructs a suitable JSON formatted extension event from it.
+ void CookieChanged(Profile* profile,
+ ChromeCookieDetails* details);
+
+ // This method dispatches events to the extension message service.
+ void DispatchEvent(Profile* context,
+ const char* event_name,
+ const std::string& json_args,
+ GURL& cookie_domain);
+
+ // Used for tracking registrations to CookieMonster notifications.
+ NotificationRegistrar registrar_;
+
+ DISALLOW_COPY_AND_ASSIGN(ExtensionCookiesEventRouter);
+};
+
// Serves as a base class for all cookies API functions, and defines some
// common functionality for parsing cookies API function arguments.
// Note that all of the functions in this file derive from ExtensionFunction,
diff --git a/chrome/browser/extensions/extension_cookies_api_constants.cc b/chrome/browser/extensions/extension_cookies_api_constants.cc
index 921a966..e77482b 100644
--- a/chrome/browser/extensions/extension_cookies_api_constants.cc
+++ b/chrome/browser/extensions/extension_cookies_api_constants.cc
@@ -6,6 +6,7 @@
namespace extension_cookies_api_constants {
+const wchar_t kCookieKey[] = L"cookie";
const wchar_t kDomainKey[] = L"domain";
const wchar_t kExpirationDateKey[] = L"expirationDate";
const wchar_t kHostOnlyKey[] = L"hostOnly";
@@ -13,6 +14,7 @@ const wchar_t kHttpOnlyKey[] = L"httpOnly";
const wchar_t kIdKey[] = L"id";
const wchar_t kNameKey[] = L"name";
const wchar_t kPathKey[] = L"path";
+const wchar_t kRemovedKey[] = L"removed";
const wchar_t kSecureKey[] = L"secure";
const wchar_t kSessionKey[] = L"session";
const wchar_t kStoreIdKey[] = L"storeId";
@@ -20,6 +22,8 @@ const wchar_t kTabIdsKey[] = L"tabIds";
const wchar_t kUrlKey[] = L"url";
const wchar_t kValueKey[] = L"value";
+const char kOnChanged[] = "experimental.cookies.onChanged";
+
const char kCookieSetFailedError[] =
"Failed to parse or set cookie named \"*\".";
const char kInvalidStoreIdError[] = "Invalid cookie store id: \"*\".";
diff --git a/chrome/browser/extensions/extension_cookies_api_constants.h b/chrome/browser/extensions/extension_cookies_api_constants.h
index 28814b7..d9c0015 100644
--- a/chrome/browser/extensions/extension_cookies_api_constants.h
+++ b/chrome/browser/extensions/extension_cookies_api_constants.h
@@ -10,6 +10,7 @@
namespace extension_cookies_api_constants {
// Keys.
+extern const wchar_t kCookieKey[];
extern const wchar_t kDomainKey[];
extern const wchar_t kExpirationDateKey[];
extern const wchar_t kHostOnlyKey[];
@@ -17,6 +18,7 @@ extern const wchar_t kHttpOnlyKey[];
extern const wchar_t kIdKey[];
extern const wchar_t kNameKey[];
extern const wchar_t kPathKey[];
+extern const wchar_t kRemovedKey[];
extern const wchar_t kSecureKey[];
extern const wchar_t kSessionKey[];
extern const wchar_t kStoreIdKey[];
@@ -24,7 +26,8 @@ extern const wchar_t kTabIdsKey[];
extern const wchar_t kUrlKey[];
extern const wchar_t kValueKey[];
-// TODO(cindylau): kOnChanged is not yet implemented.
+// Events.
+extern const char kOnChanged[];
// Errors.
extern const char kCookieSetFailedError[];
diff --git a/chrome/browser/extensions/extension_cookies_apitest.cc b/chrome/browser/extensions/extension_cookies_apitest.cc
index 12c6373..396f8c7 100644
--- a/chrome/browser/extensions/extension_cookies_apitest.cc
+++ b/chrome/browser/extensions/extension_cookies_apitest.cc
@@ -10,5 +10,12 @@ IN_PROC_BROWSER_TEST_F(ExtensionApiTest, Cookies) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kEnableExperimentalExtensionApis);
- ASSERT_TRUE(RunExtensionTest("cookies")) << message_;
+ ASSERT_TRUE(RunExtensionTest("cookies/api")) << message_;
+}
+
+IN_PROC_BROWSER_TEST_F(ExtensionApiTest, CookiesEvents) {
+ CommandLine::ForCurrentProcess()->AppendSwitch(
+ switches::kEnableExperimentalExtensionApis);
+
+ ASSERT_TRUE(RunExtensionTest("cookies/events")) << message_;
}
diff --git a/chrome/browser/extensions/extensions_service.cc b/chrome/browser/extensions/extensions_service.cc
index 1837d15..d29d5c6 100644
--- a/chrome/browser/extensions/extensions_service.cc
+++ b/chrome/browser/extensions/extensions_service.cc
@@ -20,6 +20,7 @@
#include "chrome/browser/extensions/extension_accessibility_api.h"
#include "chrome/browser/extensions/extension_bookmarks_module.h"
#include "chrome/browser/extensions/extension_browser_event_router.h"
+#include "chrome/browser/extensions/extension_cookies_api.h"
#include "chrome/browser/extensions/extension_data_deleter.h"
#include "chrome/browser/extensions/extension_dom_ui.h"
#include "chrome/browser/extensions/extension_error_reporter.h"
@@ -178,6 +179,7 @@ void ExtensionsService::InitEventRouters() {
ExtensionBrowserEventRouter::GetInstance()->Init();
ExtensionBookmarkEventRouter::GetSingleton()->Observe(
profile_->GetBookmarkModel());
+ ExtensionCookiesEventRouter::GetInstance()->Init();
}
void ExtensionsService::Init() {