OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/services/gcm/push_messaging_service_impl.h" | 5 #include "chrome/browser/services/gcm/push_messaging_service_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | |
11 #include "base/prefs/pref_service.h" | |
12 #include "base/strings/string_util.h" | |
13 #include "chrome/browser/profiles/profile.h" | |
10 #include "chrome/browser/services/gcm/gcm_profile_service.h" | 14 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
15 #include "chrome/browser/services/gcm/gcm_profile_service_factory.h" | |
16 #include "chrome/common/chrome_switches.h" | |
17 #include "chrome/common/pref_names.h" | |
11 #include "components/gcm_driver/gcm_driver.h" | 18 #include "components/gcm_driver/gcm_driver.h" |
19 #include "components/pref_registry/pref_registry_syncable.h" | |
12 | 20 |
13 namespace gcm { | 21 namespace gcm { |
14 | 22 |
23 namespace { | |
24 const std::string kAppIdPrefix = "push:"; | |
25 } // namespace | |
26 | |
27 // static | |
28 void PushMessagingServiceImpl::RegisterProfilePrefs( | |
29 user_prefs::PrefRegistrySyncable* registry) { | |
30 registry->RegisterIntegerPref( | |
31 prefs::kPushMessagingRegistrationCount, | |
32 0, | |
33 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
34 } | |
35 | |
36 // static | |
37 void PushMessagingServiceImpl::InitializeForProfile(Profile* profile) { | |
fgorski
2014/06/17 17:24:00
Is there any chance that this function gets called
Michael van Ouwerkerk
2014/06/17 19:22:05
Good question. I checked this, and we don't run in
| |
38 // TODO(mvanouwerkerk): Make sure to remove this check at the same time as | |
39 // push graduates from experimental in Blink. | |
40 if (!CommandLine::ForCurrentProcess()->HasSwitch( | |
41 switches::kEnableExperimentalWebPlatformFeatures)) { | |
fgorski
2014/06/17 17:24:00
nit: fix alignment
Michael van Ouwerkerk
2014/06/17 19:22:05
What is wrong with it? I figured a wrapped line sh
| |
42 return; | |
43 } | |
44 // TODO(johnme): Consider whether push should be enabled in incognito. If it | |
45 // does get enabled, then be careful that you're reading the pref from the | |
46 // right profile, as prefs defined in a regular profile are visible in the | |
47 // corresponding incognito profile unless overrriden. | |
48 if (!profile || profile->IsOffTheRecord() || profile->GetPrefs()->GetInteger( | |
49 prefs::kPushMessagingRegistrationCount) <= 0) | |
50 return; | |
51 // Create the GCMProfileService, and hence intiantiate this class. | |
fgorski
2014/06/17 17:24:00
typo in instantiate
Michael van Ouwerkerk
2014/06/17 19:22:05
Done.
| |
52 GCMProfileService* gcm_service = gcm::GCMProfileServiceFactory::GetForProfile( | |
53 profile); | |
54 PushMessagingServiceImpl* push_service = | |
55 static_cast<PushMessagingServiceImpl*>( | |
56 gcm_service->push_messaging_service()); | |
57 // Register ourselves as an app handler. | |
58 gcm_service->driver()->AddAppHandler(kAppIdPrefix, push_service); | |
59 } | |
60 | |
15 PushMessagingServiceImpl::PushMessagingServiceImpl( | 61 PushMessagingServiceImpl::PushMessagingServiceImpl( |
16 GCMProfileService* gcm_profile_service) | 62 GCMProfileService* gcm_profile_service, |
63 Profile* profile) | |
17 : gcm_profile_service_(gcm_profile_service), | 64 : gcm_profile_service_(gcm_profile_service), |
18 weak_factory_(this) {} | 65 profile_(profile), |
66 weak_factory_(this) { | |
67 // TODO(mvanouwerkerk): Automatically unregister Service Workers from GCM when | |
68 // they are uninstalled. | |
69 } | |
19 | 70 |
20 PushMessagingServiceImpl::~PushMessagingServiceImpl() {} | 71 PushMessagingServiceImpl::~PushMessagingServiceImpl() { |
72 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver, | |
73 // then we should call RemoveAppHandler. | |
74 } | |
75 | |
76 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { | |
77 // TODO(mvanouwerkerk): Finalize and centralize format of Push API app_id. | |
78 return StartsWithASCII(app_id, kAppIdPrefix, true); | |
79 } | |
80 | |
81 void PushMessagingServiceImpl::ShutdownHandler() { | |
82 // TODO(johnme): Do any necessary cleanup. | |
83 } | |
84 | |
85 void PushMessagingServiceImpl::OnMessage( | |
86 const std::string& app_id, | |
87 const GCMClient::IncomingMessage& message) { | |
88 // The Push API only exposes a single string of data in the push event fired | |
89 // on the Service Worker. When developers send messages using GCM to the Push | |
90 // API, they must pass a single key-value pair, where the key is "data" and | |
91 // the value is the string they want to be passed to their Service Worker. | |
92 // For example, they could send the following JSON using the HTTPS GCM API: | |
93 // { | |
94 // "registration_ids": ["FOO", "BAR"], | |
95 // "data": { | |
96 // "data": "BAZ", | |
97 // }, | |
98 // "delay_while_idle": true, | |
99 // } | |
100 // TODO(johnme): Make sure this is clearly documented for developers. | |
101 GCMClient::MessageData::const_iterator it = message.data.find("data"); | |
102 if (it != message.data.end()) { | |
103 const std::string& data ALLOW_UNUSED = it->second; | |
104 // TODO(mvanouwerkerk): Fire push event with data on the Service Worker | |
105 // corresponding to app_id (and remove ALLOW_UNUSED above). | |
106 } else { | |
107 // Drop the message, as it is invalid. | |
108 // TODO(mvanouwerkerk): Show a warning in the developer console of the | |
109 // Service Worker corresponding to app_id. | |
110 // TODO(johnme): Add diagnostic observers (e.g. UMA and an internals page) | |
111 // to know when bad things happen. | |
112 } | |
113 } | |
114 | |
115 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { | |
116 // TODO(mvanouwerkerk): Fire push error event on the Service Worker | |
117 // corresponding to app_id. | |
118 } | |
119 | |
120 void PushMessagingServiceImpl::OnSendError( | |
121 const std::string& app_id, | |
122 const GCMClient::SendErrorDetails& send_error_details) { | |
123 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | |
124 } | |
21 | 125 |
22 void PushMessagingServiceImpl::Register( | 126 void PushMessagingServiceImpl::Register( |
23 const std::string& app_id, | 127 const std::string& app_id, |
24 const std::string& sender_id, | 128 const std::string& sender_id, |
25 const content::PushMessagingService::RegisterCallback& callback) { | 129 const content::PushMessagingService::RegisterCallback& callback) { |
26 // The GCMDriver could be NULL if GCMProfileService has been shut down. | 130 // The GCMDriver could be NULL if GCMProfileService has been shut down. |
27 if (!gcm_profile_service_->driver()) | 131 if (!gcm_profile_service_->driver()) { |
132 DidRegister(app_id, callback, "", GCMClient::UNKNOWN_ERROR); | |
fgorski
2014/06/17 17:24:00
Perhaps GCMClient::GCM_DISABLED is more suitable,
Michael van Ouwerkerk
2014/06/17 19:22:05
Done.
| |
28 return; | 133 return; |
134 } | |
29 std::vector<std::string> sender_ids(1, sender_id); | 135 std::vector<std::string> sender_ids(1, sender_id); |
30 gcm_profile_service_->driver()->Register( | 136 gcm_profile_service_->driver()->Register( |
31 app_id, | 137 app_id, |
32 sender_ids, | 138 sender_ids, |
33 base::Bind(&PushMessagingServiceImpl::DidRegister, | 139 base::Bind(&PushMessagingServiceImpl::DidRegister, |
34 weak_factory_.GetWeakPtr(), | 140 GetWeakPtr(), |
141 app_id, | |
35 callback)); | 142 callback)); |
36 } | 143 } |
37 | 144 |
38 void PushMessagingServiceImpl::DidRegister( | 145 void PushMessagingServiceImpl::DidRegister( |
146 const std::string& app_id, | |
39 const content::PushMessagingService::RegisterCallback& callback, | 147 const content::PushMessagingService::RegisterCallback& callback, |
40 const std::string& registration_id, | 148 const std::string& registration_id, |
41 GCMClient::Result result) { | 149 GCMClient::Result result) { |
42 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); | 150 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); |
43 callback.Run(endpoint, registration_id, result == GCMClient::SUCCESS); | 151 bool success = (result == GCMClient::SUCCESS); |
152 callback.Run(endpoint, registration_id, success); | |
153 if (success) { | |
154 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. | |
155 int old_count = profile_->GetPrefs()->GetInteger( | |
156 prefs::kPushMessagingRegistrationCount); | |
157 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | |
158 old_count + 1); | |
159 if (old_count == 0) | |
160 gcm_profile_service_->driver()->AddAppHandler(kAppIdPrefix, this); | |
161 } | |
162 } | |
163 | |
164 // TODO(johnme): Unregister should decrement the pref, and call | |
165 // RemoveAppHandler if the count drops to zero. | |
166 | |
167 base::WeakPtr<PushMessagingServiceImpl> PushMessagingServiceImpl::GetWeakPtr() { | |
168 return weak_factory_.GetWeakPtr(); | |
44 } | 169 } |
45 | 170 |
46 } // namespace gcm | 171 } // namespace gcm |
OLD | NEW |