Roll src/third_party/WebKit 3529d49:06e8485 (svn 202554:202555)
[chromium-blink-merge.git] / google_apis / gcm / engine / registration_request.h
blobbcc3ee04fe200961bcbb96fc6889d529fb7be03e
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_REGISTRATION_REQUEST_H_
6 #define GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/time/time.h"
18 #include "google_apis/gcm/base/gcm_export.h"
19 #include "net/base/backoff_entry.h"
20 #include "net/url_request/url_fetcher_delegate.h"
21 #include "url/gurl.h"
23 namespace net {
24 class URLRequestContextGetter;
27 namespace gcm {
29 class GCMStatsRecorder;
31 // Registration request is used to obtain registration IDs for applications that
32 // want to use GCM. It requires a set of parameters to be specified to identify
33 // the Chrome instance, the user, the application and a set of senders that will
34 // be authorized to address the application using it's assigned registration ID.
35 class GCM_EXPORT RegistrationRequest : public net::URLFetcherDelegate {
36 public:
37 // This enum is also used in an UMA histogram (GCMRegistrationRequestStatus
38 // enum defined in tools/metrics/histograms/histogram.xml). Hence the entries
39 // here shouldn't be deleted or re-ordered and new ones should be added to
40 // the end.
41 enum Status {
42 SUCCESS, // Registration completed successfully.
43 INVALID_PARAMETERS, // One of request paramteres was invalid.
44 INVALID_SENDER, // One of the provided senders was invalid.
45 AUTHENTICATION_FAILED, // Authentication failed.
46 DEVICE_REGISTRATION_ERROR, // Chrome is not properly registered.
47 UNKNOWN_ERROR, // Unknown error.
48 URL_FETCHING_FAILED, // URL fetching failed.
49 HTTP_NOT_OK, // HTTP status was not OK.
50 RESPONSE_PARSING_FAILED, // Registration response parsing failed.
51 REACHED_MAX_RETRIES, // Reached maximum number of retries.
52 // NOTE: always keep this entry at the end. Add new status types only
53 // immediately above this line. Make sure to update the corresponding
54 // histogram enum accordingly.
55 STATUS_COUNT
58 // Callback completing the registration request.
59 typedef base::Callback<void(Status status,
60 const std::string& registration_id)>
61 RegistrationCallback;
63 // Defines the common info about a registration/token request. All parameters
64 // are mandatory.
65 struct GCM_EXPORT RequestInfo {
66 RequestInfo(uint64 android_id,
67 uint64 security_token,
68 const std::string& app_id);
69 ~RequestInfo();
71 // Android ID of the device.
72 uint64 android_id;
73 // Security token of the device.
74 uint64 security_token;
75 // Application ID.
76 std::string app_id;
79 // Encapsulates the custom logic that is needed to build and process the
80 // registration request.
81 class GCM_EXPORT CustomRequestHandler {
82 public:
83 CustomRequestHandler();
84 virtual ~CustomRequestHandler();
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;
91 // Reports various UMAs, including status, retry count and completion time.
92 virtual void ReportUMAs(Status status,
93 int retry_count,
94 base::TimeDelta complete_time) = 0;
97 RegistrationRequest(
98 const GURL& registration_url,
99 const RequestInfo& request_info,
100 scoped_ptr<CustomRequestHandler> custom_request_handler,
101 const net::BackoffEntry::Policy& backoff_policy,
102 const RegistrationCallback& callback,
103 int max_retry_count,
104 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
105 GCMStatsRecorder* recorder,
106 const std::string& source_to_record);
107 ~RegistrationRequest() override;
109 void Start();
111 // URLFetcherDelegate implementation.
112 void OnURLFetchComplete(const net::URLFetcher* source) override;
114 private:
115 // Schedules a retry attempt with a backoff.
116 void RetryWithBackoff();
118 void BuildRequestHeaders(std::string* extra_headers);
119 void BuildRequestBody(std::string* body);
121 // Parse the response returned by the URL fetcher into token, and returns the
122 // status.
123 Status ParseResponse(const net::URLFetcher* source, std::string* token);
125 RegistrationCallback callback_;
126 RequestInfo request_info_;
127 scoped_ptr<CustomRequestHandler> custom_request_handler_;
128 GURL registration_url_;
130 net::BackoffEntry backoff_entry_;
131 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
132 scoped_ptr<net::URLFetcher> url_fetcher_;
133 int retries_left_;
134 base::TimeTicks request_start_time_;
136 // Recorder that records GCM activities for debugging purpose. Not owned.
137 GCMStatsRecorder* recorder_;
138 std::string source_to_record_;
140 base::WeakPtrFactory<RegistrationRequest> weak_ptr_factory_;
142 DISALLOW_COPY_AND_ASSIGN(RegistrationRequest);
145 } // namespace gcm
147 #endif // GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_