summaryrefslogtreecommitdiffstats
path: root/extensions/browser/api/cast_channel/cast_channel_api.h
blob: 9e3b79978c68aab3065406380a42bc5bb7f17f66 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// 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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
#define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_

#include <string>

#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread_checker.h"
#include "extensions/browser/api/api_resource_manager.h"
#include "extensions/browser/api/async_api_function.h"
#include "extensions/browser/api/cast_channel/cast_socket.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/common/api/cast_channel.h"

class CastChannelAPITest;

namespace content {
class BrowserContext;
}

namespace net {
class IPEndPoint;
}

namespace extensions {

struct Event;

namespace api {
namespace cast_channel {
class Logger;
}  // namespace cast_channel
}  // namespace api

namespace cast_channel = api::cast_channel;

class CastChannelAPI : public BrowserContextKeyedAPI,
                       public base::SupportsWeakPtr<CastChannelAPI> {
 public:
  explicit CastChannelAPI(content::BrowserContext* context);

  static CastChannelAPI* Get(content::BrowserContext* context);

  // BrowserContextKeyedAPI implementation.
  static BrowserContextKeyedAPIFactory<CastChannelAPI>* GetFactoryInstance();

  // Returns a pointer to the Logger member variable.
  // TODO(imcheng): Consider whether it is possible for this class to own the
  // CastSockets and make this class the sole owner of Logger.
  // Alternatively,
  // consider making Logger not ref-counted by passing a weak
  // reference of Logger to the CastSockets instead.
  scoped_refptr<cast_channel::Logger> GetLogger();

  // Sets the CastSocket instance to be used for testing.
  void SetSocketForTest(scoped_ptr<cast_channel::CastSocket> socket_for_test);

  // Returns a test CastSocket instance, if it is defined.
  // Otherwise returns a scoped_ptr with a nullptr value.
  scoped_ptr<cast_channel::CastSocket> GetSocketForTest();

  // Returns the API browser context.
  content::BrowserContext* GetBrowserContext() const;

  // Sets injected ping timeout timer for testing.
  void SetPingTimeoutTimerForTest(scoped_ptr<base::Timer> timer);

  // Gets the injected ping timeout timer, if set.
  // Returns a null scoped ptr if there is no injected timer.
  scoped_ptr<base::Timer> GetInjectedTimeoutTimerForTest();

  // Sends an event to the extension's EventRouter, if it exists.
  void SendEvent(const std::string& extension_id, scoped_ptr<Event> event);

 private:
  friend class BrowserContextKeyedAPIFactory<CastChannelAPI>;
  friend class ::CastChannelAPITest;
  friend class CastTransportDelegate;

  ~CastChannelAPI() override;

  // BrowserContextKeyedAPI implementation.
  static const char* service_name() { return "CastChannelAPI"; }

  content::BrowserContext* const browser_context_;
  scoped_refptr<cast_channel::Logger> logger_;
  scoped_ptr<cast_channel::CastSocket> socket_for_test_;
  scoped_ptr<base::Timer> injected_timeout_timer_;

  DISALLOW_COPY_AND_ASSIGN(CastChannelAPI);
};

class CastChannelAsyncApiFunction : public AsyncApiFunction {
 public:
  CastChannelAsyncApiFunction();

 protected:
  typedef ApiResourceManager<cast_channel::CastSocket>::ApiResourceData
      SocketData;

  ~CastChannelAsyncApiFunction() override;

  // AsyncApiFunction:
  bool PrePrepare() override;
  bool Respond() override;

  // Returns the socket corresponding to |channel_id| if one exists.  Otherwise,
  // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
  // the function, and returns null.
  cast_channel::CastSocket* GetSocketOrCompleteWithError(int channel_id);

  // Adds |socket| to |manager_| and returns the new channel_id.  |manager_|
  // assumes ownership of |socket|.
  int AddSocket(cast_channel::CastSocket* socket);

  // Removes the CastSocket corresponding to |channel_id| from the resource
  // manager.
  void RemoveSocket(int channel_id);

  // Sets the function result to a ChannelInfo obtained from the state of
  // |socket|.
  void SetResultFromSocket(const cast_channel::CastSocket& socket);

  // Sets the function result to a ChannelInfo populated with |channel_id| and
  // |error|.
  void SetResultFromError(int channel_id, cast_channel::ChannelError error);

  // Returns the socket corresponding to |channel_id| if one exists, or null
  // otherwise.
  cast_channel::CastSocket* GetSocket(int channel_id) const;

 private:
  // Sets the function result from |channel_info|.
  void SetResultFromChannelInfo(const cast_channel::ChannelInfo& channel_info);

