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.
4
5#ifndef GOOGLE_APIS_GCM_ENGINE_UNREGISTRATION_REQUEST_H_
6#define GOOGLE_APIS_GCM_ENGINE_UNREGISTRATION_REQUEST_H_
7
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"
17#include "url/gurl.h"
18
19namespace net {
20class URLRequestContextGetter;
21}
22
23namespace gcm {
24
25class GCMStatsRecorder;
26
27// Unregistration request is used to revoke registration IDs for applications
28// that were uninstalled and should no longer receive GCM messages. In case an
29// attempt to unregister fails, it will retry using the backoff policy.
30// TODO(fgorski): Consider sharing code with RegistrationRequest if possible.
31class GCM_EXPORT UnregistrationRequest : public net::URLFetcherDelegate {
32 public:
33  // Outcome of the response parsing. Note that these enums are consumed by a
34  // histogram, so ordering should not be modified.
35  enum Status {
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
40                              // response
41                              // body.
42    INCORRECT_APP_ID,         // App ID returned by the fetcher does not match
43                              // request.
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    // NOTE: Always keep this entry at the end. Add new status types only
50    // immediately above this line. Make sure to update the corresponding
51    // histogram enum accordingly.
52    UNREGISTRATION_STATUS_COUNT,
53  };
54
55  // Callback completing the unregistration request.
56  typedef base::Callback<void(Status success)> UnregistrationCallback;
57
58  // Details of the of the Unregistration Request. All parameters are mandatory.
59  struct GCM_EXPORT RequestInfo {
60    RequestInfo(uint64 android_id,
61                uint64 security_token,
62                const std::string& app_id);
63    ~RequestInfo();
64
65    // Android ID of the device.
66    uint64 android_id;
67    // Security token of the device.
68    uint64 security_token;
69    // Application ID.
70    std::string app_id;
71  };
72
73  // Creates an instance of UnregistrationRequest. |callback| will be called
74  // once registration has been revoked or there has been an error that makes
75  // further retries pointless.
76  UnregistrationRequest(
77      const GURL& registration_url,
78      const RequestInfo& request_info,
79      const net::BackoffEntry::Policy& backoff_policy,
80      const UnregistrationCallback& callback,
81      scoped_refptr<net::URLRequestContextGetter> request_context_getter,
82      GCMStatsRecorder* recorder);
83  virtual ~UnregistrationRequest();
84
85  // Starts an unregistration request.
86  void Start();
87
88  // URLFetcherDelegate implementation.
89  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
90
91 private:
92  // Schedules a retry attempt and informs the backoff of previous request's
93  // failure, when |update_backoff| is true.
94  void RetryWithBackoff(bool update_backoff);
95
96  UnregistrationCallback callback_;
97  RequestInfo request_info_;
98  GURL registration_url_;
99
100  net::BackoffEntry backoff_entry_;
101  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
102  scoped_ptr<net::URLFetcher> url_fetcher_;
103  base::TimeTicks request_start_time_;
104
105  // Recorder that records GCM activities for debugging purpose. Not owned.
106  GCMStatsRecorder* recorder_;
107
108  base::WeakPtrFactory<UnregistrationRequest> weak_ptr_factory_;
109
110  DISALLOW_COPY_AND_ASSIGN(UnregistrationRequest);
111};
112
113}  // namespace gcm
114
115#endif  // GOOGLE_APIS_GCM_ENGINE_UNREGISTRATION_REQUEST_H_
116