Refactor the socket API to remove onEvent callback in socket.create() function.


BUG=None
TEST=browser_tests --gtest_filter=SocketApiTest.*
TEST=unit_tests --gtest_filter=SocketTest.*

Review URL: http://codereview.chromium.org/10273016

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@135709 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/extensions/api/socket/socket.h b/chrome/browser/extensions/api/socket/socket.h
index d91a950..b877713 100644
--- a/chrome/browser/extensions/api/socket/socket.h
+++ b/chrome/browser/extensions/api/socket/socket.h
@@ -8,6 +8,7 @@
 
 #include <string>
 
+#include "base/callback.h"
 #include "base/memory/scoped_ptr.h"
 #include "chrome/browser/extensions/api/api_resource.h"
 #include "net/base/io_buffer.h"
@@ -20,6 +21,13 @@
 
 namespace extensions {
 
+typedef base::Callback<void(int)> CompletionCallback;
+typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>
+    ReadCompletionCallback;
+typedef base::Callback<
+  void(int, scoped_refptr<net::IOBuffer> io_buffer, const std::string&, int)>
+      RecvFromCompletionCallback;
+
 // A Socket wraps a low-level socket and includes housekeeping information that
 // we need to manage it in the context of an extension.
 class Socket : public APIResource {
@@ -27,36 +35,35 @@
   virtual ~Socket();
 
   // Returns net::OK if successful, or an error code otherwise.
-  virtual int Connect(const std::string& address, int port) = 0;
+  virtual void Connect(const std::string& address,
+                       int port,
+                       const CompletionCallback& callback) = 0;
   virtual void Disconnect() = 0;
 
   virtual int Bind(const std::string& address, int port) = 0;
 
   // Returns the number of bytes read into the buffer, or a negative number if
   // an error occurred.
-  virtual int Read(scoped_refptr<net::IOBuffer> io_buffer,
-                   int io_buffer_size) = 0;
+  virtual void Read(int count,
+                    const ReadCompletionCallback& callback) = 0;
 
   // Returns the number of bytes successfully written, or a negative error
   // code. Note that ERR_IO_PENDING means that the operation blocked, in which
   // case |event_notifier| (supplied at socket creation) will eventually be
   // called with the final result (again, either a nonnegative number of bytes
   // written, or a negative error).
-  virtual int Write(scoped_refptr<net::IOBuffer> io_buffer,
-                    int byte_count) = 0;
-
-  virtual int RecvFrom(scoped_refptr<net::IOBuffer> io_buffer,
-                       int io_buffer_size,
-                       net::IPEndPoint *address) = 0;
-  virtual int SendTo(scoped_refptr<net::IOBuffer> io_buffer,
+  virtual void Write(scoped_refptr<net::IOBuffer> io_buffer,
                      int byte_count,
-                     const std::string& address,
-                     int port) = 0;
+                     const CompletionCallback& callback) = 0;
 
-  virtual void OnDataRead(scoped_refptr<net::IOBuffer> io_buffer,
-                          net::IPEndPoint *address,
-                          int result);
-  virtual void OnWriteComplete(int result);
+  virtual void RecvFrom(int count,
+                        const RecvFromCompletionCallback& callback) = 0;
+
+  virtual void SendTo(scoped_refptr<net::IOBuffer> io_buffer,
+                      int byte_count,
+                      const std::string& address,
+                      int port,
+                      const CompletionCallback& callback) = 0;
 
   static bool StringAndPortToAddressList(const std::string& ip_address_str,
                                          int port,
@@ -70,7 +77,6 @@
 
  protected:
   explicit Socket(APIResourceEventNotifier* event_notifier);
-
   const std::string address_;
   int port_;
   bool is_connected_;