Home | History | Annotate | Download | only in engine
      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::string& settings_digest,
     45                 const checkin_proto::ChromeBuildProto& chrome_build_proto);
     46     ~RequestInfo();
     47 
     48     // Android ID of the device.
     49     uint64 android_id;
     50     // Security token of the device.
     51     uint64 security_token;
     52     // Digest of GServices settings on the device.
     53     std::string settings_digest;
     54     // Information of the Chrome build of this device.
     55     checkin_proto::ChromeBuildProto chrome_build_proto;
     56   };
     57 
     58   CheckinRequest(const GURL& checkin_url,
     59                  const RequestInfo& request_info,
     60                  const net::BackoffEntry::Policy& backoff_policy,
     61                  const CheckinRequestCallback& callback,
     62                  net::URLRequestContextGetter* request_context_getter,
     63                  GCMStatsRecorder* recorder);
     64   virtual ~CheckinRequest();
     65 
     66   void Start();
     67 
     68   // URLFetcherDelegate implementation.
     69   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
     70 
     71  private:
     72   // Schedules a retry attempt, informs the backoff of a previous request's
     73   // failure when |update_backoff| is true.
     74   void RetryWithBackoff(bool update_backoff);
     75 
     76   net::URLRequestContextGetter* request_context_getter_;
     77   CheckinRequestCallback callback_;
     78 
     79   net::BackoffEntry backoff_entry_;
     80   GURL checkin_url_;
     81   scoped_ptr<net::URLFetcher> url_fetcher_;
     82   const RequestInfo request_info_;
     83   base::TimeTicks request_start_time_;
     84 
     85   // Recorder that records GCM activities for debugging purpose. Not owned.
     86   GCMStatsRecorder* recorder_;
     87 
     88   base::WeakPtrFactory<CheckinRequest> weak_ptr_factory_;
     89 
     90   DISALLOW_COPY_AND_ASSIGN(CheckinRequest);
     91 };
     92 
     93 }  // namespace gcm
     94 
     95 #endif  // GOOGLE_APIS_GCM_ENGINE_CHECKIN_REQUEST_H_
     96