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_INVALIDATION_PUSH_CLIENT_CHANNEL_H_
6#define COMPONENTS_INVALIDATION_PUSH_CLIENT_CHANNEL_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/memory/scoped_ptr.h"
13#include "components/invalidation/invalidation_export.h"
14#include "components/invalidation/sync_system_resources.h"
15#include "jingle/notifier/listener/push_client_observer.h"
16
17namespace notifier {
18class PushClient;
19}  // namespace notifier
20
21namespace syncer {
22
23// A PushClientChannel is an implementation of NetworkChannel that
24// routes messages through a PushClient.
25class INVALIDATION_EXPORT_PRIVATE PushClientChannel
26    : public SyncNetworkChannel,
27      public NON_EXPORTED_BASE(notifier::PushClientObserver) {
28 public:
29  // |push_client| is guaranteed to be destroyed only when this object
30  // is destroyed.
31  explicit PushClientChannel(scoped_ptr<notifier::PushClient> push_client);
32
33  virtual ~PushClientChannel();
34
35  // invalidation::NetworkChannel implementation.
36  virtual void SendMessage(const std::string& message) OVERRIDE;
37  virtual void RequestDetailedStatus(
38      base::Callback<void(const base::DictionaryValue&)> callback) OVERRIDE;
39
40  // SyncNetworkChannel implementation.
41  // If not connected, connects with the given credentials.  If
42  // already connected, the next connection attempt will use the given
43  // credentials.
44  virtual void UpdateCredentials(const std::string& email,
45      const std::string& token) OVERRIDE;
46  virtual int GetInvalidationClientType() OVERRIDE;
47
48  // notifier::PushClient::Observer implementation.
49  virtual void OnNotificationsEnabled() OVERRIDE;
50  virtual void OnNotificationsDisabled(
51      notifier::NotificationsDisabledReason reason) OVERRIDE;
52  virtual void OnIncomingNotification(
53      const notifier::Notification& notification) OVERRIDE;
54
55  const std::string& GetServiceContextForTest() const;
56
57  int64 GetSchedulingHashForTest() const;
58
59  static std::string EncodeMessageForTest(const std::string& message,
60                                          const std::string& service_context,
61                                          int64 scheduling_hash);
62
63  static bool DecodeMessageForTest(const std::string& notification,
64                                   std::string* message,
65                                   std::string* service_context,
66                                   int64* scheduling_hash);
67
68 private:
69  static void EncodeMessage(std::string* encoded_message,
70                            const std::string& message,
71                            const std::string& service_context,
72                            int64 scheduling_hash);
73  static bool DecodeMessage(const std::string& data,
74                            std::string* message,
75                            std::string* service_context,
76                            int64* scheduling_hash);
77  scoped_ptr<base::DictionaryValue> CollectDebugData() const;
78
79  scoped_ptr<notifier::PushClient> push_client_;
80  std::string service_context_;
81  int64 scheduling_hash_;
82
83  // This count is saved for displaying statatistics.
84  int sent_messages_count_;
85
86  DISALLOW_COPY_AND_ASSIGN(PushClientChannel);
87};
88
89}  // namespace syncer
90
91#endif  // COMPONENTS_INVALIDATION_PUSH_CLIENT_CHANNEL_H_
92