c48f3c96ec26e0220a8e3498394d65255524932d
[platform/framework/web/crosswalk.git] / src / google_apis / gcm / engine / checkin_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_CHECKIN_REQUEST_H_
6 #define GOOGLE_APIS_GCM_ENGINE_CHECKIN_REQUEST_H_
7
8 #include <string>
9
10 #include "base/basictypes.h"
11 #include "base/callback.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 "google_apis/gcm/protocol/android_checkin.pb.h"
16 #include "google_apis/gcm/protocol/checkin.pb.h"
17 #include "net/base/backoff_entry.h"
18 #include "net/url_request/url_fetcher_delegate.h"
19 #include "url/gurl.h"
20
21 namespace net {
22 class URLRequestContextGetter;
23 }
24
25 namespace gcm {
26
27 class GCMStatsRecorder;
28
29 // Enables making check-in requests with the GCM infrastructure. When called
30 // with android_id and security_token both set to 0 it is an initial check-in
31 // used to obtain credentials. These should be persisted and used for subsequent
32 // check-ins.
33 class GCM_EXPORT CheckinRequest : public net::URLFetcherDelegate {
34  public:
35   // A callback function for the checkin request, accepting |checkin_response|
36   // protobuf.
37   typedef base::Callback<void(const checkin_proto::AndroidCheckinResponse&
38                                   checkin_response)> CheckinRequestCallback;
39
40   // Checkin request details.
41   struct GCM_EXPORT RequestInfo {
42     RequestInfo(uint64 android_id,
43                 uint64 security_token,
44                 const std::map<std::string, std::string>& account_tokens,
45                 const std::string& settings_digest,
46                 const checkin_proto::ChromeBuildProto& chrome_build_proto);
47     ~RequestInfo();
48
49     // Android ID of the device.
50     uint64 android_id;
51     // Security token of the device.
52     uint64 security_token;
53     // Map of account OAuth2 tokens keyed by emails.
54     std::map<std::string, std::string> account_tokens;
55     // Digest of GServices settings on the device.
56     std::string settings_digest;
57     // Information of the Chrome build of this device.
58     checkin_proto::ChromeBuildProto chrome_build_proto;
59   };
60
61   CheckinRequest(const GURL& checkin_url,
62                  const RequestInfo& request_info,
63                  const net::BackoffEntry::Policy& backoff_policy,
64                  const CheckinRequestCallback& callback,
65                  net::URLRequestContextGetter* request_context_getter,
66                  GCMStatsRecorder* recorder);
67   virtual ~CheckinRequest();
68
69   void Start();
70
71   // URLFetcherDelegate implementation.
72   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
73
74  private:
75   // Schedules a retry attempt, informs the backoff of a previous request's
76   // failure when |update_backoff| is true.
77   void RetryWithBackoff(bool update_backoff);
78
79   net::URLRequestContextGetter* request_context_getter_;
80   CheckinRequestCallback callback_;
81
82   net::BackoffEntry backoff_entry_;
83   GURL checkin_url_;
84   scoped_ptr<net::URLFetcher> url_fetcher_;
85   const RequestInfo request_info_;
86   base::TimeTicks request_start_time_;
87
88   // Recorder that records GCM activities for debugging purpose. Not owned.
89   GCMStatsRecorder* recorder_;
90
91   base::WeakPtrFactory<CheckinRequest> weak_ptr_factory_;
92
93   DISALLOW_COPY_AND_ASSIGN(CheckinRequest);
94 };
95
96 }  // namespace gcm
97
98 #endif  // GOOGLE_APIS_GCM_ENGINE_CHECKIN_REQUEST_H_