  // The collection of CastSocket API resources.
  scoped_refptr<SocketData> sockets_;
};

class CastChannelOpenFunction : public CastChannelAsyncApiFunction {
 public:
  CastChannelOpenFunction();

 protected:
  ~CastChannelOpenFunction() override;

  // AsyncApiFunction:
  bool PrePrepare() override;
  bool Prepare() override;
  void AsyncWorkStart() override;

 private:
  DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN)

  // Defines a callback used to send events to the extension's
  // EventRouter.
  //     Parameter #0 is the extension's ID.
  //     Parameter #1 is a scoped pointer to the event payload.
  using EventDispatchCallback =
      base::Callback<void(const std::string&, scoped_ptr<Event>)>;

  // Receives incoming messages and errors and provides additional API and
  // origin socket context.
  class CastMessageHandler : public cast_channel::CastTransport::Delegate {
   public:
    CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb,
                       cast_channel::CastSocket* socket,
                       scoped_refptr<api::cast_channel::Logger> logger);
    ~CastMessageHandler() override;

    // CastTransport::Delegate implementation.
    void OnError(cast_channel::ChannelError error_state) override;
    void OnMessage(const cast_channel::CastMessage& message) override;
    void Start() override;

   private:
    // Callback for sending events to the extension.
    // Should be bound to a weak pointer, to prevent any use-after-free
    // conditions.
    EventDispatchCallback const ui_dispatch_cb_;
    cast_channel::CastSocket* const socket_;
    // Logger object for reporting error details.
    scoped_refptr<api::cast_channel::Logger> logger_;

    DISALLOW_COPY_AND_ASSIGN(CastMessageHandler);
  };

  // Validates that |connect_info| represents a valid IP end point and returns a
  // new IPEndPoint if so.  Otherwise returns nullptr.
  static net::IPEndPoint* ParseConnectInfo(
      const cast_channel::ConnectInfo& connect_info);

  void OnOpen(cast_channel::ChannelError result);

  scoped_ptr<cast_channel::Open::Params> params_;
  // The id of the newly opened socket.
  int new_channel_id_;
  CastChannelAPI* api_;
  scoped_ptr<net::IPEndPoint> ip_endpoint_;
  cast_channel::ChannelAuthType channel_auth_;
  base::TimeDelta liveness_timeout_;
  base::TimeDelta ping_interval_;

  FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest, TestParseConnectInfo);
  DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction);
};

class CastChannelSendFunction : public CastChannelAsyncApiFunction {
 public:
  CastChannelSendFunction();

 protected:
  ~CastChannelSendFunction() override;

  // AsyncApiFunction:
  bool Prepare() override;
  void AsyncWorkStart() override;

 private:
  DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND)

  void OnSend(int result);

  scoped_ptr<cast_channel::Send::Params> params_;

  DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction);
};

class CastChannelCloseFunction : public CastChannelAsyncApiFunction {
 public:
  CastChannelCloseFunction();

 protected:
  ~CastChannelCloseFunction() override;

  // AsyncApiFunction:
  bool Prepare() override;
  void AsyncWorkStart() override;

 private:
  DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE)

  void OnClose(int result);

  scoped_ptr<cast_channel::Close::Params> params_;

  DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction);
};

class CastChannelGetLogsFunction : public CastChannelAsyncApiFunction {
 public:
  CastChannelGetLogsFunction();

 protected:
  ~CastChannelGetLogsFunction() override;

  // AsyncApiFunction:
  bool PrePrepare() override;
  bool Prepare() override;
  void AsyncWorkStart() override;

 private:
  DECLARE_EXTENSION_FUNCTION("cast.channel.getLogs", CAST_CHANNEL_GETLOGS)

  CastChannelAPI* api_;

  DISALLOW_COPY_AND_ASSIGN(CastChannelGetLogsFunction);
};

class CastChannelSetAuthorityKeysFunction : public CastChannelAsyncApiFunction {
 public:
  CastChannelSetAuthorityKeysFunction();

 protected:
  ~CastChannelSetAuthorityKeysFunction() override;

  // AsyncApiFunction:
  bool Prepare() override;
  void AsyncWorkStart() override;

 private:
  DECLARE_EXTENSION_FUNCTION("cast.channel.setAuthorityKeys",
                             CAST_CHANNEL_SETAUTHORITYKEYS)

  scoped_ptr<cast_channel::SetAuthorityKeys::Params> params_;

  DISALLOW_COPY_AND_ASSIGN(CastChannelSetAuthorityKeysFunction);
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_