Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(74)

Side by Side Diff: chrome/browser/services/gcm/push_messaging_service_impl.cc

Issue 324913004: Skeleton GCMAppHandler for Push API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix compile Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // static
24 void PushMessagingServiceImpl::RegisterProfilePrefs(
25 user_prefs::PrefRegistrySyncable* registry) {
26 registry->RegisterIntegerPref(
27 prefs::kPushMessagingRegistrationCount,
28 0,
29 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
30 }
31
32 // static
33 void PushMessagingServiceImpl::InitializeForProfile(Profile* profile) {
34 // TODO(mvanouwerkerk): Make sure to remove this check at the same time as
35 // push graudates from experimental in Blink.
36 if (!CommandLine::ForCurrentProcess()->HasSwitch(
37 switches::kEnableExperimentalWebPlatformFeatures)) {
38 return;
39 }
40 // TODO(johnme): Consider whether push should be enabled in incognito. If it
41 // does get enabled, then be careful that you're reading the pref from the
42 // right profile, as prefs defined in a regular profile are visible in the
43 // corresponding incognito profile unless overrriden.
44 if (!profile || profile->IsOffTheRecord() || profile->GetPrefs()->GetInteger(
45 prefs::kPushMessagingRegistrationCount) <= 0)
46 return;
47 // Create the GCMProfileService, and hence intiantiate this class.
48 GCMProfileService* service = gcm::GCMProfileServiceFactory::GetForProfile(
49 profile);
50 PushMessagingServiceImpl* self = static_cast<PushMessagingServiceImpl*>(
51 service->push_messaging_service());
52 // Register ourselves as a wildcard app handler.
53 service->driver()->AddWildcardAppHandler(self);
54 }
55
15 PushMessagingServiceImpl::PushMessagingServiceImpl( 56 PushMessagingServiceImpl::PushMessagingServiceImpl(
16 GCMProfileService* gcm_profile_service) 57 GCMProfileService* gcm_profile_service,
58 Profile* profile)
17 : gcm_profile_service_(gcm_profile_service), 59 : gcm_profile_service_(gcm_profile_service),
18 weak_factory_(this) {} 60 profile_(profile),
61 weak_factory_(this) {
62 // TODO(mvanouwerkerk): Automatically unregister Service Workers from GCM when
63 // they are uninstalled.
64 }
19 65
20 PushMessagingServiceImpl::~PushMessagingServiceImpl() {} 66 PushMessagingServiceImpl::~PushMessagingServiceImpl() {
67 // TODO(johnme): If it's possible for this to be destroyed before GCMDriver,
68 // then we should call RemoveWildcardAppHandler.
69 }
70
71 bool PushMessagingServiceImpl::CanHandle(const std::string& app_id) const {
72 // TODO(johnme): Can we make this test more specific?
73 // Push API app_ids all start with https://.
74 return StartsWithASCII(app_id, "https://", true);
75 }
76
77 void PushMessagingServiceImpl::ShutdownHandler() {
78 // TODO(johnme): Do any necessary cleanup.
79 }
80
81 void PushMessagingServiceImpl::OnMessage(
82 const std::string& app_id,
83 const GCMClient::IncomingMessage& message) {
84 // The Push API only exposes a single string of data in the push event fired
85 // on the Service Worker. When developers send messages using GCM to the Push
86 // API, they must pass a single key-value pair, where the key is "data" and
87 // the value is the string they want to be passed to their Service Worker.
88 // For example, they could send the following JSON using the HTTPS GCM API:
89 // {
90 // "registration_ids": ["FOO", "BAR"],
91 // "data": {
92 // "data": "BAZ",
93 // },
94 // "delay_while_idle": true,
95 // }
96 // TODO(johnme): Make sure this is clearly documented for developers.
97 GCMClient::MessageData::const_iterator it = message.data.find("data");
98 if (it != message.data.end()) {
99 const std::string& data ALLOW_UNUSED = it->second;
100 // TODO(mvanouwerkerk): Fire push event with data on the Service Worker
101 // corresponding to app_id (and remove ALLOW_UNUSED above).
102 } else {
103 // Drop the message, as it is invalid.
104 // TODO(mvanouwerkerk): Show a warning in the developer console of the
105 // Service Worker corresponding to app_id.
106 // TODO(johnme): Add diagnostic observers (e.g. UMA and an internals page)
107 // to know when bad things happen.
108 }
109 }
110
111 void PushMessagingServiceImpl::OnMessagesDeleted(const std::string& app_id) {
112 // TODO(mvanouwerkerk): Fire push error event on the Service Worker
113 // corresponding to app_id.
114 }
115
116 void PushMessagingServiceImpl::OnSendError(
117 const std::string& app_id,
118 const GCMClient::SendErrorDetails& send_error_details) {
119 NOTREACHED() << "The Push API shouldn't have sent messages upstream";
120 }
21 121
22 void PushMessagingServiceImpl::Register( 122 void PushMessagingServiceImpl::Register(
23 const std::string& app_id, 123 const std::string& app_id,
24 const std::string& sender_id, 124 const std::string& sender_id,
25 const content::PushMessagingService::RegisterCallback& callback) { 125 const content::PushMessagingService::RegisterCallback& callback) {
26 // The GCMDriver could be NULL if GCMProfileService has been shut down. 126 // The GCMDriver could be NULL if GCMProfileService has been shut down.
27 if (!gcm_profile_service_->driver()) 127 if (!gcm_profile_service_->driver()) {
128 DidRegister(app_id, callback, "", GCMClient::UNKNOWN_ERROR);
28 return; 129 return;
130 }
29 std::vector<std::string> sender_ids(1, sender_id); 131 std::vector<std::string> sender_ids(1, sender_id);
30 gcm_profile_service_->driver()->Register( 132 gcm_profile_service_->driver()->Register(
31 app_id, 133 app_id,
32 sender_ids, 134 sender_ids,
33 base::Bind(&PushMessagingServiceImpl::DidRegister, 135 base::Bind(&PushMessagingServiceImpl::DidRegister,
34 weak_factory_.GetWeakPtr(), 136 GetWeakPtr(),
137 app_id,
35 callback)); 138 callback));
36 } 139 }
37 140
38 void PushMessagingServiceImpl::DidRegister( 141 void PushMessagingServiceImpl::DidRegister(
142 const std::string& app_id,
39 const content::PushMessagingService::RegisterCallback& callback, 143 const content::PushMessagingService::RegisterCallback& callback,
40 const std::string& registration_id, 144 const std::string& registration_id,
41 GCMClient::Result result) { 145 GCMClient::Result result) {
42 GURL endpoint = GURL("https://android.googleapis.com/gcm/send"); 146 GURL endpoint = GURL("https://android.googleapis.com/gcm/send");
43 callback.Run(endpoint, registration_id, result == GCMClient::SUCCESS); 147 bool success = (result == GCMClient::SUCCESS);
148 callback.Run(endpoint, registration_id, success);
149 if (success) {
150 // TODO(johnme): Make sure the pref doesn't get out of sync after crashes.
151 int old_count = profile_->GetPrefs()->GetInteger(
152 prefs::kPushMessagingRegistrationCount);
153 profile_->GetPrefs()->SetInteger(prefs::kPushMessagingRegistrationCount,
154 old_count + 1);
155 if (old_count == 0)
156 gcm_profile_service_->driver()->AddWildcardAppHandler(this);
157 }
158 }
159
160 // TODO(johnme): Unregister should decrement the pref, and call
161 // RemoveWildcardAppHandler if the count drops to zero.
162
163 base::WeakPtr<PushMessagingServiceImpl> PushMessagingServiceImpl::GetWeakPtr() {
164 return weak_factory_.GetWeakPtr();
44 } 165 }
45 166
46 } // namespace gcm 167 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698