summaryrefslogtreecommitdiffstats
path: root/sync/notifier/gcm_network_channel.h
blob: 4b50dc97a14e9cdac23caa67428623dc4606fbeb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_
#define SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_

#include <string>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "net/base/backoff_entry.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "sync/base/sync_export.h"
#include "sync/notifier/gcm_network_channel_delegate.h"
#include "sync/notifier/sync_system_resources.h"
#include "url/gurl.h"

class GoogleServiceAuthError;

namespace syncer {

// GCMNetworkChannel is an implementation of SyncNetworkChannel that routes
// messages through GCMProfileService.
class SYNC_EXPORT_PRIVATE GCMNetworkChannel
    : public SyncNetworkChannel,
      public net::URLFetcherDelegate,
      public base::NonThreadSafe {
 public:
  GCMNetworkChannel(
      scoped_refptr<net::URLRequestContextGetter> request_context_getter,
      scoped_ptr<GCMNetworkChannelDelegate> delegate);

  virtual ~GCMNetworkChannel();

  // invalidation::NetworkChannel implementation.
  virtual void SendMessage(const std::string& message) OVERRIDE;

  // SyncNetworkChannel implementation.
  virtual void UpdateCredentials(const std::string& email,
                                 const std::string& token) OVERRIDE;

  // URLFetcherDelegate implementation.
  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;

 protected:
  void ResetRegisterBackoffEntryForTest(
      const net::BackoffEntry::Policy* policy);

  virtual GURL BuildUrl(const std::string& registration_id);

 private:
  friend class GCMNetworkChannelTest;
  void Register();
  void OnRegisterComplete(const std::string& registration_id,
                          gcm::GCMClient::Result result);
  void RequestAccessToken();
  void OnGetTokenComplete(const GoogleServiceAuthError& error,
                          const std::string& token);
  // Base64 encoding/decoding with URL safe alphabet.
  // http://tools.ietf.org/html/rfc4648#page-7
  static void Base64EncodeURLSafe(const std::string& input,
                                  std::string* output);
  static bool Base64DecodeURLSafe(const std::string& input,
                                  std::string* output);

  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
  scoped_ptr<GCMNetworkChannelDelegate> delegate_;

  // Message is saved until all conditions are met: there is valid
  // registration_id and access_token.
  std::string cached_message_;

  // Access token is saved because in case of auth failure from server we need
  // to invalidate it.
  std::string access_token_;

  // GCM registration_id is requested one at startup and never refreshed until
  // next restart.
  std::string registration_id_;
  scoped_ptr<net::BackoffEntry> register_backoff_entry_;

  scoped_ptr<net::URLFetcher> fetcher_;

  base::WeakPtrFactory<GCMNetworkChannel> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(GCMNetworkChannel);
};

}  // namespace syncer

#endif  // SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_