Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / sync / notifier / gcm_network_channel.h
blob5f86ce768d35e70af24e788f552c3be3642acaed
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.
5 #ifndef SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_
6 #define SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/non_thread_safe.h"
14 #include "net/base/backoff_entry.h"
15 #include "net/base/network_change_notifier.h"
16 #include "net/url_request/url_fetcher_delegate.h"
17 #include "sync/base/sync_export.h"
18 #include "sync/notifier/gcm_network_channel_delegate.h"
19 #include "sync/notifier/sync_system_resources.h"
20 #include "url/gurl.h"
22 class GoogleServiceAuthError;
24 namespace syncer {
25 class GCMNetworkChannel;
27 // POD with copy of some statuses for debugging purposes.
28 struct GCMNetworkChannelDiagnostic {
29 explicit GCMNetworkChannelDiagnostic(GCMNetworkChannel* parent);
31 // Collect all the internal variables in a single readable dictionary.
32 scoped_ptr<base::DictionaryValue> CollectDebugData() const;
34 // TODO(pavely): Move this toString to a more appropiate place in GCMClient.
35 std::string GCMClientResultToString(
36 const gcm::GCMClient::Result result) const;
38 GCMNetworkChannel* parent_;
39 bool last_message_empty_echo_token_;
40 base::Time last_message_received_time_;
41 int last_post_response_code_;
42 std::string registration_id_;
43 gcm::GCMClient::Result registration_result_;
44 int sent_messages_count_;
47 // GCMNetworkChannel is an implementation of SyncNetworkChannel that routes
48 // messages through GCMService.
49 class SYNC_EXPORT_PRIVATE GCMNetworkChannel
50 : public SyncNetworkChannel,
51 public net::URLFetcherDelegate,
52 public net::NetworkChangeNotifier::NetworkChangeObserver,
53 public base::NonThreadSafe {
54 public:
55 GCMNetworkChannel(
56 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
57 scoped_ptr<GCMNetworkChannelDelegate> delegate);
59 virtual ~GCMNetworkChannel();
61 // invalidation::NetworkChannel implementation.
62 virtual void SendMessage(const std::string& message) OVERRIDE;
63 virtual void SetMessageReceiver(
64 invalidation::MessageCallback* incoming_receiver) OVERRIDE;
66 // SyncNetworkChannel implementation.
67 virtual void UpdateCredentials(const std::string& email,
68 const std::string& token) OVERRIDE;
69 virtual int GetInvalidationClientType() OVERRIDE;
70 virtual void RequestDetailedStatus(
71 base::Callback<void(const base::DictionaryValue&)> callback) OVERRIDE;
73 // URLFetcherDelegate implementation.
74 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
76 // NetworkChangeObserver implementation.
77 virtual void OnNetworkChanged(
78 net::NetworkChangeNotifier::ConnectionType connection_type) OVERRIDE;
80 protected:
81 void ResetRegisterBackoffEntryForTest(
82 const net::BackoffEntry::Policy* policy);
84 virtual GURL BuildUrl(const std::string& registration_id);
86 private:
87 friend class GCMNetworkChannelTest;
88 void Register();
89 void OnRegisterComplete(const std::string& registration_id,
90 gcm::GCMClient::Result result);
91 void RequestAccessToken();
92 void OnGetTokenComplete(const GoogleServiceAuthError& error,
93 const std::string& token);
94 void OnIncomingMessage(const std::string& message,
95 const std::string& echo_token);
97 // Base64 encoding/decoding with URL safe alphabet.
98 // http://tools.ietf.org/html/rfc4648#page-7
99 static void Base64EncodeURLSafe(const std::string& input,
100 std::string* output);
101 static bool Base64DecodeURLSafe(const std::string& input,
102 std::string* output);
104 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
105 scoped_ptr<GCMNetworkChannelDelegate> delegate_;
107 // Message is saved until all conditions are met: there is valid
108 // registration_id and access_token.
109 std::string cached_message_;
111 // Access token is saved because in case of auth failure from server we need
112 // to invalidate it.
113 std::string access_token_;
115 // GCM registration_id is requested one at startup and never refreshed until
116 // next restart.
117 std::string registration_id_;
118 scoped_ptr<net::BackoffEntry> register_backoff_entry_;
120 scoped_ptr<net::URLFetcher> fetcher_;
122 // cacheinvalidation client receives echo_token with incoming message from
123 // GCM and shuld include it in headers with outgoing message over http.
124 std::string echo_token_;
126 GCMNetworkChannelDiagnostic diagnostic_info_;
128 base::WeakPtrFactory<GCMNetworkChannel> weak_factory_;
130 DISALLOW_COPY_AND_ASSIGN(GCMNetworkChannel);
133 } // namespace syncer
135 #endif // SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_