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 "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 static const char kAppIdPrefix[] = "push:"; | |
|
jochen (gone - plz use gerrit)
2014/06/18 03:58:40
no static
Michael van Ouwerkerk
2014/06/18 11:17:51
Done.
| |
| 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) { | |
| 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)) { | |
| 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; | |
|
jochen (gone - plz use gerrit)
2014/06/18 03:58:40
nit { }
Michael van Ouwerkerk
2014/06/18 11:17:51
Done.
| |
| 51 // Create the GCMProfileService, and hence instantiate this class. | |
| 52 GCMProfileService* gcm_service = gcm::GCMProfileServiceFactory::GetForProfile( | |
|
jochen (gone - plz use gerrit)
2014/06/18 03:58:40
is this clang-formatted?
Michael van Ouwerkerk
2014/06/18 11:17:51
Done.
| |
| 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 } | |
| 19 | 68 |
| 20 PushMessagingServiceImpl::~PushMessagingServiceImpl() {} | 69 PushMessagingServiceImpl::~PushMessagingServiceImpl() { |
| 70 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver, | |
| 71 // then we should call RemoveAppHandler. | |
| 72 } | |
| 73 | |
| 74 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const { | |
| 75 // TODO(mvanouwerkerk): Finalize and centralize format of Push API app_id. | |
| 76 return StartsWithASCII(app_id, kAppIdPrefix, true); | |
| 77 } | |
| 78 | |
| 79 void PushMessagingServiceImpl::ShutdownHandler() { | |
| 80 // TODO(johnme): Do any necessary cleanup. | |
| 81 } | |
| 82 | |
| 83 void PushMessagingServiceImpl::OnMessage( | |
| 84 const std::string& app_id, | |
| 85 const GCMClient::IncomingMessage& message) { | |
| 86 // The Push API only exposes a single string of data in the push event fired | |
| 87 // on the Service Worker. When developers send messages using GCM to the Push | |
| 88 // API, they must pass a single key-value pair, where the key is "data" and | |
| 89 // the value is the string they want to be passed to their Service Worker. | |
| 90 // For example, they could send the following JSON using the HTTPS GCM API: | |
| 91 // { | |
| 92 // "registration_ids": ["FOO", "BAR"], | |
| 93 // "data": { | |
| 94 // "data": "BAZ", | |
| 95 // }, | |
| 96 // "delay_while_idle": true, | |
| 97 // } | |
| 98 // TODO(johnme): Make sure this is clearly documented for developers. | |
| 99 GCMClient::MessageData::const_iterator it = message.data.find("data"); | |
| 100 if (it != message.data.end()) { | |
| 101 const std::string& data ALLOW_UNUSED = it->second; | |
| 102 // TODO(mvanouwerkerk): Fire push event with data on the Service Worker | |
| 103 // corresponding to app_id (and remove ALLOW_UNUSED above). | |
| 104 } else { | |
| 105 // Drop the message, as it is invalid. | |
| 106 // TODO(mvanouwerkerk): Show a warning in the developer console of the | |
| 107 // Service Worker corresponding to app_id. | |
| 108 // TODO(johnme): Add diagnostic observers (e.g. UMA and an internals page) | |
| 109 // to know when bad things happen. | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) { | |
| 114 // TODO(mvanouwerkerk): Fire push error event on the Service Worker | |
| 115 // corresponding to app_id. | |
| 116 } | |
| 117 | |
| 118 void PushMessagingServiceImpl::OnSendError( | |
| 119 const std::string& app_id, | |
| 120 const GCMClient::SendErrorDetails& send_error_details) { | |
| 121 NOTREACHED() << "The Push API shouldn't have sent messages upstream"; | |
| 122 } | |
| 21 | 123 |
| 22 void PushMessagingServiceImpl::Register( | 124 void PushMessagingServiceImpl::Register( |
| 23 const std::string& app_id, | 125 const std::string& app_id, |
| 24 const std::string& sender_id, | 126 const std::string& sender_id, |
| 25 const content::PushMessagingService::RegisterCallback& callback) { | 127 const content::PushMessagingService::RegisterCallback& callback) { |
| 26 // The GCMDriver could be NULL if GCMProfileService has been shut down. | 128 // The GCMDriver could be NULL if GCMProfileService has been shut down. |
| 27 if (!gcm_profile_service_->driver()) | 129 if (!gcm_profile_service_->driver()) { |
| 130 DidRegister(app_id, callback, "", GCMClient::GCM_DISABLED); | |
|
jianli
2014/06/17 22:05:17
It is not about a different error code we return.
Michael van Ouwerkerk
2014/06/18 11:17:51
Done.
| |
| 28 return; | 131 return; |
| 132 } | |
| 133 | |
| 134 // If this is registering for the first time then the driver does not have | |
| 135 // this as an app handler and registration would fail. | |
| 136 if (gcm_profile_service_->driver()->GetAppHandler(kAppIdPrefix) != this) | |
| 137 gcm_profile_service_->driver()->AddAppHandler(kAppIdPrefix, this); | |
| 138 | |
| 29 std::vector<std::string> sender_ids(1, sender_id); | 139 std::vector<std::string> sender_ids(1, sender_id); |
| 30 gcm_profile_service_->driver()->Register( | 140 gcm_profile_service_->driver()->Register( |
| 31 app_id, | 141 app_id, |
| 32 sender_ids, | 142 sender_ids, |
| 33 base::Bind(&PushMessagingServiceImpl::DidRegister, | 143 base::Bind(&PushMessagingServiceImpl::DidRegister, |
| 34 weak_factory_.GetWeakPtr(), | 144 weak_factory_.GetWeakPtr(), |
| 145 app_id, | |
| 35 callback)); | 146 callback)); |
| 36 } | 147 } |
| 37 | 148 |
| 38 void PushMessagingServiceImpl::DidRegister( | 149 void PushMessagingServiceImpl::DidRegister( |
| 150 const std::string& app_id, | |
| 39 const content::PushMessagingService::RegisterCallback& callback, | 151 const content::PushMessagingService::RegisterCallback& callback, |
| 40 const std::string& registration_id, | 152 const std::string& registration_id, |
| 41 GCMClient::Result result) { | 153 GCMClient::Result result) { |
| 42 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); | 154 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); |
| 43 callback.Run(endpoint, registration_id, result == GCMClient::SUCCESS); | 155 bool success = (result == GCMClient::SUCCESS); |
| 156 callback.Run(endpoint, registration_id, success); | |
| 157 if (success) { | |
| 158 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes. | |
| 159 int registration_count = profile_->GetPrefs()->GetInteger( | |
| 160 prefs::kPushMessagingRegistrationCount); | |
| 161 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount, | |
| 162 registration_count + 1); | |
|
jochen (gone - plz use gerrit)
2014/06/18 03:58:40
what do you do on overflow?
Michael van Ouwerkerk
2014/06/18 11:17:51
While I don't expect anyone will get that far, let
| |
| 163 } | |
| 44 } | 164 } |
| 45 | 165 |
| 166 // TODO(johnme): Unregister should decrement the pref, and call | |
| 167 // RemoveAppHandler if the count drops to zero. | |
| 168 | |
| 46 } // namespace gcm | 169 } // namespace gcm |
| OLD | NEW |