OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_ | 5 #ifndef SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_ |
6 #define SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_ | 6 #define SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/threading/non_thread_safe.h" |
| 14 #include "net/url_request/url_fetcher_delegate.h" |
13 #include "sync/base/sync_export.h" | 15 #include "sync/base/sync_export.h" |
| 16 #include "sync/notifier/gcm_network_channel_delegate.h" |
14 #include "sync/notifier/sync_system_resources.h" | 17 #include "sync/notifier/sync_system_resources.h" |
| 18 #include "url/gurl.h" |
| 19 |
| 20 class GoogleServiceAuthError; |
15 | 21 |
16 namespace syncer { | 22 namespace syncer { |
17 | 23 |
18 // GCMNetworkChannel is an implementation of SyncNetworkChannel that routes | 24 // GCMNetworkChannel is an implementation of SyncNetworkChannel that routes |
19 // messages through GCMProfileService. | 25 // messages through GCMProfileService. |
20 class SYNC_EXPORT_PRIVATE GCMNetworkChannel | 26 class SYNC_EXPORT_PRIVATE GCMNetworkChannel |
21 : public SyncNetworkChannel { | 27 : public SyncNetworkChannel, |
| 28 public net::URLFetcherDelegate, |
| 29 public base::NonThreadSafe { |
22 public: | 30 public: |
23 explicit GCMNetworkChannel(); | 31 GCMNetworkChannel( |
| 32 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 33 scoped_ptr<GCMNetworkChannelDelegate> delegate); |
24 | 34 |
25 virtual ~GCMNetworkChannel(); | 35 virtual ~GCMNetworkChannel(); |
26 | 36 |
27 // SyncNetworkChannel implementation. | 37 // SyncNetworkChannel implementation. |
28 virtual void SendEncodedMessage(const std::string& encoded_message) OVERRIDE; | 38 virtual void SendEncodedMessage(const std::string& encoded_message) OVERRIDE; |
29 virtual void UpdateCredentials(const std::string& email, | 39 virtual void UpdateCredentials(const std::string& email, |
30 const std::string& token) OVERRIDE; | 40 const std::string& token) OVERRIDE; |
| 41 |
| 42 // URLFetcherDelegate implementation. |
| 43 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
31 | 44 |
32 private: | 45 private: |
| 46 void OnRegisterComplete(const std::string& registration_id, |
| 47 gcm::GCMClient::Result result); |
| 48 void RequestAccessToken(); |
| 49 void OnGetTokenComplete(const GoogleServiceAuthError& error, |
| 50 const std::string& token); |
| 51 GURL BuildUrl(); |
| 52 |
| 53 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 54 scoped_ptr<GCMNetworkChannelDelegate> delegate_; |
| 55 |
| 56 // Message is saved until all conditions are met: there is valid |
| 57 // registration_id and access_token. |
| 58 std::string encoded_message_; |
| 59 |
| 60 // Access token is saved because in case of auth failure from server we need |
| 61 // to invalidate it. |
| 62 std::string access_token_; |
| 63 |
| 64 // GCM registration_id is requested one at startup and never refreshed until |
| 65 // next restart. |
| 66 std::string registration_id_; |
| 67 |
| 68 scoped_ptr<net::URLFetcher> fetcher_; |
| 69 |
| 70 base::WeakPtrFactory<GCMNetworkChannel> weak_factory_; |
| 71 |
33 DISALLOW_COPY_AND_ASSIGN(GCMNetworkChannel); | 72 DISALLOW_COPY_AND_ASSIGN(GCMNetworkChannel); |
34 }; | 73 }; |
35 | 74 |
36 } // namespace syncer | 75 } // namespace syncer |
37 | 76 |
38 #endif // SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_ | 77 #endif // SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_ |
OLD | NEW |