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