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