Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / cast_channel / cast_channel_api.h
blobebaf41867c7461caa50fc4d44f6b4d9916e30014
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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
8 #include <string>
10 #include "base/basictypes.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/thread_checker.h"
14 #include "extensions/browser/api/api_resource_manager.h"
15 #include "extensions/browser/api/async_api_function.h"
16 #include "extensions/browser/api/cast_channel/cast_socket.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
18 #include "extensions/common/api/cast_channel.h"
20 class GURL;
21 class CastChannelAPITest;
23 namespace content {
24 class BrowserContext;
27 namespace net {
28 class IPEndPoint;
31 namespace extensions {
33 namespace core_api {
34 namespace cast_channel {
35 class Logger;
36 } // namespace cast_channel
37 } // namespace core_api
39 namespace cast_channel = core_api::cast_channel;
41 class CastChannelAPI : public BrowserContextKeyedAPI,
42 public cast_channel::CastSocket::Delegate {
43 public:
44 explicit CastChannelAPI(content::BrowserContext* context);
46 static CastChannelAPI* Get(content::BrowserContext* context);
48 // BrowserContextKeyedAPI implementation.
49 static BrowserContextKeyedAPIFactory<CastChannelAPI>* GetFactoryInstance();
51 // Returns a pointer to the Logger member variable.
52 // TODO(imcheng): Consider whether it is possible for this class to own the
53 // CastSockets and make this class the sole owner of Logger.
54 // Alternatively,
55 // consider making Logger not ref-counted by passing a weak
56 // reference of Logger to the CastSockets instead.
57 scoped_refptr<cast_channel::Logger> GetLogger();
59 // Sets the CastSocket instance to be returned by CreateCastSocket for
60 // testing.
61 void SetSocketForTest(scoped_ptr<cast_channel::CastSocket> socket_for_test);
63 // Returns a test CastSocket instance, if it is defined.
64 // Otherwise returns a scoped_ptr with a NULL ptr value.
65 scoped_ptr<cast_channel::CastSocket> GetSocketForTest();
67 private:
68 friend class BrowserContextKeyedAPIFactory<CastChannelAPI>;
69 friend class ::CastChannelAPITest;
71 ~CastChannelAPI() override;
73 // CastSocket::Delegate. Called on IO thread.
74 void OnError(const cast_channel::CastSocket* socket,
75 cast_channel::ChannelError error_state,
76 const cast_channel::LastErrors& last_errors) override;
77 void OnMessage(const cast_channel::CastSocket* socket,
78 const cast_channel::MessageInfo& message) override;
80 // BrowserContextKeyedAPI implementation.
81 static const char* service_name() { return "CastChannelAPI"; }
83 content::BrowserContext* const browser_context_;
84 scoped_refptr<cast_channel::Logger> logger_;
85 scoped_ptr<cast_channel::CastSocket> socket_for_test_;
87 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI);
90 class CastChannelAsyncApiFunction : public AsyncApiFunction {
91 public:
92 CastChannelAsyncApiFunction();
94 protected:
95 ~CastChannelAsyncApiFunction() override;
97 // AsyncApiFunction:
98 bool PrePrepare() override;
99 bool Respond() override;
101 // Returns the socket corresponding to |channel_id| if one exists. Otherwise,
102 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
103 // the function, and returns null.
104 cast_channel::CastSocket* GetSocketOrCompleteWithError(int channel_id);
106 // Adds |socket| to |manager_| and returns the new channel_id. |manager_|
107 // assumes ownership of |socket|.
108 int AddSocket(cast_channel::CastSocket* socket);
110 // Removes the CastSocket corresponding to |channel_id| from the resource
111 // manager.
112 void RemoveSocket(int channel_id);
114 // Sets the function result to a ChannelInfo obtained from the state of
115 // |socket|.
116 void SetResultFromSocket(const cast_channel::CastSocket& socket);
118 // Sets the function result to a ChannelInfo populated with |channel_id| and
119 // |error|.
120 void SetResultFromError(int channel_id, cast_channel::ChannelError error);
122 // Returns the socket corresponding to |channel_id| if one exists, or null
123 // otherwise.
124 cast_channel::CastSocket* GetSocket(int channel_id);
126 private:
127 // Sets the function result from |channel_info|.
128 void SetResultFromChannelInfo(const cast_channel::ChannelInfo& channel_info);
130 // The API resource manager for CastSockets.
131 ApiResourceManager<cast_channel::CastSocket>* manager_;
133 // The result of the function.
134 cast_channel::ChannelError error_;
137 class CastChannelOpenFunction : public CastChannelAsyncApiFunction {
138 public:
139 CastChannelOpenFunction();
141 protected:
142 ~CastChannelOpenFunction() override;
144 // AsyncApiFunction:
145 bool PrePrepare() override;
146 bool Prepare() override;
147 void AsyncWorkStart() override;
149 private:
150 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN)
152 // Parses the cast:// or casts:// |url|, fills |connect_info| with the
153 // corresponding details, and returns true. Returns false if |url| is not a
154 // valid Cast URL.
155 static bool ParseChannelUrl(const GURL& url,
156 cast_channel::ConnectInfo* connect_info);
158 // Validates that |connect_info| represents a valid IP end point and returns a
159 // new IPEndPoint if so. Otherwise returns NULL.
160 static net::IPEndPoint* ParseConnectInfo(
161 const cast_channel::ConnectInfo& connect_info);
163 void OnOpen(int result);
165 scoped_ptr<cast_channel::Open::Params> params_;
166 // The id of the newly opened socket.
167 int new_channel_id_;
168 CastChannelAPI* api_;
169 scoped_ptr<cast_channel::ConnectInfo> connect_info_;
170 scoped_ptr<net::IPEndPoint> ip_endpoint_;
171 cast_channel::ChannelAuthType channel_auth_;
173 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest, TestParseChannelUrl);
174 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest, TestParseConnectInfo);
175 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction);
178 class CastChannelSendFunction : public CastChannelAsyncApiFunction {
179 public:
180 CastChannelSendFunction();
182 protected:
183 ~CastChannelSendFunction() override;
185 // AsyncApiFunction:
186 bool Prepare() override;
187 void AsyncWorkStart() override;
189 private:
190 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND)
192 void OnSend(int result);
194 scoped_ptr<cast_channel::Send::Params> params_;
196 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction);
199 class CastChannelCloseFunction : public CastChannelAsyncApiFunction {
200 public:
201 CastChannelCloseFunction();
203 protected:
204 ~CastChannelCloseFunction() override;
206 // AsyncApiFunction:
207 bool Prepare() override;
208 void AsyncWorkStart() override;
210 private:
211 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE)
213 void OnClose(int result);
215 scoped_ptr<cast_channel::Close::Params> params_;
217 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction);
220 class CastChannelGetLogsFunction : public CastChannelAsyncApiFunction {
221 public:
222 CastChannelGetLogsFunction();
224 protected:
225 ~CastChannelGetLogsFunction() override;
227 // AsyncApiFunction:
228 bool PrePrepare() override;
229 bool Prepare() override;
230 void AsyncWorkStart() override;
232 private:
233 DECLARE_EXTENSION_FUNCTION("cast.channel.getLogs", CAST_CHANNEL_GETLOGS)
235 CastChannelAPI* api_;
237 DISALLOW_COPY_AND_ASSIGN(CastChannelGetLogsFunction);
240 class CastChannelSetAuthorityKeysFunction : public CastChannelAsyncApiFunction {
241 public:
242 CastChannelSetAuthorityKeysFunction();
244 protected:
245 virtual ~CastChannelSetAuthorityKeysFunction();
247 // AsyncApiFunction:
248 virtual bool Prepare() override;
249 virtual void AsyncWorkStart() override;
251 private:
252 DECLARE_EXTENSION_FUNCTION("cast.channel.setAuthorityKeys",
253 CAST_CHANNEL_SETAUTHORITYKEYS)
255 scoped_ptr<cast_channel::SetAuthorityKeys::Params> params_;
257 DISALLOW_COPY_AND_ASSIGN(CastChannelSetAuthorityKeysFunction);
260 } // namespace extensions
262 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_