| 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 #ifndef GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_ | 5 #ifndef GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_ |
| 6 #define GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_ | 6 #define GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> |
| 9 #include <vector> | 10 #include <vector> |
| 10 | 11 |
| 11 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 12 #include "base/callback.h" | 13 #include "base/callback.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "google_apis/gcm/base/gcm_export.h" | 18 #include "google_apis/gcm/base/gcm_export.h" |
| 18 #include "net/base/backoff_entry.h" | 19 #include "net/base/backoff_entry.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 // immediately above this line. Make sure to update the corresponding | 53 // immediately above this line. Make sure to update the corresponding |
| 53 // histogram enum accordingly. | 54 // histogram enum accordingly. |
| 54 STATUS_COUNT | 55 STATUS_COUNT |
| 55 }; | 56 }; |
| 56 | 57 |
| 57 // Callback completing the registration request. | 58 // Callback completing the registration request. |
| 58 typedef base::Callback<void(Status status, | 59 typedef base::Callback<void(Status status, |
| 59 const std::string& registration_id)> | 60 const std::string& registration_id)> |
| 60 RegistrationCallback; | 61 RegistrationCallback; |
| 61 | 62 |
| 62 // Details of the of the Registration Request. Only user's android ID and | 63 // Defines the common info about a registration/token request. All parameters |
| 63 // its serial number are optional and can be set to 0. All other parameters | 64 // are mandatory. |
| 64 // have to be specified to successfully complete the call. | |
| 65 struct GCM_EXPORT RequestInfo { | 65 struct GCM_EXPORT RequestInfo { |
| 66 RequestInfo(uint64 android_id, | 66 RequestInfo(uint64 android_id, |
| 67 uint64 security_token, | 67 uint64 security_token, |
| 68 const std::string& app_id, | 68 const std::string& app_id); |
| 69 const std::vector<std::string>& sender_ids); | |
| 70 ~RequestInfo(); | 69 ~RequestInfo(); |
| 71 | 70 |
| 72 // Android ID of the device. | 71 // Android ID of the device. |
| 73 uint64 android_id; | 72 uint64 android_id; |
| 74 // Security token of the device. | 73 // Security token of the device. |
| 75 uint64 security_token; | 74 uint64 security_token; |
| 76 // Application ID. | 75 // Application ID. |
| 77 std::string app_id; | 76 std::string app_id; |
| 78 // Certificate of the application. | 77 }; |
| 79 std::string cert; | 78 |
| 80 // List of IDs of senders. Allowed up to 100. | 79 // Encapsulates the custom logic that is needed to build and process the |
| 81 std::vector<std::string> sender_ids; | 80 // registration request. |
| 81 class GCM_EXPORT CustomRequestHandler { |
| 82 public: |
| 83 CustomRequestHandler(); |
| 84 virtual ~CustomRequestHandler(); |
| 85 |
| 86 // Builds the HTTP request body data. It is called after |
| 87 // RegistrationRequest::BuildRequestBody to append more custom info to |
| 88 // |body|. Note that the request body is encoded in HTTP form format. |
| 89 virtual void BuildRequestBody(std::string* body) = 0; |
| 82 }; | 90 }; |
| 83 | 91 |
| 84 RegistrationRequest( | 92 RegistrationRequest( |
| 85 const GURL& registration_url, | 93 const GURL& registration_url, |
| 86 const RequestInfo& request_info, | 94 const RequestInfo& request_info, |
| 95 scoped_ptr<CustomRequestHandler> custom_request_handler, |
| 87 const net::BackoffEntry::Policy& backoff_policy, | 96 const net::BackoffEntry::Policy& backoff_policy, |
| 88 const RegistrationCallback& callback, | 97 const RegistrationCallback& callback, |
| 89 int max_retry_count, | 98 int max_retry_count, |
| 90 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 99 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 91 GCMStatsRecorder* recorder); | 100 GCMStatsRecorder* recorder, |
| 101 const std::string& source_to_record); |
| 92 ~RegistrationRequest() override; | 102 ~RegistrationRequest() override; |
| 93 | 103 |
| 94 void Start(); | 104 void Start(); |
| 95 | 105 |
| 96 // URLFetcherDelegate implementation. | 106 // URLFetcherDelegate implementation. |
| 97 void OnURLFetchComplete(const net::URLFetcher* source) override; | 107 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 98 | 108 |
| 99 private: | 109 private: |
| 100 // Schedules a retry attempt, informs the backoff of a previous request's | 110 // Schedules a retry attempt, informs the backoff of a previous request's |
| 101 // failure, when |update_backoff| is true. | 111 // failure, when |update_backoff| is true. |
| 102 void RetryWithBackoff(bool update_backoff); | 112 void RetryWithBackoff(bool update_backoff); |
| 103 | 113 |
| 114 void BuildRequestHeaders(std::string* extra_headers); |
| 115 void BuildRequestBody(std::string* body); |
| 116 |
| 104 // Parse the response returned by the URL fetcher into token, and returns the | 117 // Parse the response returned by the URL fetcher into token, and returns the |
| 105 // status. | 118 // status. |
| 106 Status ParseResponse(const net::URLFetcher* source, std::string* token); | 119 Status ParseResponse(const net::URLFetcher* source, std::string* token); |
| 107 | 120 |
| 108 RegistrationCallback callback_; | 121 RegistrationCallback callback_; |
| 109 RequestInfo request_info_; | 122 RequestInfo request_info_; |
| 123 scoped_ptr<CustomRequestHandler> custom_request_handler_; |
| 110 GURL registration_url_; | 124 GURL registration_url_; |
| 111 | 125 |
| 112 net::BackoffEntry backoff_entry_; | 126 net::BackoffEntry backoff_entry_; |
| 113 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 127 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 114 scoped_ptr<net::URLFetcher> url_fetcher_; | 128 scoped_ptr<net::URLFetcher> url_fetcher_; |
| 115 int retries_left_; | 129 int retries_left_; |
| 116 base::TimeTicks request_start_time_; | 130 base::TimeTicks request_start_time_; |
| 117 | 131 |
| 118 // Recorder that records GCM activities for debugging purpose. Not owned. | 132 // Recorder that records GCM activities for debugging purpose. Not owned. |
| 119 GCMStatsRecorder* recorder_; | 133 GCMStatsRecorder* recorder_; |
| 134 std::string source_to_record_; |
| 120 | 135 |
| 121 base::WeakPtrFactory<RegistrationRequest> weak_ptr_factory_; | 136 base::WeakPtrFactory<RegistrationRequest> weak_ptr_factory_; |
| 122 | 137 |
| 123 DISALLOW_COPY_AND_ASSIGN(RegistrationRequest); | 138 DISALLOW_COPY_AND_ASSIGN(RegistrationRequest); |
| 124 }; | 139 }; |
| 125 | 140 |
| 126 } // namespace gcm | 141 } // namespace gcm |
| 127 | 142 |
| 128 #endif // GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_ | 143 #endif // GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_ |
| OLD | NEW |