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" | |
| 13 | |
| 14 using instance_id::InstanceID; | |
| 15 | |
| 16 namespace ntp_snippets { | |
| 17 | |
| 18 // Our sender ID we send up with all of our GCM messages. | |
|
sfiera
2017/06/02 08:43:05
This doesn't tell me a lot more than the variable
mamir
2017/06/02 13:13:56
Done.
| |
| 19 const char* kContentSuggestionsGCMAppID = "com.google.contentsuggestions.gcm"; | |
| 20 const char* kContentSuggestionsGCMSenderId = "128223710667"; | |
| 21 | |
| 22 // Scope passed to getToken to obtain GCM registration tokens. | |
|
sfiera
2017/06/02 08:43:04
// OAuth2 scope…
mamir
2017/06/02 13:13:57
Done.
| |
| 23 // Must match Java GoogleCloudMessaging.INSTANCE_ID_SCOPE. | |
| 24 const char kGCMScope[] = "GCM"; | |
| 25 | |
| 26 ContentSuggestionsGCMAppHandler::ContentSuggestionsGCMAppHandler( | |
| 27 gcm::GCMDriver* gcm_driver, | |
| 28 std::unique_ptr<BreakingNewsSubscriptionManager> subscription_manager) | |
| 29 : gcm_driver_(gcm_driver), | |
| 30 subscription_manager_(std::move(subscription_manager)), | |
| 31 weak_factory_(this) { | |
| 32 if (gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID) == this) { | |
|
sfiera
2017/06/02 08:43:05
I'm not sure how this is possible. We are in the c
mamir
2017/06/02 13:13:57
Acknowledged.
| |
| 33 // GCM app handler already added. | |
| 34 return; | |
| 35 } | |
| 36 | |
| 37 gcm_driver_->AddAppHandler(kContentSuggestionsGCMAppID, this); | |
| 38 } | |
| 39 | |
| 40 void ContentSuggestionsGCMAppHandler::DoSubscribe( | |
| 41 instance_id::InstanceIDDriver* instance_id_driver) { | |
| 42 instance_id_driver->GetInstanceID(kContentSuggestionsGCMAppID) | |
| 43 ->GetToken(kContentSuggestionsGCMSenderId, kGCMScope, | |
| 44 std::map<std::string, std::string>() /* options */, | |
| 45 base::Bind(&ContentSuggestionsGCMAppHandler::DidSubscribe, | |
| 46 weak_factory_.GetWeakPtr())); | |
| 47 } | |
| 48 | |
| 49 void ContentSuggestionsGCMAppHandler::Shutdown() { | |
|
sfiera
2017/06/02 08:43:05
Can this object be destroyed without being Shutdow
mamir
2017/06/02 13:13:57
Done.
| |
| 50 gcm_driver_->RemoveAppHandler(kContentSuggestionsGCMAppID); | |
| 51 } | |
| 52 | |
| 53 void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} | |
| 54 | |
| 55 void ContentSuggestionsGCMAppHandler::OnStoreReset() {} | |
| 56 | |
| 57 void ContentSuggestionsGCMAppHandler::OnMessage( | |
| 58 const std::string& app_id, | |
| 59 const gcm::IncomingMessage& message) { | |
| 60 // TODO(mamir): Implement Show notification and update the feed. | |
|
sfiera
2017/06/02 08:43:04
Add a DVLOG(1) for now?
mamir
2017/06/02 13:13:57
Done.
| |
| 61 } | |
| 62 | |
| 63 void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( | |
| 64 const std::string& app_id) { | |
| 65 // Messages don't get deleted. | |
| 66 } | |
| 67 | |
| 68 void ContentSuggestionsGCMAppHandler::OnSendError( | |
| 69 const std::string& app_id, | |
| 70 const gcm::GCMClient::SendErrorDetails& details) { | |
| 71 // Should never be called because we don't send GCM messages to | |
| 72 // the server. | |
| 73 } | |
| 74 | |
| 75 void ContentSuggestionsGCMAppHandler::OnSendAcknowledged( | |
| 76 const std::string& app_id, | |
| 77 const std::string& message_id) { | |
| 78 // Should never be called because we don't send GCM messages to | |
| 79 // the server. | |
| 80 } | |
| 81 | |
| 82 void ContentSuggestionsGCMAppHandler::DidSubscribe( | |
| 83 const std::string& subscription_id, | |
| 84 InstanceID::Result result) { | |
| 85 content::PushRegistrationStatus status = | |
| 86 content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | |
| 87 | |
| 88 switch (result) { | |
| 89 case InstanceID::SUCCESS: | |
| 90 // TODO (mamir): store the subscription_id in prefs. | |
| 91 subscription_manager_->Subscribe(subscription_id); | |
| 92 return; | |
| 93 case InstanceID::INVALID_PARAMETER: | |
| 94 case InstanceID::DISABLED: | |
| 95 case InstanceID::ASYNC_OPERATION_PENDING: | |
| 96 case InstanceID::SERVER_ERROR: | |
| 97 case InstanceID::UNKNOWN_ERROR: | |
| 98 DLOG(WARNING) | |
| 99 << "Push messaging subscription failed; InstanceID::Result = " | |
| 100 << result; | |
| 101 status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; | |
| 102 break; | |
| 103 case InstanceID::NETWORK_ERROR: | |
| 104 status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; | |
| 105 break; | |
| 106 } | |
| 107 DLOG(WARNING) << "Status = " << status; | |
| 108 } | |
| 109 } // namespace ntp_snippets | |
| OLD | NEW |