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_SOCKET_API_CONTROLLER_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_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 // A Socket wraps a low-level socket and includes housekeeping information that | |
32 // we need to manage it in the context of an extension. | |
33 class Socket { | |
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
It seems like this class could live entirely insid
miket_OOO
2011/12/02 21:06:36
Sounds great! Done.
| |
34 public: | |
35 explicit Socket(const Profile* profile); | |
36 ~Socket(); | |
37 | |
38 void set_src_extension_id(const std::string& src_extension_id) { | |
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
Having these properties as constructor parameters
miket_OOO
2011/12/02 21:06:36
Done.
| |
39 src_extension_id_ = src_extension_id; | |
40 } | |
41 const std::string& src_extension_id() { return src_extension_id_; } | |
42 void set_src_id(int src_id) { src_id_ = src_id; } | |
43 int src_id() { return src_id_; } | |
44 void set_src_url(const GURL& src_url) { src_url_ = src_url; } | |
45 const GURL& src_url() { return src_url_; } | |
46 | |
47 bool Connect(const net::IPEndPoint& ip_end_point); | |
48 void Close(); | |
49 int Write(const std::string message); | |
50 | |
51 private: | |
52 // TODO(miket): this metadata will enable us to pass events back to the | |
53 // extension that created this Socket. | |
54 const Profile* profile_; | |
55 int id_; | |
56 std::string src_extension_id_; | |
57 int src_id_; | |
58 GURL src_url_; | |
59 | |
60 net::UDPClientSocket* udp_client_socket_; | |
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
Will TCP socket functionality go into the same cla
miket_OOO
2011/12/02 21:06:36
I've thought about the same thing. I'd like to do
| |
61 bool is_connected_; | |
62 net::OldCompletionCallbackImpl<Socket> io_callback_; | |
63 | |
64 // A callback required by UDPClientSocket::Write(). | |
65 void OnIOComplete(int result); | |
66 }; | |
67 | |
68 // The SocketController singleton keeps track of all our Sockets, and provides | |
69 // a convenient set of methods to manipulate them. | |
70 class SocketController { | |
71 public: | |
72 static SocketController* GetInstance(); | |
73 | |
74 SocketController(); | |
75 virtual ~SocketController(); | |
76 | |
77 // Create/Destroy are a pair. They represent the allocation and deallocation | |
78 // of the Socket object in memory. | |
79 // | |
80 // TODO(miket): we currently require the app developer to remember to call | |
81 // Destroy, which is a buzzkill in JavaScript. I believe that to solve this, | |
82 // we'll have to associate each Socket with a creator extension, and then | |
83 // clean up when the extension goes out of scope. As the API is defined | |
84 // today, we're exposing only primitive socketIds to JavaScript, which seems | |
85 // to imply that we won't be able to garbage-collect when individual sockets | |
86 // "go out of scope" (in quotes because they never do). | |
Mihai Parparita -not on Chrome
2011/12/01 23:39:32
We could perhaps expose sockets as v8 objects, in
Aaron Boodman
2011/12/02 06:06:12
GC is non-deterministic so it's not usually a good
miket_OOO
2011/12/02 21:06:36
OK. I'll do that in a separate CL.
| |
87 int CreateUdp(const Profile* profile, const std::string& extension_id, | |
88 const GURL& src_url); | |
89 bool DestroyUdp(int socket_id); | |
90 | |
91 // Connect, Close, Read, and Write map to the equivalent methods in | |
92 // UDPClientSocket. | |
93 // | |
94 // TODO(miket): Implement Read. | |
95 bool ConnectUdp(int socket_id, const std::string address, int port); | |
96 void CloseUdp(int socket_id); | |
97 int WriteUdp(int socket_id, const std::string msg); | |
98 | |
99 // Converts a string IP address and integer port into a format that | |
100 // UDPClientSocket can deal with. Public so test harness can use it. | |
101 static bool CreateIPEndPoint(const std::string address, int port, | |
102 net::IPEndPoint* ip_end_point); | |
103 | |
104 private: | |
105 int next_socket_id_; | |
106 typedef std::map<int, Socket*> SocketMap; | |
107 SocketMap socket_map_; | |
108 | |
109 // Convenience method for accessing SocketMap. | |
110 Socket* GetSocket(int socket_id); | |
111 | |
112 friend struct DefaultSingletonTraits<SocketController>; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(SocketController); | |
115 }; | |
116 | |
117 } // namespace extensions | |
118 | |
119 #endif // CHROME_BROWSER_EXTENSIONS_SOCKET_API_CONTROLLER_H_ | |
OLD | NEW |