Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/ntp_snippets/breaking_news/content_suggestions_gcm_app_hand ler.h" | |
| 6 | |
| 7 #include "base/strings/string_util.h" | |
| 8 #include "components/gcm_driver/gcm_driver.h" | |
| 9 #include "components/gcm_driver/gcm_profile_service.h" | |
| 10 #include "components/gcm_driver/instance_id/instance_id.h" | |
| 11 #include "components/gcm_driver/instance_id/instance_id_driver.h" | |
| 12 #include "content/public/common/push_messaging_status.h" | |
|
Peter Beverloo
2017/06/02 14:02:02
This is specific to the Push API, I wouldn't add a
mamir
2017/06/04 15:10:14
Ah, sorry.
I guess you can guess how this code cam
| |
| 13 | |
| 14 using instance_id::InstanceID; | |
| 15 | |
| 16 namespace ntp_snippets { | |
| 17 | |
| 18 const char* kContentSuggestionsGCMAppID = "com.google.contentsuggestions.gcm"; | |
| 19 // The sender ID is used in the registration process. | |
| 20 // See: https://developers.google.com/cloud-messaging/gcm#senderid | |
| 21 const char* kContentSuggestionsGCMSenderId = "128223710667"; | |
|
Peter Beverloo
2017/06/02 14:02:02
style nit: define string constants as:
const ch
mamir
2017/06/04 15:10:13
Done.
| |
| 22 | |
| 23 // OAuth2 Scope passed to getToken to obtain GCM registration tokens. | |
| 24 // Must match Java GoogleCloudMessaging.INSTANCE_ID_SCOPE. | |
| 25 const char kGCMScope[] = "GCM"; | |
| 26 | |
| 27 ContentSuggestionsGCMAppHandler::ContentSuggestionsGCMAppHandler( | |
| 28 gcm::GCMDriver* gcm_driver, | |
| 29 instance_id::InstanceIDDriver* instance_id_driver, | |
| 30 std::unique_ptr<BreakingNewsSubscriptionManager> subscription_manager) | |
| 31 : gcm_driver_(gcm_driver), | |
| 32 instance_id_driver_(instance_id_driver), | |
| 33 subscription_manager_(std::move(subscription_manager)), | |
| 34 weak_factory_(this) { | |
| 35 Subscribe(); | |
|
sfiera
2017/06/02 13:41:24
The subscription isn't necessary until StartListen
Peter Beverloo
2017/06/02 14:02:02
+1. May I also ask you to consider adding the foll
mamir
2017/06/04 15:10:13
The whole content provider instantiation will be g
| |
| 36 } | |
| 37 ContentSuggestionsGCMAppHandler::~ContentSuggestionsGCMAppHandler() { | |
| 38 if (gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID) == this) { | |
| 39 StopListening(); | |
|
Peter Beverloo
2017/06/02 14:02:02
Something to consider: it's safe to just always ca
mamir
2017/06/04 15:10:13
That because AddAppHandler DCHECKs?
So we are sure
Peter Beverloo
2017/06/08 07:02:39
No - calling RemoveAppHandler() with an unregister
mamir
2017/06/08 10:31:38
Done.
| |
| 40 } | |
| 41 } | |
| 42 | |
| 43 void ContentSuggestionsGCMAppHandler::StartListening() { | |
| 44 DCHECK(!gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID)); | |
| 45 if (gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID) == this) { | |
|
sfiera
2017/06/02 13:41:24
Redundant. By the DCHECK(), we already know that t
Peter Beverloo
2017/06/02 14:02:02
AddAppHandler() itself will DCHECK, so you can rem
mamir
2017/06/04 15:10:13
I got confused. So we remove the whole if check a
Peter Beverloo
2017/06/08 07:02:39
Yes.
mamir
2017/06/08 10:31:38
Done.
| |
| 46 // GCM app handler already added. | |
| 47 return; | |
| 48 } | |
| 49 gcm_driver_->AddAppHandler(kContentSuggestionsGCMAppID, this); | |
| 50 } | |
| 51 | |
| 52 void ContentSuggestionsGCMAppHandler::StopListening() { | |
| 53 DCHECK_EQ(gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID), this); | |
| 54 gcm_driver_->RemoveAppHandler(kContentSuggestionsGCMAppID); | |
| 55 } | |
| 56 | |
| 57 void ContentSuggestionsGCMAppHandler::Subscribe() { | |
| 58 // TODO (mamir): Check the prefs for the cached subscription_id before calling | |
| 59 // the GCM service. | |
| 60 DVLOG(1) << "Implement subscription token caching."; | |
| 61 instance_id_driver_->GetInstanceID(kContentSuggestionsGCMAppID) | |
| 62 ->GetToken(kContentSuggestionsGCMSenderId, kGCMScope, | |
| 63 std::map<std::string, std::string>() /* options */, | |
| 64 base::Bind(&ContentSuggestionsGCMAppHandler::DidSubscribe, | |
| 65 weak_factory_.GetWeakPtr())); | |
| 66 } | |
| 67 | |
| 68 void ContentSuggestionsGCMAppHandler::DidSubscribe( | |
| 69 const std::string& subscription_id, | |
| 70 InstanceID::Result result) { | |
| 71 content::PushRegistrationStatus status = | |
| 72 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | |
| 73 | |
| 74 switch (result) { | |
| 75 case InstanceID::SUCCESS: | |
| 76 // TODO (mamir): cache the subscription_id in prefs. | |
| 77 DVLOG(1) << "Implement subscription token caching."; | |
|
Peter Beverloo
2017/06/02 14:02:02
nit: we usually don't add D(V)LOG()s for TODOs.
mamir
2017/06/04 15:10:13
Done.
| |
| 78 subscription_manager_->Subscribe(subscription_id); | |
| 79 return; | |
| 80 case InstanceID::INVALID_PARAMETER: | |
| 81 case InstanceID::DISABLED: | |
| 82 case InstanceID::ASYNC_OPERATION_PENDING: | |
| 83 case InstanceID::SERVER_ERROR: | |
| 84 case InstanceID::UNKNOWN_ERROR: | |
| 85 DLOG(WARNING) | |
| 86 << "Push messaging subscription failed; InstanceID::Result = " | |
| 87 << result; | |
| 88 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | |
| 89 break; | |
| 90 case InstanceID::NETWORK_ERROR: | |
| 91 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; | |
| 92 break; | |
| 93 } | |
| 94 DLOG(WARNING) << "Status = " << status; | |
| 95 } | |
| 96 | |
| 97 void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} | |
| 98 | |
| 99 void ContentSuggestionsGCMAppHandler::OnStoreReset() {} | |
|
Peter Beverloo
2017/06/02 14:02:02
You need to clear your token caching in preference
mamir
2017/06/04 15:10:13
Added a TODO for now.
| |
| 100 | |
| 101 void ContentSuggestionsGCMAppHandler::OnMessage( | |
| 102 const std::string& app_id, | |
| 103 const gcm::IncomingMessage& message) { | |
| 104 // TODO (mamir): Implement Show notification and update the feed. | |
|
sfiera
2017/06/02 13:41:24
Nit: I think TODO() should have no space (like a f
mamir
2017/06/04 15:10:13
Done.
| |
| 105 } | |
| 106 | |
| 107 void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( | |
| 108 const std::string& app_id) { | |
| 109 // Messages don't get deleted. | |
|
Peter Beverloo
2017/06/02 14:02:02
fwiw, better to be strict and NOTREACHED() in that
mamir
2017/06/04 15:10:13
Done.
| |
| 110 } | |
| 111 | |
| 112 void ContentSuggestionsGCMAppHandler::OnSendError( | |
| 113 const std::string& app_id, | |
| 114 const gcm::GCMClient::SendErrorDetails& details) { | |
| 115 // Should never be called because we don't send GCM messages to | |
| 116 // the server. | |
| 117 } | |
| 118 | |
| 119 void ContentSuggestionsGCMAppHandler::OnSendAcknowledged( | |
| 120 const std::string& app_id, | |
| 121 const std::string& message_id) { | |
| 122 // Should never be called because we don't send GCM messages to | |
| 123 // the server. | |
| 124 } | |
| 125 | |
| 126 } // namespace ntp_snippets | |
| OLD | NEW |