| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_CONTROLLER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_CONTROLLER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <map> | 10 #include <map> |
| 11 | 11 |
| 12 #include "base/memory/linked_ptr.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/singleton.h" | |
| 14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 15 #include "net/base/completion_callback.h" | 15 #include "net/base/completion_callback.h" |
| 16 | 16 |
| 17 class Profile; | 17 class Profile; |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 class ListValue; | 20 class ListValue; |
| 21 class Value; | 21 class Value; |
| 22 } | 22 } |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 class UDPClientSocket; | 25 class UDPClientSocket; |
| 26 class IPEndPoint; | 26 class IPEndPoint; |
| 27 } | 27 } |
| 28 | 28 |
| 29 namespace extensions { | 29 namespace extensions { |
| 30 | 30 |
| 31 class Socket; | 31 class Socket; |
| 32 | 32 |
| 33 // The SocketController singleton keeps track of all our Sockets, and provides | 33 // SocketController keeps track of a collection of Sockets and provides a |
| 34 // a convenient set of methods to manipulate them. | 34 // convenient set of methods to manipulate them. |
| 35 class SocketController { | 35 class SocketController { |
| 36 public: | 36 public: |
| 37 static SocketController* GetInstance(); | |
| 38 | |
| 39 SocketController(); | 37 SocketController(); |
| 40 virtual ~SocketController(); | 38 virtual ~SocketController(); |
| 41 | 39 |
| 42 // Create/Destroy are a pair. They represent the allocation and deallocation | 40 // Create/Destroy are a pair. They represent the allocation and deallocation |
| 43 // of the Socket object in memory. | 41 // of the Socket object in memory. |
| 44 // | 42 // |
| 45 // TODO(miket): we currently require the app developer to remember to call | 43 // TODO(miket): aa's suggestion to track lifetime of callbacks associated |
| 46 // Destroy, which is a buzzkill in JavaScript. I believe that to solve this, | 44 // with each socket, which will then let us clean up when we go out of scope |
| 47 // we'll have to associate each Socket with a creator extension, and then | 45 // rather than requiring that the app developer remember to call Destroy. |
| 48 // clean up when the extension goes out of scope. As the API is defined | |
| 49 // today, we're exposing only primitive socketIds to JavaScript, which seems | |
| 50 // to imply that we won't be able to garbage-collect when individual sockets | |
| 51 // "go out of scope" (in quotes because they never do). | |
| 52 int CreateUdp(const Profile* profile, const std::string& extension_id, | 46 int CreateUdp(const Profile* profile, const std::string& extension_id, |
| 53 const GURL& src_url); | 47 const GURL& src_url); |
| 54 bool DestroyUdp(int socket_id); | 48 bool DestroyUdp(int socket_id); |
| 55 | 49 |
| 56 // Connect, Close, Read, and Write map to the equivalent methods in | 50 // Connect, Close, Read, and Write map to the equivalent methods in |
| 57 // UDPClientSocket. | 51 // UDPClientSocket. |
| 58 // | 52 // |
| 59 // TODO(miket): Implement Read. | 53 // TODO(miket): Implement Read. |
| 60 bool ConnectUdp(int socket_id, const std::string address, int port); | 54 bool ConnectUdp(int socket_id, const std::string address, int port); |
| 61 void CloseUdp(int socket_id); | 55 void CloseUdp(int socket_id); |
| 62 int WriteUdp(int socket_id, const std::string msg); | 56 int WriteUdp(int socket_id, const std::string msg); |
| 63 | 57 |
| 64 // Converts a string IP address and integer port into a format that | 58 // Converts a string IP address and integer port into a format that |
| 65 // UDPClientSocket can deal with. Public so test harness can use it. | 59 // UDPClientSocket can deal with. Public so test harness can use it. |
| 66 static bool CreateIPEndPoint(const std::string address, int port, | 60 static bool CreateIPEndPoint(const std::string address, int port, |
| 67 net::IPEndPoint* ip_end_point); | 61 net::IPEndPoint* ip_end_point); |
| 68 | 62 |
| 69 private: | 63 private: |
| 70 int next_socket_id_; | 64 int next_socket_id_; |
| 71 typedef std::map<int, Socket*> SocketMap; | 65 typedef std::map<int, linked_ptr<Socket> > SocketMap; |
| 72 SocketMap socket_map_; | 66 SocketMap socket_map_; |
| 73 | 67 |
| 74 // Convenience method for accessing SocketMap. | 68 // Convenience method for accessing SocketMap. |
| 75 Socket* GetSocket(int socket_id); | 69 Socket* GetSocket(int socket_id); |
| 76 | 70 |
| 77 friend struct DefaultSingletonTraits<SocketController>; | |
| 78 | |
| 79 DISALLOW_COPY_AND_ASSIGN(SocketController); | 71 DISALLOW_COPY_AND_ASSIGN(SocketController); |
| 80 }; | 72 }; |
| 81 | 73 |
| 82 } // namespace extensions | 74 } // namespace extensions |
| 83 | 75 |
| 84 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_CONTROLLER_H_ | 76 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_CONTROLLER_H_ |
| OLD | NEW |