OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_CONTROLLER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 #include <map> |
| 11 |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/singleton.h" |
| 14 #include "googleurl/src/gurl.h" |
| 15 #include "net/base/completion_callback.h" |
| 16 |
| 17 class Profile; |
| 18 |
| 19 namespace base { |
| 20 class ListValue; |
| 21 class Value; |
| 22 } |
| 23 |
| 24 namespace net { |
| 25 class UDPClientSocket; |
| 26 class IPEndPoint; |
| 27 } |
| 28 |
| 29 namespace extensions { |
| 30 |
| 31 class Socket; |
| 32 |
| 33 // The SocketController singleton keeps track of all our Sockets, and provides |
| 34 // a convenient set of methods to manipulate them. |
| 35 class SocketController { |
| 36 public: |
| 37 static SocketController* GetInstance(); |
| 38 |
| 39 SocketController(); |
| 40 virtual ~SocketController(); |
| 41 |
| 42 // Create/Destroy are a pair. They represent the allocation and deallocation |
| 43 // of the Socket object in memory. |
| 44 // |
| 45 // TODO(miket): we currently require the app developer to remember to call |
| 46 // Destroy, which is a buzzkill in JavaScript. I believe that to solve this, |
| 47 // we'll have to associate each Socket with a creator extension, and then |
| 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, |
| 53 const GURL& src_url); |
| 54 bool DestroyUdp(int socket_id); |
| 55 |
| 56 // Connect, Close, Read, and Write map to the equivalent methods in |
| 57 // UDPClientSocket. |
| 58 // |
| 59 // TODO(miket): Implement Read. |
| 60 bool ConnectUdp(int socket_id, const std::string address, int port); |
| 61 void CloseUdp(int socket_id); |
| 62 int WriteUdp(int socket_id, const std::string msg); |
| 63 |
| 64 // Converts a string IP address and integer port into a format that |
| 65 // UDPClientSocket can deal with. Public so test harness can use it. |
| 66 static bool CreateIPEndPoint(const std::string address, int port, |
| 67 net::IPEndPoint* ip_end_point); |
| 68 |
| 69 private: |
| 70 int next_socket_id_; |
| 71 typedef std::map<int, Socket*> SocketMap; |
| 72 SocketMap socket_map_; |
| 73 |
| 74 // Convenience method for accessing SocketMap. |
| 75 Socket* GetSocket(int socket_id); |
| 76 |
| 77 friend struct DefaultSingletonTraits<SocketController>; |
| 78 |
| 79 DISALLOW_COPY_AND_ASSIGN(SocketController); |
| 80 }; |
| 81 |
| 82 } // namespace extensions |
| 83 |
| 84 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_CONTROLLER_H_ |
OLD | NEW |