OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" | 5 #include "chrome/browser/extensions/api/push_messaging/push_messaging_api.h" |
6 | 6 |
| 7 #include <set> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/logging.h" |
8 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/extensions/api/push_messaging/push_messaging_invalidati
on_handler.h" |
9 #include "chrome/browser/extensions/event_names.h" | 13 #include "chrome/browser/extensions/event_names.h" |
10 #include "chrome/browser/extensions/event_router.h" | 14 #include "chrome/browser/extensions/event_router.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/extension_system.h" |
11 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/sync/profile_sync_service.h" |
| 19 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 20 #include "chrome/common/chrome_notification_types.h" |
12 #include "chrome/common/extensions/api/experimental_push_messaging.h" | 21 #include "chrome/common/extensions/api/experimental_push_messaging.h" |
13 #include "content/public/browser/browser_thread.h" | 22 #include "chrome/common/extensions/extension.h" |
| 23 #include "chrome/common/extensions/extension_set.h" |
| 24 #include "chrome/common/extensions/permissions/api_permission.h" |
| 25 #include "content/public/browser/notification_details.h" |
| 26 #include "content/public/browser/notification_source.h" |
14 #include "googleurl/src/gurl.h" | 27 #include "googleurl/src/gurl.h" |
15 | 28 |
16 namespace extensions { | 29 namespace extensions { |
17 | 30 |
18 namespace glue = extensions::api::experimental_push_messaging; | 31 namespace glue = api::experimental_push_messaging; |
19 | 32 |
20 PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile) | 33 PushMessagingEventRouter::PushMessagingEventRouter(Profile* profile) |
21 : profile_(profile) { | 34 : profile_(profile) { |
22 } | 35 } |
23 | 36 |
24 PushMessagingEventRouter::~PushMessagingEventRouter() {} | 37 PushMessagingEventRouter::~PushMessagingEventRouter() {} |
25 | 38 |
26 void PushMessagingEventRouter::Init() { | 39 void PushMessagingEventRouter::Init() { |
27 // TODO(dcheng): Add hooks into InvalidationHandler when landed. | 40 ProfileSyncService* pss = ProfileSyncServiceFactory::GetForProfile(profile_); |
| 41 // This may be NULL; for example, for the ChromeOS guest user. In these cases, |
| 42 // just return without setting up anything, since it won't work anyway. |
| 43 if (!pss) |
| 44 return; |
| 45 |
| 46 const ExtensionSet* extensions = |
| 47 ExtensionSystem::Get(profile_)->extension_service()->extensions(); |
| 48 std::set<std::string> push_messaging_extensions; |
| 49 for (ExtensionSet::const_iterator it = extensions->begin(); |
| 50 it != extensions->end(); ++it) { |
| 51 const Extension* extension = *it; |
| 52 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) { |
| 53 push_messaging_extensions.insert(extension->id()); |
| 54 } |
| 55 } |
| 56 handler_.reset(new PushMessagingInvalidationHandler( |
| 57 pss, this, push_messaging_extensions)); |
| 58 |
| 59 // Register for extension load/unload as well, so we can update any |
| 60 // registrations as appropriate. |
| 61 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 62 content::Source<Profile>(profile_->GetOriginalProfile())); |
| 63 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 64 content::Source<Profile>(profile_->GetOriginalProfile())); |
| 65 } |
| 66 |
| 67 void PushMessagingEventRouter::Shutdown() { |
| 68 // We need an explicit Shutdown() due to the dependencies among the various |
| 69 // ProfileKeyedServices. ProfileSyncService depends on ExtensionSystem, so |
| 70 // it is destroyed before us in the destruction phase of Profile shutdown. |
| 71 // As a result, we need to drop any references to it in the Shutdown() phase |
| 72 // instead. |
| 73 handler_.reset(); |
| 74 } |
| 75 |
| 76 void PushMessagingEventRouter::SetMapperForTest( |
| 77 scoped_ptr<PushMessagingInvalidationMapper> mapper) { |
| 78 handler_ = mapper.Pass(); |
| 79 } |
| 80 |
| 81 void PushMessagingEventRouter::TriggerMessageForTest( |
| 82 const std::string& extension_id, |
| 83 int subchannel, |
| 84 const std::string& payload) { |
| 85 OnMessage(extension_id, subchannel, payload); |
28 } | 86 } |
29 | 87 |
30 void PushMessagingEventRouter::OnMessage(const std::string& extension_id, | 88 void PushMessagingEventRouter::OnMessage(const std::string& extension_id, |
31 int subchannel, | 89 int subchannel, |
32 const std::string& payload) { | 90 const std::string& payload) { |
33 glue::Message message; | 91 glue::Message message; |
34 message.subchannel_id = subchannel; | 92 message.subchannel_id = subchannel; |
35 message.payload = payload; | 93 message.payload = payload; |
36 | 94 |
37 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); | 95 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); |
38 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | 96 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
39 extension_id, | 97 extension_id, |
40 event_names::kOnPushMessage, | 98 event_names::kOnPushMessage, |
41 args.Pass(), | 99 args.Pass(), |
42 profile_, | 100 profile_, |
43 GURL()); | 101 GURL()); |
44 } | 102 } |
45 | 103 |
| 104 void PushMessagingEventRouter::Observe( |
| 105 int type, |
| 106 const content::NotificationSource& source, |
| 107 const content::NotificationDetails& details) { |
| 108 switch (type) { |
| 109 case chrome::NOTIFICATION_EXTENSION_LOADED: { |
| 110 const Extension* extension = content::Details<Extension>(details).ptr(); |
| 111 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) { |
| 112 handler_->RegisterExtension(extension->id()); |
| 113 } |
| 114 break; |
| 115 } |
| 116 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 117 const Extension* extension = |
| 118 content::Details<UnloadedExtensionInfo>(details)->extension; |
| 119 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) { |
| 120 handler_->UnregisterExtension(extension->id()); |
| 121 } |
| 122 break; |
| 123 } |
| 124 default: |
| 125 NOTREACHED(); |
| 126 } |
| 127 } |
| 128 |
46 } // namespace extensions | 129 } // namespace extensions |
OLD | NEW |