Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(775)

Side by Side Diff: chrome/browser/extensions/api/socket/socket_api.h

Issue 10273016: Refactor the socket API to remove onEvent callback in socket.create() function. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Update Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_
7 #pragma once 7 #pragma once
8 8
9 #include "base/memory/ref_counted.h" 9 #include "base/memory/ref_counted.h"
10 #include "chrome/browser/extensions/api/api_function.h" 10 #include "chrome/browser/extensions/api/api_function.h"
11 #include "net/base/io_buffer.h" 11 #include "net/base/io_buffer.h"
12 #include "net/base/ip_endpoint.h"
13 12
14 #include <string> 13 #include <string>
15 14
16 namespace extensions { 15 namespace extensions {
17 16
18 class APIResourceController; 17 class APIResourceController;
19 class APIResourceEventNotifier; 18 class APIResourceEventNotifier;
20 19
21 extern const char kBytesWrittenKey[]; 20 extern const char kBytesWrittenKey[];
22 extern const char kSocketIdKey[]; 21 extern const char kSocketIdKey[];
23 extern const char kUdpSocketType[]; 22 extern const char kUdpSocketType[];
24 23
24 class SocketExtensionFunction : public AsyncIOAPIFunction {
25 public:
26 virtual void Work() OVERRIDE;
27 virtual bool Respond() OVERRIDE;
28 };
29
25 // Many of these socket functions are synchronous in the sense that 30 // Many of these socket functions are synchronous in the sense that
26 // they don't involve blocking operations, but we've made them all 31 // they don't involve blocking operations, but we've made them all
27 // AsyncExtensionFunctions because the underlying UDPClientSocket 32 // AsyncExtensionFunctions because the underlying UDPClientSocket
28 // library wants all operations to happen on the same thread as the 33 // library wants all operations to happen on the same thread as the
29 // one that created the socket. Too bad. 34 // one that created the socket. Too bad.
30 35
31 class SocketCreateFunction : public AsyncIOAPIFunction { 36 class SocketCreateFunction : public SocketExtensionFunction {
32 public: 37 public:
33 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.create") 38 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.create")
34 39
35 SocketCreateFunction(); 40 SocketCreateFunction();
36 41
37 protected: 42 protected:
38 virtual ~SocketCreateFunction(); 43 virtual ~SocketCreateFunction();
39 44
40 // AsyncIOAPIFunction: 45 // AsyncIOAPIFunction:
41 virtual bool Prepare() OVERRIDE; 46 virtual bool Prepare() OVERRIDE;
42 virtual void Work() OVERRIDE; 47 virtual void Work() OVERRIDE;
43 virtual bool Respond() OVERRIDE;
44 48
45 private: 49 private:
46 enum SocketType { 50 enum SocketType {
47 kSocketTypeInvalid = -1, 51 kSocketTypeInvalid = -1,
48 kSocketTypeTCP, 52 kSocketTypeTCP,
49 kSocketTypeUDP 53 kSocketTypeUDP
50 }; 54 };
51 55
52 SocketType socket_type_; 56 SocketType socket_type_;
53 int src_id_; 57 int src_id_;
54 APIResourceEventNotifier* event_notifier_; 58 APIResourceEventNotifier* event_notifier_;
55 }; 59 };
56 60
57 class SocketDestroyFunction : public AsyncIOAPIFunction { 61 class SocketDestroyFunction : public SocketExtensionFunction {
58 public: 62 public:
59 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.destroy") 63 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.destroy")
60 64
61 protected: 65 protected:
62 virtual ~SocketDestroyFunction() {} 66 virtual ~SocketDestroyFunction() {}
63 67
64 // AsyncIOAPIFunction: 68 // AsyncIOAPIFunction:
65 virtual bool Prepare() OVERRIDE; 69 virtual bool Prepare() OVERRIDE;
66 virtual void Work() OVERRIDE; 70 virtual void Work() OVERRIDE;
67 virtual bool Respond() OVERRIDE;
68 71
69 private: 72 private:
70 int socket_id_; 73 int socket_id_;
71 }; 74 };
72 75
73 class SocketConnectFunction : public AsyncIOAPIFunction { 76 class SocketConnectFunction : public SocketExtensionFunction {
74 public: 77 public:
75 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.connect") 78 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.connect")
76 79
80 void OnCompleted(int result);
81
77 protected: 82 protected:
78 virtual ~SocketConnectFunction() {} 83 virtual ~SocketConnectFunction() {}
79 84
80 // AsyncIOAPIFunction: 85 // AsyncIOAPIFunction:
81 virtual bool Prepare() OVERRIDE; 86 virtual bool Prepare() OVERRIDE;
82 virtual void Work() OVERRIDE; 87 virtual void AsyncWorkStart() OVERRIDE;
83 virtual bool Respond() OVERRIDE;
84 88
85 private: 89 private:
86 int socket_id_; 90 int socket_id_;
87 std::string address_; 91 std::string address_;
88 int port_; 92 int port_;
89 }; 93 };
90 94
91 class SocketDisconnectFunction : public AsyncIOAPIFunction { 95 class SocketDisconnectFunction : public SocketExtensionFunction {
92 public: 96 public:
93 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.disconnect") 97 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.disconnect")
94 98
95 protected: 99 protected:
96 virtual ~SocketDisconnectFunction() {} 100 virtual ~SocketDisconnectFunction() {}
97 101
98 // AsyncIOAPIFunction: 102 // AsyncIOAPIFunction:
99 virtual bool Prepare() OVERRIDE; 103 virtual bool Prepare() OVERRIDE;
100 virtual void Work() OVERRIDE; 104 virtual void Work() OVERRIDE;
101 virtual bool Respond() OVERRIDE;
102 105
103 private: 106 private:
104 int socket_id_; 107 int socket_id_;
105 }; 108 };
106 109
107 class SocketBindFunction : public AsyncIOAPIFunction { 110 class SocketBindFunction : public SocketExtensionFunction {
108 protected: 111 protected:
109 virtual bool Prepare() OVERRIDE; 112 virtual bool Prepare() OVERRIDE;
110 virtual void Work() OVERRIDE; 113 virtual void Work() OVERRIDE;
111 virtual bool Respond() OVERRIDE;
112 114
113 private: 115 private:
114 int socket_id_; 116 int socket_id_;
115 std::string address_; 117 std::string address_;
116 int port_; 118 int port_;
117 119
118 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.bind") 120 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.bind")
119 }; 121 };
120 122
121 class SocketReadFunction : public AsyncIOAPIFunction { 123 class SocketReadFunction : public SocketExtensionFunction {
122 public: 124 public:
123 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.read") 125 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.read")
124 126
127 void OnCompleted(int result, scoped_refptr<net::IOBuffer> io_buffer);
128
125 protected: 129 protected:
126 virtual ~SocketReadFunction() {} 130 virtual ~SocketReadFunction() {}
127 131
128 // AsyncIOAPIFunction: 132 // AsyncIOAPIFunction:
129 virtual bool Prepare() OVERRIDE; 133 virtual bool Prepare() OVERRIDE;
130 virtual void Work() OVERRIDE; 134 virtual void AsyncWorkStart() OVERRIDE;
131 virtual bool Respond() OVERRIDE;
132 135
133 private: 136 private:
134 int socket_id_; 137 int socket_id_;
135 }; 138 };
136 139
137 class SocketWriteFunction : public AsyncIOAPIFunction { 140 class SocketWriteFunction : public SocketExtensionFunction {
138 public: 141 public:
139 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.write") 142 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.write")
140 143
141 SocketWriteFunction(); 144 SocketWriteFunction();
145 void OnCompleted(int result);
142 146
143 protected: 147 protected:
144 virtual ~SocketWriteFunction(); 148 virtual ~SocketWriteFunction();
145 149
146 // AsyncIOAPIFunction: 150 // AsyncIOAPIFunction:
147 virtual bool Prepare() OVERRIDE; 151 virtual bool Prepare() OVERRIDE;
148 virtual void Work() OVERRIDE; 152 virtual void AsyncWorkStart() OVERRIDE;
149 virtual bool Respond() OVERRIDE;
150 153
151 private: 154 private:
152 int socket_id_; 155 int socket_id_;
153 scoped_refptr<net::IOBufferWithSize> io_buffer_; 156 scoped_refptr<net::IOBufferWithSize> io_buffer_;
154 }; 157 };
155 158
156 class SocketRecvFromFunction : public AsyncIOAPIFunction { 159 class SocketRecvFromFunction : public SocketExtensionFunction {
157 public: 160 public:
158 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.recvFrom") 161 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.recvFrom")
159 162
163 void OnCompleted(int result,
164 scoped_refptr<net::IOBuffer> io_buffer,
165 const std::string& address,
166 int port);
167
160 protected: 168 protected:
161 virtual ~SocketRecvFromFunction(); 169 virtual ~SocketRecvFromFunction();
162 170
163 // AsyncIOAPIFunction 171 // AsyncIOAPIFunction
164 virtual bool Prepare() OVERRIDE; 172 virtual bool Prepare() OVERRIDE;
165 virtual void Work() OVERRIDE; 173 virtual void AsyncWorkStart() OVERRIDE;
166 virtual bool Respond() OVERRIDE;
167 174
168 private: 175 private:
169 int socket_id_; 176 int socket_id_;
170 net::IPEndPoint address_;
171 }; 177 };
172 178
173 class SocketSendToFunction : public AsyncIOAPIFunction { 179 class SocketSendToFunction : public SocketExtensionFunction {
174 public: 180 public:
175 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.sendTo") 181 DECLARE_EXTENSION_FUNCTION_NAME("experimental.socket.sendTo")
176 182
177 SocketSendToFunction(); 183 SocketSendToFunction();
178 184
179 protected: 185 protected:
180 virtual ~SocketSendToFunction(); 186 virtual ~SocketSendToFunction();
187 void OnCompleted(int result);
181 188
182 // AsyncIOAPIFunction: 189 // AsyncIOAPIFunction:
183 virtual bool Prepare() OVERRIDE; 190 virtual bool Prepare() OVERRIDE;
184 virtual void Work() OVERRIDE; 191 virtual void AsyncWorkStart() OVERRIDE;
185 virtual bool Respond() OVERRIDE;
186 192
187 private: 193 private:
188 int socket_id_; 194 int socket_id_;
189 scoped_refptr<net::IOBufferWithSize> io_buffer_; 195 scoped_refptr<net::IOBufferWithSize> io_buffer_;
190 std::string address_; 196 std::string address_;
191 int port_; 197 int port_;
192 }; 198 };
193 199
194 } // namespace extensions 200 } // namespace extensions
195 201
196 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_ 202 #endif // CHROME_BROWSER_EXTENSIONS_API_SOCKET_SOCKET_API_H_
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/socket/socket.cc ('k') | chrome/browser/extensions/api/socket/socket_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698