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 EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ |
6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/threading/thread_checker.h" | 14 #include "base/threading/thread_checker.h" |
15 #include "extensions/browser/api/api_resource_manager.h" | |
16 #include "extensions/browser/api/async_api_function.h" | 15 #include "extensions/browser/api/async_api_function.h" |
17 #include "extensions/browser/api/cast_channel/cast_socket.h" | 16 #include "extensions/browser/api/cast_channel/cast_socket.h" |
18 #include "extensions/browser/browser_context_keyed_api_factory.h" | 17 #include "extensions/browser/browser_context_keyed_api_factory.h" |
19 #include "extensions/common/api/cast_channel.h" | 18 #include "extensions/common/api/cast_channel.h" |
20 | 19 |
21 class CastChannelAPITest; | 20 class CastChannelAPITest; |
22 | 21 |
23 namespace content { | 22 namespace content { |
24 class BrowserContext; | 23 class BrowserContext; |
25 } | 24 } |
26 | 25 |
27 namespace net { | 26 namespace net { |
28 class IPEndPoint; | 27 class IPEndPoint; |
29 } | 28 } |
30 | 29 |
31 namespace extensions { | 30 namespace extensions { |
32 | 31 |
33 struct Event; | 32 struct Event; |
34 | 33 |
35 namespace api { | 34 namespace api { |
36 namespace cast_channel { | 35 namespace cast_channel { |
| 36 class CastSocketService; |
37 class Logger; | 37 class Logger; |
38 } // namespace cast_channel | 38 } // namespace cast_channel |
39 } // namespace api | 39 } // namespace api |
40 | 40 |
41 namespace cast_channel = api::cast_channel; | 41 namespace cast_channel = api::cast_channel; |
42 | 42 |
43 class CastChannelAPI : public BrowserContextKeyedAPI, | 43 class CastChannelAPI : public BrowserContextKeyedAPI, |
44 public base::SupportsWeakPtr<CastChannelAPI> { | 44 public base::SupportsWeakPtr<CastChannelAPI> { |
45 public: | 45 public: |
46 explicit CastChannelAPI(content::BrowserContext* context); | 46 explicit CastChannelAPI(content::BrowserContext* context); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 std::unique_ptr<base::Timer> injected_timeout_timer_; | 95 std::unique_ptr<base::Timer> injected_timeout_timer_; |
96 | 96 |
97 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI); | 97 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI); |
98 }; | 98 }; |
99 | 99 |
100 class CastChannelAsyncApiFunction : public AsyncApiFunction { | 100 class CastChannelAsyncApiFunction : public AsyncApiFunction { |
101 public: | 101 public: |
102 CastChannelAsyncApiFunction(); | 102 CastChannelAsyncApiFunction(); |
103 | 103 |
104 protected: | 104 protected: |
105 typedef ApiResourceManager<cast_channel::CastSocket>::ApiResourceData | |
106 SocketData; | |
107 | |
108 ~CastChannelAsyncApiFunction() override; | 105 ~CastChannelAsyncApiFunction() override; |
109 | 106 |
110 // AsyncApiFunction: | 107 // AsyncApiFunction: |
111 bool PrePrepare() override; | 108 bool PrePrepare() override; |
112 bool Respond() override; | 109 bool Respond() override; |
113 | 110 |
114 // Returns the socket corresponding to |channel_id| if one exists. Otherwise, | 111 // Returns the socket corresponding to |channel_id| if one exists. Otherwise, |
115 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes | 112 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes |
116 // the function, and returns null. | 113 // the function, and returns null. |
117 cast_channel::CastSocket* GetSocketOrCompleteWithError(int channel_id); | 114 cast_channel::CastSocket* GetSocketOrCompleteWithError(int channel_id); |
118 | 115 |
119 // Adds |socket| to |manager_| and returns the new channel_id. |manager_| | 116 // Adds |socket| to |manager_| and returns the new channel_id. |manager_| |
120 // assumes ownership of |socket|. | 117 // assumes ownership of |socket|. |
121 int AddSocket(cast_channel::CastSocket* socket); | 118 int AddSocket(std::unique_ptr<cast_channel::CastSocket> socket); |
122 | 119 |
123 // Removes the CastSocket corresponding to |channel_id| from the resource | 120 // Removes the CastSocket corresponding to |channel_id| from the resource |
124 // manager. | 121 // manager. |
125 void RemoveSocket(int channel_id); | 122 void RemoveSocket(int channel_id); |
126 | 123 |
127 // Sets the function result to a ChannelInfo obtained from the state of | 124 // Sets the function result to a ChannelInfo obtained from the state of |
128 // |socket|. | 125 // |socket|. |
129 void SetResultFromSocket(const cast_channel::CastSocket& socket); | 126 void SetResultFromSocket(const cast_channel::CastSocket& socket); |
130 | 127 |
131 // Sets the function result to a ChannelInfo populated with |channel_id| and | 128 // Sets the function result to a ChannelInfo populated with |channel_id| and |
132 // |error|. | 129 // |error|. |
133 void SetResultFromError(int channel_id, cast_channel::ChannelError error); | 130 void SetResultFromError(int channel_id, cast_channel::ChannelError error); |
134 | 131 |
135 // Returns the socket corresponding to |channel_id| if one exists, or null | 132 // Returns the socket corresponding to |channel_id| if one exists, or null |
136 // otherwise. | 133 // otherwise. |
137 cast_channel::CastSocket* GetSocket(int channel_id) const; | 134 cast_channel::CastSocket* GetSocket(int channel_id) const; |
138 | 135 |
139 private: | 136 private: |
140 // Sets the function result from |channel_info|. | 137 // Sets the function result from |channel_info|. |
141 void SetResultFromChannelInfo(const cast_channel::ChannelInfo& channel_info); | 138 void SetResultFromChannelInfo(const cast_channel::ChannelInfo& channel_info); |
142 | 139 |
143 // The collection of CastSocket API resources. | 140 // Manages creating and removing cast sockets. |
144 scoped_refptr<SocketData> sockets_; | 141 api::cast_channel::CastSocketService* cast_socket_service_; |
145 }; | 142 }; |
146 | 143 |
147 class CastChannelOpenFunction : public CastChannelAsyncApiFunction { | 144 class CastChannelOpenFunction : public CastChannelAsyncApiFunction { |
148 public: | 145 public: |
149 CastChannelOpenFunction(); | 146 CastChannelOpenFunction(); |
150 | 147 |
151 protected: | 148 protected: |
152 ~CastChannelOpenFunction() override; | 149 ~CastChannelOpenFunction() override; |
153 | 150 |
154 // AsyncApiFunction: | 151 // AsyncApiFunction: |
155 bool PrePrepare() override; | 152 bool PrePrepare() override; |
156 bool Prepare() override; | 153 bool Prepare() override; |
157 void AsyncWorkStart() override; | 154 void AsyncWorkStart() override; |
158 | 155 |
159 private: | 156 private: |
160 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) | 157 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) |
161 | 158 |
162 // Defines a callback used to send events to the extension's | 159 // Defines a callback used to send events to the extension's |
163 // EventRouter. | 160 // EventRouter. |
164 // Parameter #0 is the extension's ID. | 161 // Parameter #0 is a scoped pointer to the event payload. |
165 // Parameter #1 is a scoped pointer to the event payload. | 162 using EventDispatchCallback = base::Callback<void(std::unique_ptr<Event>)>; |
166 using EventDispatchCallback = | |
167 base::Callback<void(const std::string&, std::unique_ptr<Event>)>; | |
168 | 163 |
169 // Receives incoming messages and errors and provides additional API and | 164 // Receives incoming messages and errors and provides additional API and |
170 // origin socket context. | 165 // origin socket context. |
171 class CastMessageHandler : public cast_channel::CastTransport::Delegate { | 166 class CastMessageHandler : public cast_channel::CastTransport::Delegate { |
172 public: | 167 public: |
173 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb, | 168 CastMessageHandler(const EventDispatchCallback& ui_dispatch_cb, |
174 cast_channel::CastSocket* socket, | 169 cast_channel::CastSocket* socket, |
175 scoped_refptr<api::cast_channel::Logger> logger); | 170 scoped_refptr<api::cast_channel::Logger> logger); |
176 ~CastMessageHandler() override; | 171 ~CastMessageHandler() override; |
177 | 172 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 247 |
253 std::unique_ptr<cast_channel::Close::Params> params_; | 248 std::unique_ptr<cast_channel::Close::Params> params_; |
254 CastChannelAPI* api_; | 249 CastChannelAPI* api_; |
255 | 250 |
256 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); | 251 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); |
257 }; | 252 }; |
258 | 253 |
259 } // namespace extensions | 254 } // namespace extensions |
260 | 255 |
261 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | 256 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ |
OLD | NEW |