Chromium Code Reviews| 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..3eb0542fbe1f98eb3ef8e1c51fde5aa7ad477963 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/push_messaging/push_messaging_api.cc |
| @@ -0,0 +1,58 @@ |
| +// 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"; |
|
Mihai Parparita -not on Chrome
2012/08/01 20:05:50
The extensions infrastructure now supports auto-ge
dcheng
2012/08/01 21:15:33
Done. This is really neat!
|
| +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::OnMessage( |
| + const std::string& extension_id, |
| + int subchannel, |
| + const std::string& payload) { |
| + DictionaryValue* dict = new DictionaryValue; |
| + dict->SetInteger(kSubchannelKey, subchannel); |
| + dict->SetString(kPayloadKey, payload); |
| + |
| + ListValue args; |
| + args.Append(dict); |
| + std::string json_args; |
| + base::JSONWriter::Write(&args, &json_args); |
| + |
| + profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| + extension_id, |
| + kEventName, |
| + json_args, |
| + profile_, |
| + GURL()); |
| +} |
| + |
| +} // namespace extensions |