Index: chrome/browser/extensions/api/push_messaging/push_messaging_api.cc |
diff --git a/chrome/browser/extensions/api/push_messaging/push_messaging_api.cc b/chrome/browser/extensions/api/push_messaging/push_messaging_api.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3f910fe91648a1e1b26a0cf829e40a9c0c6fe5ed |
--- /dev/null |
+++ b/chrome/browser/extensions/api/push_messaging/push_messaging_api.cc |
@@ -0,0 +1,65 @@ |
+// 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. |
+ |
+#include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" |
+ |
+#include "base/bind.h" |
+#include "base/json/json_writer.h" |
+#include "base/values.h" |
+#include "chrome/browser/extensions/event_router.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "content/public/browser/browser_thread.h" |
+#include "googleurl/src/gurl.h" |
+ |
+using content::BrowserThread; |
+ |
+namespace extensions { |
+ |
+namespace { |
+const char kEventName[] = "experimental.pushMessaging.onMessage"; |
+// Keys for JSON dictionary for the onMessage event. |
+const char kPayloadKey[] = "payload"; |
+const char kSubchannelKey[] = "subchannel"; |
+} // namespace |
+ |
+ExtensionPushMessagingEventRouter::ExtensionPushMessagingEventRouter( |
+ Profile* profile) |
+ : profile_(profile) { |
+} |
+ |
+ExtensionPushMessagingEventRouter::~ExtensionPushMessagingEventRouter() {} |
+ |
+void ExtensionPushMessagingEventRouter::Init() { |
+ // TODO(dcheng): Add hooks into InvalidationHandler when landed. |
+} |
+ |
+void ExtensionPushMessagingEventRouter::TriggerMessageForTest( |
+ const std::string& extension_id, |
+ int subchannel, |
+ const std::string& payload) { |
+ OnMessage(extension_id, subchannel, payload); |
+} |
+ |
+void ExtensionPushMessagingEventRouter::OnMessage( |
+ const std::string& extension_id, |
+ int subchannel, |
+ const std::string& payload) { |
+ ListValue args; |
Munjal (Google)
2012/07/31 17:36:12
Define args closer to its first use; before args.A
dcheng
2012/07/31 19:04:11
Done.
|
+ DictionaryValue* dict = new DictionaryValue; |
+ dict->SetInteger(kSubchannelKey, subchannel); |
+ dict->SetString(kPayloadKey, payload); |
+ |
+ args.Append(dict); |
+ |
+ std::string json_args; |
+ base::JSONWriter::Write(&args, &json_args); |
+ profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
+ extension_id, |
Munjal (Google)
2012/07/31 17:36:12
Nit: indentation.
dcheng
2012/07/31 19:04:11
Done.
|
+ kEventName, |
+ json_args, |
+ profile_, |
+ GURL()); |
+} |
+ |
+} // namespace extensions |