| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | |
| 7 | |
| 8 #include <string> | |
| 9 | |
| 10 #include "base/basictypes.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/threading/thread_checker.h" | |
| 13 #include "chrome/browser/extensions/api/cast_channel/cast_socket.h" | |
| 14 #include "chrome/common/extensions/api/cast_channel.h" | |
| 15 #include "extensions/browser/api/api_resource_manager.h" | |
| 16 #include "extensions/browser/api/async_api_function.h" | |
| 17 #include "extensions/browser/browser_context_keyed_api_factory.h" | |
| 18 | |
| 19 class GURL; | |
| 20 class CastChannelAPITest; | |
| 21 | |
| 22 namespace content { | |
| 23 class BrowserContext; | |
| 24 } | |
| 25 | |
| 26 namespace net { | |
| 27 class IPEndPoint; | |
| 28 } | |
| 29 | |
| 30 namespace extensions { | |
| 31 | |
| 32 namespace cast_channel = api::cast_channel; | |
| 33 | |
| 34 class CastChannelAPI : public BrowserContextKeyedAPI, | |
| 35 public cast_channel::CastSocket::Delegate { | |
| 36 public: | |
| 37 explicit CastChannelAPI(content::BrowserContext* context); | |
| 38 | |
| 39 static CastChannelAPI* Get(content::BrowserContext* context); | |
| 40 | |
| 41 // BrowserContextKeyedAPI implementation. | |
| 42 static BrowserContextKeyedAPIFactory<CastChannelAPI>* GetFactoryInstance(); | |
| 43 | |
| 44 // Returns a new CastSocket that connects to |ip_endpoint| with authentication | |
| 45 // |channel_auth| and is to be owned by |extension_id|. | |
| 46 scoped_ptr<cast_channel::CastSocket> CreateCastSocket( | |
| 47 const std::string& extension_id, | |
| 48 const net::IPEndPoint& ip_endpoint, | |
| 49 cast_channel::ChannelAuthType channel_auth, | |
| 50 const base::TimeDelta& timeout); | |
| 51 | |
| 52 // Sets the CastSocket instance to be returned by CreateCastSocket for | |
| 53 // testing. | |
| 54 void SetSocketForTest(scoped_ptr<cast_channel::CastSocket> socket_for_test); | |
| 55 | |
| 56 private: | |
| 57 friend class BrowserContextKeyedAPIFactory<CastChannelAPI>; | |
| 58 friend class ::CastChannelAPITest; | |
| 59 | |
| 60 virtual ~CastChannelAPI(); | |
| 61 | |
| 62 // CastSocket::Delegate. Called on IO thread. | |
| 63 virtual void OnError(const cast_channel::CastSocket* socket, | |
| 64 cast_channel::ChannelError error) OVERRIDE; | |
| 65 virtual void OnMessage(const cast_channel::CastSocket* socket, | |
| 66 const cast_channel::MessageInfo& message) OVERRIDE; | |
| 67 | |
| 68 // BrowserContextKeyedAPI implementation. | |
| 69 static const char* service_name() { return "CastChannelAPI"; } | |
| 70 | |
| 71 content::BrowserContext* const browser_context_; | |
| 72 scoped_ptr<cast_channel::CastSocket> socket_for_test_; | |
| 73 | |
| 74 DISALLOW_COPY_AND_ASSIGN(CastChannelAPI); | |
| 75 }; | |
| 76 | |
| 77 class CastChannelAsyncApiFunction : public AsyncApiFunction { | |
| 78 public: | |
| 79 CastChannelAsyncApiFunction(); | |
| 80 | |
| 81 protected: | |
| 82 virtual ~CastChannelAsyncApiFunction(); | |
| 83 | |
| 84 // AsyncApiFunction: | |
| 85 virtual bool PrePrepare() OVERRIDE; | |
| 86 virtual bool Respond() OVERRIDE; | |
| 87 | |
| 88 // Returns the socket corresponding to |channel_id| if one exists. Otherwise, | |
| 89 // sets the function result with CHANNEL_ERROR_INVALID_CHANNEL_ID, completes | |
| 90 // the function, and returns null. | |
| 91 cast_channel::CastSocket* GetSocketOrCompleteWithError(int channel_id); | |
| 92 | |
| 93 // Adds |socket| to |manager_| and returns the new channel_id. |manager_| | |
| 94 // assumes ownership of |socket|. | |
| 95 int AddSocket(cast_channel::CastSocket* socket); | |
| 96 | |
| 97 // Removes the CastSocket corresponding to |channel_id| from the resource | |
| 98 // manager. | |
| 99 void RemoveSocket(int channel_id); | |
| 100 | |
| 101 // Sets the function result to a ChannelInfo obtained from the state of the | |
| 102 // CastSocket corresponding to |channel_id|. | |
| 103 void SetResultFromSocket(int channel_id); | |
| 104 | |
| 105 // Sets the function result to a ChannelInfo with |error|. | |
| 106 void SetResultFromError(cast_channel::ChannelError error); | |
| 107 | |
| 108 // Returns the socket corresponding to |channel_id| if one exists, or null | |
| 109 // otherwise. | |
| 110 cast_channel::CastSocket* GetSocket(int channel_id); | |
| 111 | |
| 112 private: | |
| 113 // Sets the function result from |channel_info|. | |
| 114 void SetResultFromChannelInfo(const cast_channel::ChannelInfo& channel_info); | |
| 115 | |
| 116 // The API resource manager for CastSockets. | |
| 117 ApiResourceManager<cast_channel::CastSocket>* manager_; | |
| 118 | |
| 119 // The result of the function. | |
| 120 cast_channel::ChannelError error_; | |
| 121 }; | |
| 122 | |
| 123 class CastChannelOpenFunction : public CastChannelAsyncApiFunction { | |
| 124 public: | |
| 125 CastChannelOpenFunction(); | |
| 126 | |
| 127 protected: | |
| 128 virtual ~CastChannelOpenFunction(); | |
| 129 | |
| 130 // AsyncApiFunction: | |
| 131 virtual bool PrePrepare() OVERRIDE; | |
| 132 virtual bool Prepare() OVERRIDE; | |
| 133 virtual void AsyncWorkStart() OVERRIDE; | |
| 134 | |
| 135 private: | |
| 136 DECLARE_EXTENSION_FUNCTION("cast.channel.open", CAST_CHANNEL_OPEN) | |
| 137 | |
| 138 // Parses the cast:// or casts:// |url|, fills |connect_info| with the | |
| 139 // corresponding details, and returns true. Returns false if |url| is not a | |
| 140 // valid Cast URL. | |
| 141 static bool ParseChannelUrl(const GURL& url, | |
| 142 cast_channel::ConnectInfo* connect_info); | |
| 143 | |
| 144 // Validates that |connect_info| represents a valid IP end point and returns a | |
| 145 // new IPEndPoint if so. Otherwise returns NULL. | |
| 146 static net::IPEndPoint* ParseConnectInfo( | |
| 147 const cast_channel::ConnectInfo& connect_info); | |
| 148 | |
| 149 void OnOpen(int result); | |
| 150 | |
| 151 scoped_ptr<cast_channel::Open::Params> params_; | |
| 152 // The id of the newly opened socket. | |
| 153 int new_channel_id_; | |
| 154 CastChannelAPI* api_; | |
| 155 scoped_ptr<cast_channel::ConnectInfo> connect_info_; | |
| 156 scoped_ptr<net::IPEndPoint> ip_endpoint_; | |
| 157 cast_channel::ChannelAuthType channel_auth_; | |
| 158 | |
| 159 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest, TestParseChannelUrl); | |
| 160 FRIEND_TEST_ALL_PREFIXES(CastChannelOpenFunctionTest, TestParseConnectInfo); | |
| 161 DISALLOW_COPY_AND_ASSIGN(CastChannelOpenFunction); | |
| 162 }; | |
| 163 | |
| 164 class CastChannelSendFunction : public CastChannelAsyncApiFunction { | |
| 165 public: | |
| 166 CastChannelSendFunction(); | |
| 167 | |
| 168 protected: | |
| 169 virtual ~CastChannelSendFunction(); | |
| 170 | |
| 171 // AsyncApiFunction: | |
| 172 virtual bool Prepare() OVERRIDE; | |
| 173 virtual void AsyncWorkStart() OVERRIDE; | |
| 174 | |
| 175 private: | |
| 176 DECLARE_EXTENSION_FUNCTION("cast.channel.send", CAST_CHANNEL_SEND) | |
| 177 | |
| 178 void OnSend(int result); | |
| 179 | |
| 180 scoped_ptr<cast_channel::Send::Params> params_; | |
| 181 | |
| 182 DISALLOW_COPY_AND_ASSIGN(CastChannelSendFunction); | |
| 183 }; | |
| 184 | |
| 185 class CastChannelCloseFunction : public CastChannelAsyncApiFunction { | |
| 186 public: | |
| 187 CastChannelCloseFunction(); | |
| 188 | |
| 189 protected: | |
| 190 virtual ~CastChannelCloseFunction(); | |
| 191 | |
| 192 // AsyncApiFunction: | |
| 193 virtual bool Prepare() OVERRIDE; | |
| 194 virtual void AsyncWorkStart() OVERRIDE; | |
| 195 | |
| 196 private: | |
| 197 DECLARE_EXTENSION_FUNCTION("cast.channel.close", CAST_CHANNEL_CLOSE) | |
| 198 | |
| 199 void OnClose(int result); | |
| 200 | |
| 201 scoped_ptr<cast_channel::Close::Params> params_; | |
| 202 | |
| 203 DISALLOW_COPY_AND_ASSIGN(CastChannelCloseFunction); | |
| 204 }; | |
| 205 | |
| 206 } // namespace extensions | |
| 207 | |
| 208 #endif // CHROME_BROWSER_EXTENSIONS_API_CAST_CHANNEL_CAST_CHANNEL_API_H_ | |
| OLD | NEW |