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_UNREGISTRATION_REQUEST_H_ | 5 #ifndef GOOGLE_APIS_GCM_ENGINE_UNREGISTRATION_REQUEST_H_ |
6 #define GOOGLE_APIS_GCM_ENGINE_UNREGISTRATION_REQUEST_H_ | 6 #define GOOGLE_APIS_GCM_ENGINE_UNREGISTRATION_REQUEST_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
14 #include "google_apis/gcm/base/gcm_export.h" | 14 #include "google_apis/gcm/base/gcm_export.h" |
15 #include "net/base/backoff_entry.h" | 15 #include "net/base/backoff_entry.h" |
16 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 class URLRequestContextGetter; | 20 class URLRequestContextGetter; |
21 } | 21 } |
22 | 22 |
23 namespace gcm { | 23 namespace gcm { |
24 | 24 |
25 class GCMStatsRecorder; | 25 class GCMStatsRecorder; |
26 | 26 |
27 // Unregistration request is used to revoke registration IDs for applications | 27 // Encapsulates the common logic applying to both GCM unregistration requests |
28 // that were uninstalled and should no longer receive GCM messages. In case an | 28 // and InstanceID delete-token requests. In case an attempt fails, it will retry |
29 // attempt to unregister fails, it will retry using the backoff policy. | 29 // using the backoff policy. |
30 // TODO(fgorski): Consider sharing code with RegistrationRequest if possible. | 30 // TODO(fgorski): Consider sharing code with RegistrationRequest if possible. |
31 class GCM_EXPORT UnregistrationRequest : public net::URLFetcherDelegate { | 31 class GCM_EXPORT UnregistrationRequest : public net::URLFetcherDelegate { |
32 public: | 32 public: |
33 // Outcome of the response parsing. Note that these enums are consumed by a | 33 // Outcome of the response parsing. Note that these enums are consumed by a |
34 // histogram, so ordering should not be modified. | 34 // histogram, so ordering should not be modified. |
35 enum Status { | 35 enum Status { |
36 SUCCESS, // Unregistration completed successfully. | 36 SUCCESS, // Unregistration completed successfully. |
37 URL_FETCHING_FAILED, // URL fetching failed. | 37 URL_FETCHING_FAILED, // URL fetching failed. |
38 NO_RESPONSE_BODY, // No response body. | 38 NO_RESPONSE_BODY, // No response body. |
39 RESPONSE_PARSING_FAILED, // Failed to parse a meaningful output from | 39 RESPONSE_PARSING_FAILED, // Failed to parse a meaningful output from |
40 // response | 40 // response |
41 // body. | 41 // body. |
42 INCORRECT_APP_ID, // App ID returned by the fetcher does not match | 42 INCORRECT_APP_ID, // App ID returned by the fetcher does not match |
43 // request. | 43 // request. |
44 INVALID_PARAMETERS, // Request parameters were invalid. | 44 INVALID_PARAMETERS, // Request parameters were invalid. |
45 SERVICE_UNAVAILABLE, // Unregistration service unavailable. | 45 SERVICE_UNAVAILABLE, // Unregistration service unavailable. |
46 INTERNAL_SERVER_ERROR, // Internal server error happened during request. | 46 INTERNAL_SERVER_ERROR, // Internal server error happened during request. |
47 HTTP_NOT_OK, // HTTP response code was not OK. | 47 HTTP_NOT_OK, // HTTP response code was not OK. |
48 UNKNOWN_ERROR, // Unknown error. | 48 UNKNOWN_ERROR, // Unknown error. |
49 // NOTE: Always keep this entry at the end. Add new status types only | 49 // NOTE: Always keep this entry at the end. Add new status types only |
50 // immediately above this line. Make sure to update the corresponding | 50 // immediately above this line. Make sure to update the corresponding |
51 // histogram enum accordingly. | 51 // histogram enum accordingly. |
52 UNREGISTRATION_STATUS_COUNT, | 52 UNREGISTRATION_STATUS_COUNT, |
53 }; | 53 }; |
54 | 54 |
55 // Callback completing the unregistration request. | 55 // Callback completing the unregistration request. |
56 typedef base::Callback<void(Status success)> UnregistrationCallback; | 56 typedef base::Callback<void(Status success)> UnregistrationCallback; |
57 | 57 |
58 // Details of the of the Unregistration Request. All parameters are mandatory. | 58 // Defines the common info about an unregistration/token-deletion request. |
| 59 // All parameters are mandatory. |
59 struct GCM_EXPORT RequestInfo { | 60 struct GCM_EXPORT RequestInfo { |
60 RequestInfo(uint64 android_id, | 61 RequestInfo(uint64 android_id, |
61 uint64 security_token, | 62 uint64 security_token, |
62 const std::string& app_id); | 63 const std::string& app_id); |
63 ~RequestInfo(); | 64 ~RequestInfo(); |
64 | 65 |
65 // Android ID of the device. | 66 // Android ID of the device. |
66 uint64 android_id; | 67 uint64 android_id; |
67 // Security token of the device. | 68 // Security token of the device. |
68 uint64 security_token; | 69 uint64 security_token; |
69 // Application ID. | 70 // Application ID. |
70 std::string app_id; | 71 std::string app_id; |
71 }; | 72 }; |
72 | 73 |
| 74 // Encapsulates the custom logic that is needed to build and process the |
| 75 // unregistration request. |
| 76 class GCM_EXPORT CustomRequestHandler { |
| 77 public: |
| 78 CustomRequestHandler(); |
| 79 virtual ~CustomRequestHandler(); |
| 80 |
| 81 // Builds the HTTP request body data. It is called after |
| 82 // UnregistrationRequest::BuildRequestBody to append more custom info to |
| 83 // |body|. Note that the request body is encoded in HTTP form format. |
| 84 virtual void BuildRequestBody(std::string* body) = 0; |
| 85 |
| 86 // Parses the HTTP response. It is called after |
| 87 // UnregistrationRequest::ParseResponse to proceed the parsing. |
| 88 virtual Status ParseResponse(const net::URLFetcher* source) = 0; |
| 89 }; |
| 90 |
73 // Creates an instance of UnregistrationRequest. |callback| will be called | 91 // Creates an instance of UnregistrationRequest. |callback| will be called |
74 // once registration has been revoked or there has been an error that makes | 92 // once registration has been revoked or there has been an error that makes |
75 // further retries pointless. | 93 // further retries pointless. |
76 UnregistrationRequest( | 94 UnregistrationRequest( |
77 const GURL& registration_url, | 95 const GURL& registration_url, |
78 const RequestInfo& request_info, | 96 const RequestInfo& request_info, |
| 97 scoped_ptr<CustomRequestHandler> custom_request_handler, |
79 const net::BackoffEntry::Policy& backoff_policy, | 98 const net::BackoffEntry::Policy& backoff_policy, |
80 const UnregistrationCallback& callback, | 99 const UnregistrationCallback& callback, |
81 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 100 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
82 GCMStatsRecorder* recorder); | 101 GCMStatsRecorder* recorder); |
83 ~UnregistrationRequest() override; | 102 ~UnregistrationRequest() override; |
84 | 103 |
85 // Starts an unregistration request. | 104 // Starts an unregistration request. |
86 void Start(); | 105 void Start(); |
87 | 106 |
| 107 private: |
88 // URLFetcherDelegate implementation. | 108 // URLFetcherDelegate implementation. |
89 void OnURLFetchComplete(const net::URLFetcher* source) override; | 109 void OnURLFetchComplete(const net::URLFetcher* source) override; |
90 | 110 |
91 private: | 111 void BuildRequestHeaders(std::string* extra_headers); |
| 112 void BuildRequestBody(std::string* body); |
| 113 Status ParseResponse(const net::URLFetcher* source); |
| 114 |
92 // Schedules a retry attempt and informs the backoff of previous request's | 115 // Schedules a retry attempt and informs the backoff of previous request's |
93 // failure, when |update_backoff| is true. | 116 // failure, when |update_backoff| is true. |
94 void RetryWithBackoff(bool update_backoff); | 117 void RetryWithBackoff(bool update_backoff); |
95 | 118 |
96 UnregistrationCallback callback_; | 119 UnregistrationCallback callback_; |
97 RequestInfo request_info_; | 120 RequestInfo request_info_; |
| 121 scoped_ptr<CustomRequestHandler> custom_request_handler_; |
98 GURL registration_url_; | 122 GURL registration_url_; |
99 | 123 |
100 net::BackoffEntry backoff_entry_; | 124 net::BackoffEntry backoff_entry_; |
101 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 125 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
102 scoped_ptr<net::URLFetcher> url_fetcher_; | 126 scoped_ptr<net::URLFetcher> url_fetcher_; |
103 base::TimeTicks request_start_time_; | 127 base::TimeTicks request_start_time_; |
104 | 128 |
105 // Recorder that records GCM activities for debugging purpose. Not owned. | 129 // Recorder that records GCM activities for debugging purpose. Not owned. |
106 GCMStatsRecorder* recorder_; | 130 GCMStatsRecorder* recorder_; |
107 | 131 |
108 base::WeakPtrFactory<UnregistrationRequest> weak_ptr_factory_; | 132 base::WeakPtrFactory<UnregistrationRequest> weak_ptr_factory_; |
109 | 133 |
110 DISALLOW_COPY_AND_ASSIGN(UnregistrationRequest); | 134 DISALLOW_COPY_AND_ASSIGN(UnregistrationRequest); |
111 }; | 135 }; |
112 | 136 |
113 } // namespace gcm | 137 } // namespace gcm |
114 | 138 |
115 #endif // GOOGLE_APIS_GCM_ENGINE_UNREGISTRATION_REQUEST_H_ | 139 #endif // GOOGLE_APIS_GCM_ENGINE_UNREGISTRATION_REQUEST_H_ |
OLD | NEW |