Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / net / socket / socket_libevent.cc
index c7b08be..cd40e49 100644 (file)
@@ -106,6 +106,13 @@ int SocketLibevent::AdoptConnectedSocket(SocketDescriptor socket,
   return OK;
 }
 
+SocketDescriptor SocketLibevent::ReleaseConnectedSocket() {
+  StopWatchingAndCleanUp();
+  SocketDescriptor socket_fd = socket_fd_;
+  socket_fd_ = kInvalidSocket;
+  return socket_fd;
+}
+
 int SocketLibevent::Bind(const SockaddrStorage& address) {
   DCHECK(thread_checker_.CalledOnValidThread());
   DCHECK_NE(kInvalidSocket, socket_fd_);
@@ -326,38 +333,13 @@ bool SocketLibevent::HasPeerAddress() const {
 void SocketLibevent::Close() {
   DCHECK(thread_checker_.CalledOnValidThread());
 
-  bool ok = accept_socket_watcher_.StopWatchingFileDescriptor();
-  DCHECK(ok);
-  ok = read_socket_watcher_.StopWatchingFileDescriptor();
-  DCHECK(ok);
-  ok = write_socket_watcher_.StopWatchingFileDescriptor();
-  DCHECK(ok);
+  StopWatchingAndCleanUp();
 
   if (socket_fd_ != kInvalidSocket) {
     if (IGNORE_EINTR(close(socket_fd_)) < 0)
       PLOG(ERROR) << "close() returned an error, errno=" << errno;
     socket_fd_ = kInvalidSocket;
   }
-
-  if (!accept_callback_.is_null()) {
-    accept_socket_ = NULL;
-    accept_callback_.Reset();
-  }
-
-  if (!read_callback_.is_null()) {
-    read_buf_ = NULL;
-    read_buf_len_ = 0;
-    read_callback_.Reset();
-  }
-
-  if (!write_callback_.is_null()) {
-    write_buf_ = NULL;
-    write_buf_len_ = 0;
-    write_callback_.Reset();
-  }
-
-  waiting_connect_ = false;
-  peer_address_.reset();
 }
 
 void SocketLibevent::OnFileCanReadWithoutBlocking(int fd) {
@@ -440,7 +422,7 @@ int SocketLibevent::DoRead(IOBuffer* buf, int buf_len) {
 }
 
 void SocketLibevent::ReadCompleted() {
-  int rv = DoRead(read_buf_, read_buf_len_);
+  int rv = DoRead(read_buf_.get(), read_buf_len_);
   if (rv == ERR_IO_PENDING)
     return;
 
@@ -457,7 +439,7 @@ int SocketLibevent::DoWrite(IOBuffer* buf, int buf_len) {
 }
 
 void SocketLibevent::WriteCompleted() {
-  int rv = DoWrite(write_buf_, write_buf_len_);
+  int rv = DoWrite(write_buf_.get(), write_buf_len_);
   if (rv == ERR_IO_PENDING)
     return;
 
@@ -468,4 +450,33 @@ void SocketLibevent::WriteCompleted() {
   base::ResetAndReturn(&write_callback_).Run(rv);
 }
 
+void SocketLibevent::StopWatchingAndCleanUp() {
+  bool ok = accept_socket_watcher_.StopWatchingFileDescriptor();
+  DCHECK(ok);
+  ok = read_socket_watcher_.StopWatchingFileDescriptor();
+  DCHECK(ok);
+  ok = write_socket_watcher_.StopWatchingFileDescriptor();
+  DCHECK(ok);
+
+  if (!accept_callback_.is_null()) {
+    accept_socket_ = NULL;
+    accept_callback_.Reset();
+  }
+
+  if (!read_callback_.is_null()) {
+    read_buf_ = NULL;
+    read_buf_len_ = 0;
+    read_callback_.Reset();
+  }
+
+  if (!write_callback_.is_null()) {
+    write_buf_ = NULL;
+    write_buf_len_ = 0;
+    write_callback_.Reset();
+  }
+
+  waiting_connect_ = false;
+  peer_address_.reset();
+}
+
 }  // namespace net