OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 "google_apis/gcm/engine/gcm_unregistration_request_handler.h" |
| 6 |
| 7 #include "google_apis/gcm/base/gcm_util.h" |
| 8 #include "net/url_request/url_fetcher.h" |
| 9 |
| 10 namespace gcm { |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Request constants. |
| 15 const char kUnregistrationCallerKey[] = "gcm_unreg_caller"; |
| 16 // We are going to set the value to "false" in order to forcefully unregister |
| 17 // the application. |
| 18 const char kUnregistrationCallerValue[] = "false"; |
| 19 |
| 20 // Response constants. |
| 21 const char kDeletedPrefix[] = "deleted="; |
| 22 const char kErrorPrefix[] = "Error="; |
| 23 const char kInvalidParameters[] = "INVALID_PARAMETERS"; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 GCMUnregistrationRequestHandler::GCMUnregistrationRequestHandler( |
| 28 const std::string& app_id) |
| 29 : app_id_(app_id) { |
| 30 } |
| 31 |
| 32 GCMUnregistrationRequestHandler::~GCMUnregistrationRequestHandler() {} |
| 33 |
| 34 void GCMUnregistrationRequestHandler::BuildRequestBody(std::string* body){ |
| 35 BuildFormEncoding(kUnregistrationCallerKey, kUnregistrationCallerValue, body); |
| 36 } |
| 37 |
| 38 UnregistrationRequest::Status GCMUnregistrationRequestHandler::ParseResponse( |
| 39 const net::URLFetcher* source) { |
| 40 std::string response; |
| 41 if (!source->GetResponseAsString(&response)) { |
| 42 DVLOG(1) << "Failed to get response body."; |
| 43 return UnregistrationRequest::NO_RESPONSE_BODY; |
| 44 } |
| 45 |
| 46 DVLOG(1) << "Parsing unregistration response."; |
| 47 if (response.find(kDeletedPrefix) != std::string::npos) { |
| 48 std::string deleted_app_id = response.substr( |
| 49 response.find(kDeletedPrefix) + arraysize(kDeletedPrefix) - 1); |
| 50 return deleted_app_id == app_id_ ? |
| 51 UnregistrationRequest::SUCCESS : |
| 52 UnregistrationRequest::INCORRECT_APP_ID; |
| 53 } |
| 54 |
| 55 if (response.find(kErrorPrefix) != std::string::npos) { |
| 56 std::string error = response.substr( |
| 57 response.find(kErrorPrefix) + arraysize(kErrorPrefix) - 1); |
| 58 return error == kInvalidParameters ? |
| 59 UnregistrationRequest::INVALID_PARAMETERS : |
| 60 UnregistrationRequest::UNKNOWN_ERROR; |
| 61 } |
| 62 |
| 63 DVLOG(1) << "Not able to parse a meaningful output from response body." |
| 64 << response; |
| 65 return UnregistrationRequest::RESPONSE_PARSING_FAILED; |
| 66 } |
| 67 |
| 68 } // namespace gcm |
OLD | NEW |