Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / google_apis / gcm / engine / registration_request.h
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_REGISTRATION_REQUEST_H_
6 #define GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_
7
8 #include <map>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "google_apis/gcm/base/gcm_export.h"
17 #include "net/base/backoff_entry.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19
20 namespace net {
21 class URLRequestContextGetter;
22 }
23
24 namespace gcm {
25
26 // Registration request is used to obtain registration IDs for applications that
27 // want to use GCM. It requires a set of parameters to be specified to identify
28 // the Chrome instance, the user, the application and a set of senders that will
29 // be authorized to address the application using it's assigned registration ID.
30 class GCM_EXPORT RegistrationRequest : public net::URLFetcherDelegate {
31  public:
32   // This enum is also used in an UMA histogram (GCMRegistrationRequestStatus
33   // enum defined in tools/metrics/histograms/histogram.xml). Hence the entries
34   // here shouldn't be deleted or re-ordered and new ones should be added to
35   // the end.
36   enum Status {
37     SUCCESS,                    // Registration completed successfully.
38     INVALID_PARAMETERS,         // One of request paramteres was invalid.
39     INVALID_SENDER,             // One of the provided senders was invalid.
40     AUTHENTICATION_FAILED,      // Authentication failed.
41     DEVICE_REGISTRATION_ERROR,  // Chrome is not properly registered.
42     UNKNOWN_ERROR,              // Unknown error.
43     // NOTE: always keep this entry at the end. Add new status types only
44     // immediately above this line. Make sure to update the corresponding
45     // histogram enum accordingly.
46     STATUS_COUNT
47   };
48
49   // Callback completing the registration request.
50   typedef base::Callback<void(Status status,
51                               const std::string& registration_id)>
52       RegistrationCallback;
53
54   // Details of the of the Registration Request. Only user's android ID and
55   // its serial number are optional and can be set to 0. All other parameters
56   // have to be specified to successfully complete the call.
57   struct GCM_EXPORT RequestInfo {
58     RequestInfo(uint64 android_id,
59                 uint64 security_token,
60                 const std::string& app_id,
61                 const std::string& cert,
62                 const std::vector<std::string>& sender_ids);
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     // Certificate of the application.
72     std::string cert;
73     // List of IDs of senders. Allowed up to 100.
74     std::vector<std::string> sender_ids;
75   };
76
77   RegistrationRequest(
78       const RequestInfo& request_info,
79       const net::BackoffEntry::Policy& backoff_policy,
80       const RegistrationCallback& callback,
81       scoped_refptr<net::URLRequestContextGetter> request_context_getter);
82   virtual ~RegistrationRequest();
83
84   void Start();
85
86   // URLFetcherDelegate implementation.
87   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
88
89  private:
90   // Schedules a retry attempt, informs the backoff of a previous request's
91   // failure, when |update_backoff| is true.
92   void RetryWithBackoff(bool update_backoff);
93
94   RegistrationCallback callback_;
95   RequestInfo request_info_;
96
97   net::BackoffEntry backoff_entry_;
98   scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
99   scoped_ptr<net::URLFetcher> url_fetcher_;
100
101   base::WeakPtrFactory<RegistrationRequest> weak_ptr_factory_;
102
103   DISALLOW_COPY_AND_ASSIGN(RegistrationRequest);
104 };
105
106 }  // namespace gcm
107
108 #endif  // GOOGLE_APIS_GCM_ENGINE_REGISTRATION_REQUEST_H_