Chromium Code Reviews| 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 "chrome/browser/services/gcm/gcm_profile_service.h" | 10 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
| 11 #include "components/gcm_driver/gcm_driver.h" | 11 #include "components/gcm_driver/gcm_driver.h" |
| 12 | 12 |
| 13 namespace gcm { | 13 namespace gcm { |
| 14 | 14 |
| 15 PushMessagingServiceImpl::PushMessagingServiceImpl( | 15 PushMessagingServiceImpl::PushMessagingServiceImpl( |
| 16 GCMProfileService* gcm_profile_service) | 16 GCMProfileService* gcm_profile_service) |
| 17 : gcm_profile_service_(gcm_profile_service), | 17 : gcm_profile_service_(gcm_profile_service), |
| 18 weak_factory_(this) {} | 18 weak_factory_(this) { |
| 19 // TODO(mvanouwerkerk): Automatically unregister Service Workers from GCM when | |
| 20 // they are uninstalled. | |
| 21 } | |
| 19 | 22 |
| 20 PushMessagingServiceImpl::~PushMessagingServiceImpl() {} | 23 PushMessagingServiceImpl::~PushMessagingServiceImpl() { |
| 24 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver, | |
| 25 // then we should RemoveAppHandler all our app handlers. | |
|
fgorski
2014/06/10 17:09:27
Perhaps you can do that in Shutdown app handler. U
johnme
2014/06/10 20:33:48
Not sure, I based this on ExtensionGCMAppHandler.
| |
| 26 } | |
| 27 | |
| 28 base::WeakPtr<PushMessagingServiceImpl> PushMessagingServiceImpl::GetWeakPtr() { | |
| 29 return weak_factory_.GetWeakPtr(); | |
| 30 } | |
| 31 | |
| 32 void PushMessagingServiceImpl::LazyLoadAppHandlers() { | |
| 33 // TODO(mvanouwerkerk): For each Service Worker that is able to receive push | |
| 34 // events, run gcm_profile_service_->driver()->AddAppHandler(sw_id, this) | |
| 35 } | |
| 36 | |
| 37 void PushMessagingGCMAppHandler::ShutdownHandler() { | |
| 38 // TODO(mvanouwerkerk): Do any necessary cleanup. | |
| 39 } | |
| 40 | |
| 41 void PushMessagingGCMAppHandler::OnMessage( | |
| 42 const std::string& app_id, | |
| 43 const GCMClient::IncomingMessage& message) { | |
| 44 // The Push API only exposes a single string of data in the push event fired | |
| 45 // on the Service Worker. When developers send messages using GCM to the Push | |
| 46 // API, they must pass a single key-value pair, where the key is "data" and | |
| 47 // the value is the string they want to be passed to their Service Worker. | |
| 48 // For example, they could send the following JSON using the HTTPS GCM API: | |
| 49 // { | |
| 50 // "registration_ids": ["FOO", "BAR"], | |
| 51 // "data": { | |
| 52 // "data": "BAZ", | |
|
fgorski
2014/06/10 17:09:27
this looks redundant... is that a work in progress
johnme
2014/06/10 20:33:48
There are no standards around the GCM part. The Pu
| |
| 53 // }, | |
| 54 // "delay_while_idle": true, | |
| 55 // } | |
| 56 // TODO(johnme): Make sure this is clearly documented for developers. | |
| 57 const GCMClient::MessageData::iterator it = message.data.find("data"); | |
| 58 if (it != message.data.end()) { | |
| 59 const std::string& data = it->second; | |
| 60 // TODO(mvanouwerkerk): Fire push event with data on the Service Worker | |
| 61 // corresponding to app_id. | |
| 62 } else { | |
| 63 // Drop the message, as it is invalid. | |
| 64 // TODO(mvanouwerkerk): Show a warning in the developer console of the | |
| 65 // Service Worker corresponding to app_id. | |
|
fgorski
2014/06/10 17:09:27
Add a TODO for diagnostic observers (you likely wa
johnme
2014/06/10 20:33:48
Done.
| |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void PushMessagingGCMAppHandler::OnMessagesDeleted(const std::string& app_id) { | |
| 70 // TODO(mvanouwerkerk): Fire push error event on the Service Worker | |
|
fgorski
2014/06/10 17:09:27
Are you sure this deserves a semantic of an error?
johnme
2014/06/10 20:33:48
Last time we discussed the spec, there seemed to b
| |
| 71 // corresponding to app_id. | |
| 72 } | |
| 73 | |
| 74 void PushMessagingGCMAppHandler::OnSendError( | |
| 75 const std::string& app_id, | |
| 76 const GCMClient::SendErrorDetails& send_error_details) { | |
| 77 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | |
| 78 } | |
| 21 | 79 |
| 22 void PushMessagingServiceImpl::Register( | 80 void PushMessagingServiceImpl::Register( |
| 23 const std::string& app_id, | 81 const std::string& app_id, |
| 24 const std::string& sender_id, | 82 const std::string& sender_id, |
| 25 const content::PushMessagingService::RegisterCallback& callback) { | 83 const content::PushMessagingService::RegisterCallback& callback) { |
| 26 // The GCMDriver could be NULL if GCMProfileService has been shut down. | 84 // The GCMDriver could be NULL if GCMProfileService has been shut down. |
| 27 if (!gcm_profile_service_->driver()) | 85 if (!gcm_profile_service_->driver()) |
| 28 return; | 86 return; |
| 29 std::vector<std::string> sender_ids(1, sender_id); | 87 std::vector<std::string> sender_ids(1, sender_id); |
| 30 gcm_profile_service_->driver()->Register( | 88 gcm_profile_service_->driver()->Register( |
| 31 app_id, | 89 app_id, |
| 32 sender_ids, | 90 sender_ids, |
| 33 base::Bind(&PushMessagingServiceImpl::DidRegister, | 91 base::Bind(&PushMessagingServiceImpl::DidRegister, |
| 34 weak_factory_.GetWeakPtr(), | 92 GetWeakPtr(), |
| 93 app_id, | |
| 35 callback)); | 94 callback)); |
| 36 } | 95 } |
| 37 | 96 |
| 38 void PushMessagingServiceImpl::DidRegister( | 97 void PushMessagingServiceImpl::DidRegister( |
| 98 const std::string& app_id, | |
| 39 const content::PushMessagingService::RegisterCallback& callback, | 99 const content::PushMessagingService::RegisterCallback& callback, |
| 40 const std::string& registration_id, | 100 const std::string& registration_id, |
| 41 GCMClient::Result result) { | 101 GCMClient::Result result) { |
| 42 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); | 102 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); |
| 43 bool error = (result != GCMClient::SUCCESS); | 103 bool error = (result != GCMClient::SUCCESS); |
| 44 callback.Run(endpoint, registration_id, error); | 104 callback.Run(endpoint, registration_id, error); |
| 105 if (!error) | |
| 106 gcm_profile_service_->driver()->AddAppHandler(app_id, this); | |
| 45 } | 107 } |
| 46 | 108 |
| 109 // TODO(johnme): Unregister should call RemoveAppHandler before unregistering. | |
| 110 | |
| 47 } // namespace gcm | 111 } // namespace gcm |
| OLD | NEW |