1 // Copyright 2013 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 CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/threading/thread_checker.h"
11 #include "chrome/browser/extensions/api/api_function.h"
12 #include "chrome/browser/extensions/api/api_resource_manager.h"
13 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h"
14 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h"
15 #include "chrome/browser/extensions/extension_function.h"
16 #include "chrome/common/extensions/api/cast_channel.h"
20 class CastChannelAPITest;
22 namespace extensions {
24 namespace cast_channel = api::cast_channel;
26 class CastChannelAPI : public ProfileKeyedAPI,
27 public cast_channel::CastSocket::Delegate {
30 explicit CastChannelAPI(Profile* profile);
32 static CastChannelAPI* Get(Profile* profile);
34 // ProfileKeyedAPI implementation.
35 static ProfileKeyedAPIFactory<CastChannelAPI>* GetFactoryInstance();
37 // Returns a new CastSocket that connects to |url| and is to be owned by
39 scoped_ptr<cast_channel::CastSocket> CreateCastSocket(
40 const std::string& extension_id, const GURL& gurl);
42 // Sets the CastSocket instance to be returned by CreateCastSocket for
44 void SetSocketForTest(scoped_ptr<cast_channel::CastSocket> socket_for_test);
47 friend class ProfileKeyedAPIFactory<CastChannelAPI>;
48 friend class ::CastChannelAPITest;
50 virtual ~CastChannelAPI();
52 // CastSocket::Delegate. Called on IO thread.
53 virtual void OnError(const cast_channel::CastSocket* socket,
54 cast_channel::ChannelError error) OVERRIDE;
55 virtual void OnMessage(const cast_channel::CastSocket* socket,
56 const cast_channel::MessageInfo& message) OVERRIDE;
58 // ProfileKeyedAPI implementation.
59 static const char* service_name() {
60 return "CastChannelAPI";
63 Profile* const profile_;
64 scoped_ptr<cast_channel::CastSocket> socket_for_test_;
66 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI);
69 class CastChannelAsyncApiFunction : public AsyncApiFunction {
71 CastChannelAsyncApiFunction();
74 virtual ~CastChannelAsyncApiFunction();
77 virtual bool PrePrepare() OVERRIDE;
78 virtual bool Respond() OVERRIDE;
80 // Returns the socket corresponding to |channel_id| if one exists. Otherwise,
81 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes
82 // the function, and returns null.
83 cast_channel::CastSocket* GetSocketOrCompleteWithError(int channel_id);
85 // Adds |socket| to |manager_| and returns the new channel_id. |manager_|
86 // assumes ownership of |socket|.
87 int AddSocket(cast_channel::CastSocket* socket);
89 // Removes the CastSocket corresponding to |channel_id| from the resource
91 void RemoveSocket(int channel_id);
93 // Sets the function result to a ChannelInfo obtained from the state of the
94 // CastSocket corresponding to |channel_id|.
95 void SetResultFromSocket(int channel_id);
97 // Sets the function result to a ChannelInfo with |error|.
98 void SetResultFromError(cast_channel::ChannelError error);
100 // Returns the socket corresponding to |channel_id| if one exists, or null
102 cast_channel::CastSocket* GetSocket(int channel_id);
105 // Sets the function result from |channel_info|.
106 void SetResultFromChannelInfo(
107 const cast_channel::ChannelInfo& channel_info);
109 // The API resource manager for CastSockets.
110 ApiResourceManager<cast_channel::CastSocket>* manager_;
112 // The result of the function.
113 cast_channel::ChannelError error_;
116 class CastChannelOpenFunction : public CastChannelAsyncApiFunction {
118 CastChannelOpenFunction();
121 virtual ~CastChannelOpenFunction();
124 virtual bool PrePrepare() OVERRIDE;
125 virtual bool Prepare() OVERRIDE;
126 virtual void AsyncWorkStart() OVERRIDE;
129 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN)
131 void OnOpen(int result);
133 scoped_ptr<cast_channel::Open::Params> params_;
134 // The id of the newly opened socket.
136 CastChannelAPI* api_;
138 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction);
141 class CastChannelSendFunction : public CastChannelAsyncApiFunction {
143 CastChannelSendFunction();
146 virtual ~CastChannelSendFunction();
149 virtual bool Prepare() OVERRIDE;
150 virtual void AsyncWorkStart() OVERRIDE;
153 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND)
155 void OnSend(int result);
157 scoped_ptr<cast_channel::Send::Params> params_;
159 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction);
162 class CastChannelCloseFunction : public CastChannelAsyncApiFunction {
164 CastChannelCloseFunction();
167 virtual ~CastChannelCloseFunction();
170 virtual bool Prepare() OVERRIDE;
171 virtual void AsyncWorkStart() OVERRIDE;
174 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE)
176 void OnClose(int result);
178 scoped_ptr<cast_channel::Close::Params> params_;
180 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction);
183 } // namespace extensions
185 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_