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 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
41 content::Source<Profile>(profile_->GetOriginalProfile())); | |
42 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
43 content::Source<Profile>(profile_->GetOriginalProfile())); | |
44 // TODO(dcheng): This is not testable. We need to allow handler_ to be | |
45 // dependency injected. | |
46 const ExtensionSet* extensions = | |
dcheng
2012/08/09 20:43:54
It's unclear from the comments whether this is onl
| |
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 ProfileSyncServiceFactory::GetForProfile(profile_), | |
58 this, | |
59 push_messaging_extensions)); | |
60 } | |
61 | |
62 void PushMessagingEventRouter::Shutdown() { | |
63 handler_.reset(); | |
64 } | |
65 | |
66 void PushMessagingEventRouter::SetMapperForTest( | |
67 scoped_ptr<PushMessagingInvalidationMapper> mapper) { | |
68 handler_ = mapper.Pass(); | |
28 } | 69 } |
29 | 70 |
30 void PushMessagingEventRouter::OnMessage(const std::string& extension_id, | 71 void PushMessagingEventRouter::OnMessage(const std::string& extension_id, |
31 int subchannel, | 72 int subchannel, |
32 const std::string& payload) { | 73 const std::string& payload) { |
33 glue::Message message; | 74 glue::Message message; |
34 message.subchannel_id = subchannel; | 75 message.subchannel_id = subchannel; |
35 message.payload = payload; | 76 message.payload = payload; |
36 | 77 |
37 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); | 78 scoped_ptr<base::ListValue> args(glue::OnMessage::Create(message)); |
38 profile_->GetExtensionEventRouter()->DispatchEventToExtension( | 79 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
39 extension_id, | 80 extension_id, |
40 event_names::kOnPushMessage, | 81 event_names::kOnPushMessage, |
41 args.Pass(), | 82 args.Pass(), |
42 profile_, | 83 profile_, |
43 GURL()); | 84 GURL()); |
44 } | 85 } |
45 | 86 |
87 void PushMessagingEventRouter::Observe( | |
88 int type, | |
89 const content::NotificationSource& source, | |
90 const content::NotificationDetails& details) { | |
91 switch (type) { | |
92 case chrome::NOTIFICATION_EXTENSION_LOADED: { | |
93 const Extension* extension = content::Details<Extension>(details).ptr(); | |
94 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) { | |
95 handler_->RegisterExtension(extension->id()); | |
96 } | |
97 break; | |
98 } | |
99 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | |
dcheng
2012/08/09 20:43:54
I believe this doesn't get called when a Profile i
| |
100 // TODO(dcheng): Does this get dispatched when shutting down? Should be | |
101 // no-op in that case. | |
102 const Extension* extension = | |
103 content::Details<UnloadedExtensionInfo>(details)->extension; | |
104 if (extension->HasAPIPermission(APIPermission::kPushMessaging)) { | |
105 handler_->UnregisterExtension(extension->id()); | |
106 } | |
107 break; | |
108 } | |
109 default: | |
110 NOTREACHED(); | |
111 } | |
112 } | |
113 | |
46 } // namespace extensions | 114 } // namespace extensions |
OLD | NEW |