OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/json/json_writer.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/api/push_messaging/invalidation_handler.h" |
| 11 #include "chrome/browser/extensions/event_router.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "content/public/browser/browser_thread.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 |
| 16 using content::BrowserThread; |
| 17 |
| 18 namespace extensions { |
| 19 |
| 20 namespace { |
| 21 const char kEventName[] = "experimental.pushMessaging.onMessage"; |
| 22 // Keys for JSON dictionary for the onMessage event. |
| 23 const char kPayloadKey[] = "payload"; |
| 24 const char kSubchannelKey[] = "subchannel"; |
| 25 } // namespace |
| 26 |
| 27 ExtensionPushMessagingEventRouter::ExtensionPushMessagingEventRouter( |
| 28 Profile* profile) |
| 29 : profile_(profile) { |
| 30 } |
| 31 |
| 32 ExtensionPushMessagingEventRouter::~ExtensionPushMessagingEventRouter() {} |
| 33 |
| 34 void ExtensionPushMessagingEventRouter::Init() { |
| 35 // Hook into PSS here with InvalidationHandler. |
| 36 handler_.reset(new InvalidationHandler(this)); |
| 37 } |
| 38 |
| 39 void ExtensionPushMessagingEventRouter::TriggerMessageForTest( |
| 40 const std::string& extension_id, |
| 41 int subchannel, |
| 42 const std::string& payload) { |
| 43 OnMessage(extension_id, subchannel, payload); |
| 44 } |
| 45 |
| 46 void ExtensionPushMessagingEventRouter::OnMessage( |
| 47 const std::string& extension_id, |
| 48 int subchannel, |
| 49 const std::string& payload) { |
| 50 ListValue args; |
| 51 DictionaryValue* dict = new DictionaryValue; |
| 52 dict->SetInteger(kSubchannelKey, subchannel); |
| 53 dict->SetString(kPayloadKey, payload); |
| 54 |
| 55 args.Append(dict); |
| 56 |
| 57 std::string json_args; |
| 58 base::JSONWriter::Write(&args, &json_args); |
| 59 profile_->GetExtensionEventRouter()->DispatchEventToExtension( |
| 60 extension_id, |
| 61 kEventName, |
| 62 json_args, |
| 63 profile_, |
| 64 GURL()); |
| 65 } |
| 66 |
| 67 } // namespace extensions |
OLD | NEW |