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 COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_REQUEST_H_
6#define COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_REQUEST_H_
7
8#include "base/callback.h"
9#include "base/compiler_specific.h"
10#include "base/gtest_prod_util.h"
11#include "base/macros.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/weak_ptr.h"
15#include "net/base/backoff_entry.h"
16#include "net/url_request/url_fetcher_delegate.h"
17#include "net/url_request/url_request_context_getter.h"
18
19namespace net {
20class URLRequestContextGetter;
21}
22
23namespace gcm {
24
25// Defines the request to talk with the server to determine if the GCM support
26// should be enabled.
27class GCMChannelStatusRequest : public net::URLFetcherDelegate {
28 public:
29  // Callback completing the channel status request.
30  // |update_received|: use the existing values if it is false which means no
31  //                    update is received.
32  // |enabled|: indicates if GCM is enabled (allowed to run) or not.
33  // |poll_interval_seconds|: the interval in seconds to start next poll
34  //                          request.
35  typedef base::Callback<void(bool update_received,
36                              bool enabled,
37                              int poll_interval_seconds)>
38      GCMChannelStatusRequestCallback;
39
40  GCMChannelStatusRequest(
41      const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
42      const std::string& channel_status_request_url,
43      const std::string& user_agent,
44      const GCMChannelStatusRequestCallback& callback);
45  virtual ~GCMChannelStatusRequest();
46
47  void Start();
48
49  static int default_poll_interval_seconds();
50  static int min_poll_interval_seconds();
51
52 private:
53  FRIEND_TEST_ALL_PREFIXES(GCMChannelStatusRequestTest, RequestData);
54
55  // Overridden from URLFetcherDelegate:
56  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
57
58  bool ParseResponse(const net::URLFetcher* source);
59  void RetryWithBackoff(bool update_backoff);
60
61  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
62  const std::string channel_status_request_url_;
63  const std::string user_agent_;
64  GCMChannelStatusRequestCallback callback_;
65  scoped_ptr<net::URLFetcher> url_fetcher_;
66  net::BackoffEntry backoff_entry_;
67  base::WeakPtrFactory<GCMChannelStatusRequest> weak_ptr_factory_;
68
69  DISALLOW_COPY_AND_ASSIGN(GCMChannelStatusRequest);
70};
71
72}  // namespace gcm
73
74#endif  // COMPONENTS_GCM_DRIVER_GCM_CHANNEL_STATUS_REQUEST_H_
75