diff --git a/base/BUILD.gn b/base/BUILD.gn index 5ad4e4869019..e3a1b37fe2d8 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -972,6 +972,7 @@ component("base") { "files/file_path_watcher_stub.cc", "files/file_starboard.cc", "files/file_util_starboard.cc", + # "files/file_util_posix.cc", "files/memory_mapped_file_starboard.cc", "memory/page_size_starboard.cc", "memory/platform_shared_memory_mapper_starboard.cc", diff --git a/base/files/file_descriptor_watcher_posix.cc b/base/files/file_descriptor_watcher_posix.cc index cb520db19a39..47e285ce6aed 100644 --- a/base/files/file_descriptor_watcher_posix.cc +++ b/base/files/file_descriptor_watcher_posix.cc @@ -46,8 +46,8 @@ class FileDescriptorWatcher::Controller::Watcher friend class FileDescriptorWatcher; // MessagePumpForIO::FdWatcher: - void OnFileCanReadWithoutBlocking(int fd) override; - void OnFileCanWriteWithoutBlocking(int fd) override; + void OnSocketReadyToRead(int fd) override; + void OnSocketReadyToWrite(int fd) override; // CurrentThread::DestructionObserver: void WillDestroyCurrentMessageLoop() override; @@ -122,7 +122,7 @@ void FileDescriptorWatcher::Controller::Watcher::StartWatching() { } } -void FileDescriptorWatcher::Controller::Watcher::OnFileCanReadWithoutBlocking( +void FileDescriptorWatcher::Controller::Watcher::OnSocketReadyToRead( int fd) { DCHECK_EQ(fd_, fd); DCHECK_EQ(MessagePumpForIO::WATCH_READ, mode_); @@ -133,7 +133,7 @@ void FileDescriptorWatcher::Controller::Watcher::OnFileCanReadWithoutBlocking( FROM_HERE, BindOnce(&Controller::RunCallback, controller_)); } -void FileDescriptorWatcher::Controller::Watcher::OnFileCanWriteWithoutBlocking( +void FileDescriptorWatcher::Controller::Watcher::OnSocketReadyToWrite( int fd) { DCHECK_EQ(fd_, fd); DCHECK_EQ(MessagePumpForIO::WATCH_WRITE, mode_); diff --git a/base/files/file_util_starboard.cc b/base/files/file_util_starboard.cc index cdc2c8f9b00d..c7e236ab9fd1 100644 --- a/base/files/file_util_starboard.cc +++ b/base/files/file_util_starboard.cc @@ -476,6 +476,17 @@ FilePath MakeAbsoluteFilePath(const FilePath& input) { return input; } +bool SetNonBlocking(int fd) { + const int flags = fcntl(fd, F_GETFL); + if (flags == -1) + return false; + if (flags & O_NONBLOCK) + return true; + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) + return false; + return true; +} + namespace internal { bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) { diff --git a/base/message_loop/fd_watch_controller_posix_unittest.cc b/base/message_loop/fd_watch_controller_posix_unittest.cc index 87fdb1652d2a..5b025a18895a 100644 --- a/base/message_loop/fd_watch_controller_posix_unittest.cc +++ b/base/message_loop/fd_watch_controller_posix_unittest.cc @@ -59,12 +59,12 @@ class FdWatchControllerPosixTest : public testing::Test { class TestHandler : public MessagePumpForIO::FdWatcher { public: - void OnFileCanReadWithoutBlocking(int fd) override { + void OnSocketReadyToRead(int fd) override { watcher_to_delete_ = nullptr; is_readable_ = true; RunLoop::QuitCurrentWhenIdleDeprecated(); } - void OnFileCanWriteWithoutBlocking(int fd) override { + void OnSocketReadyToWrite(int fd) override { watcher_to_delete_ = nullptr; is_writable_ = true; RunLoop::QuitCurrentWhenIdleDeprecated(); @@ -102,7 +102,7 @@ class CallClosureHandler : public MessagePumpForIO::FdWatcher { } // base::WatchableIOMessagePumpPosix::FdWatcher: - void OnFileCanReadWithoutBlocking(int fd) override { + void OnSocketReadyToRead(int fd) override { // Empty the pipe buffer to reset the event. Otherwise libevent // implementation of MessageLoop may call the event handler again even if // |read_closure_| below quits the RunLoop. @@ -118,7 +118,7 @@ class CallClosureHandler : public MessagePumpForIO::FdWatcher { std::move(read_closure_).Run(); } - void OnFileCanWriteWithoutBlocking(int fd) override { + void OnSocketReadyToWrite(int fd) override { ASSERT_FALSE(write_closure_.is_null()); std::move(write_closure_).Run(); } @@ -209,7 +209,7 @@ class ReaderWriterHandler : public MessagePumpForIO::FdWatcher { ReaderWriterHandler& operator=(const ReaderWriterHandler&) = delete; // base::WatchableIOMessagePumpPosix::FdWatcher: - void OnFileCanReadWithoutBlocking(int fd) override { + void OnSocketReadyToRead(int fd) override { if (when_ == kOnReadEvent) { DoAction(); } else { @@ -218,7 +218,7 @@ class ReaderWriterHandler : public MessagePumpForIO::FdWatcher { } } - void OnFileCanWriteWithoutBlocking(int fd) override { + void OnSocketReadyToWrite(int fd) override { if (when_ == kOnWriteEvent) { DoAction(); } else { diff --git a/base/message_loop/message_pump_fuchsia.cc b/base/message_loop/message_pump_fuchsia.cc index 5163464346ba..8c734d3c7bd9 100644 --- a/base/message_loop/message_pump_fuchsia.cc +++ b/base/message_loop/message_pump_fuchsia.cc @@ -133,9 +133,9 @@ void MessagePumpFuchsia::FdWatchController::OnZxHandleSignalled( // work that would touch |this|. bool* was_stopped = was_stopped_; if (filtered_events & FDIO_EVT_WRITABLE) - watcher_->OnFileCanWriteWithoutBlocking(fd_); + watcher_->OnSocketReadyToWrite(fd_); if (!*was_stopped && (filtered_events & FDIO_EVT_READABLE)) - watcher_->OnFileCanReadWithoutBlocking(fd_); + watcher_->OnSocketReadyToRead(fd_); // Don't add additional work here without checking |*was_stopped_| again. } diff --git a/base/message_loop/message_pump_glib.cc b/base/message_loop/message_pump_glib.cc index dfc8a9555784..687381fa5268 100644 --- a/base/message_loop/message_pump_glib.cc +++ b/base/message_loop/message_pump_glib.cc @@ -488,14 +488,14 @@ void MessagePumpGlib::FdWatchController::NotifyCanRead() { if (!watcher_) return; DCHECK(poll_fd_); - watcher_->OnFileCanReadWithoutBlocking(poll_fd_->fd); + watcher_->OnSocketReadyToRead(poll_fd_->fd); } void MessagePumpGlib::FdWatchController::NotifyCanWrite() { if (!watcher_) return; DCHECK(poll_fd_); - watcher_->OnFileCanWriteWithoutBlocking(poll_fd_->fd); + watcher_->OnSocketReadyToWrite(poll_fd_->fd); } bool MessagePumpGlib::WatchFileDescriptor(int fd, diff --git a/base/message_loop/message_pump_glib_unittest.cc b/base/message_loop/message_pump_glib_unittest.cc index 77b5ca425024..933831c60dee 100644 --- a/base/message_loop/message_pump_glib_unittest.cc +++ b/base/message_loop/message_pump_glib_unittest.cc @@ -658,8 +658,8 @@ class BaseWatcher : public MessagePumpGlib::FdWatcher { ~BaseWatcher() override = default; // base:MessagePumpGlib::FdWatcher interface - void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); } - void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); } + void OnSocketReadyToRead(int /* fd */) override { NOTREACHED(); } + void OnSocketReadyToWrite(int /* fd */) override { NOTREACHED(); } protected: raw_ptr controller_; @@ -676,7 +676,7 @@ class DeleteWatcher : public BaseWatcher { bool HasController() const { return !!controller_; } - void OnFileCanWriteWithoutBlocking(int /* fd */) override { + void OnSocketReadyToWrite(int /* fd */) override { ClearController(); } @@ -698,7 +698,7 @@ class StopWatcher : public BaseWatcher { ~StopWatcher() override = default; - void OnFileCanWriteWithoutBlocking(int /* fd */) override { + void OnSocketReadyToWrite(int /* fd */) override { controller_->StopWatchingFileDescriptor(); } }; @@ -717,14 +717,14 @@ class NestedPumpWatcher : public MessagePumpGlib::FdWatcher { NestedPumpWatcher() = default; ~NestedPumpWatcher() override = default; - void OnFileCanReadWithoutBlocking(int /* fd */) override { + void OnSocketReadyToRead(int /* fd */) override { RunLoop runloop; SingleThreadTaskRunner::GetCurrentDefault()->PostTask( FROM_HERE, BindOnce(&QuitMessageLoopAndStart, runloop.QuitClosure())); runloop.Run(); } - void OnFileCanWriteWithoutBlocking(int /* fd */) override {} + void OnSocketReadyToWrite(int /* fd */) override {} }; class QuitWatcher : public DeleteWatcher { @@ -734,7 +734,7 @@ class QuitWatcher : public DeleteWatcher { : DeleteWatcher(std::move(controller)), quit_closure_(std::move(quit_closure)) {} - void OnFileCanReadWithoutBlocking(int fd) override { + void OnSocketReadyToRead(int fd) override { ClearController(); if (quit_closure_) std::move(quit_closure_).Run(); @@ -753,9 +753,9 @@ void WriteFDWrapper(const int fd, } // namespace -// Tests that MessagePumpGlib::FdWatcher::OnFileCanReadWithoutBlocking is not +// Tests that MessagePumpGlib::FdWatcher::OnSocketReadyToRead is not // called for a READ_WRITE event, and that the controller is destroyed in -// OnFileCanWriteWithoutBlocking callback. +// OnSocketReadyToWrite callback. TEST_F(MessagePumpGLibFdWatchTest, DeleteWatcher) { auto pump = std::make_unique(); auto controller_ptr = @@ -771,9 +771,9 @@ TEST_F(MessagePumpGLibFdWatchTest, DeleteWatcher) { EXPECT_FALSE(watcher.HasController()); } -// Tests that MessagePumpGlib::FdWatcher::OnFileCanReadWithoutBlocking is not +// Tests that MessagePumpGlib::FdWatcher::OnSocketReadyToRead is not // called for a READ_WRITE event, when the watcher calls -// StopWatchingFileDescriptor in OnFileCanWriteWithoutBlocking callback. +// StopWatchingFileDescriptor in OnSocketReadyToWrite callback. TEST_F(MessagePumpGLibFdWatchTest, StopWatcher) { std::unique_ptr pump(new MessagePumpGlib); MessagePumpGlib::FdWatchController controller(FROM_HERE); diff --git a/base/message_loop/message_pump_io_ios.cc b/base/message_loop/message_pump_io_ios.cc index e6ab6e3370c0..3d4369e4f1eb 100644 --- a/base/message_loop/message_pump_io_ios.cc +++ b/base/message_loop/message_pump_io_ios.cc @@ -44,18 +44,18 @@ void MessagePumpIOSForIO::FdWatchController::Init(CFFileDescriptorRef fdref, fd_source_.reset(fd_source); } -void MessagePumpIOSForIO::FdWatchController::OnFileCanReadWithoutBlocking( +void MessagePumpIOSForIO::FdWatchController::OnSocketReadyToRead( int fd, MessagePumpIOSForIO* pump) { DCHECK(callback_types_ & kCFFileDescriptorReadCallBack); - watcher_->OnFileCanReadWithoutBlocking(fd); + watcher_->OnSocketReadyToRead(fd); } -void MessagePumpIOSForIO::FdWatchController::OnFileCanWriteWithoutBlocking( +void MessagePumpIOSForIO::FdWatchController::OnSocketReadyToWrite( int fd, MessagePumpIOSForIO* pump) { DCHECK(callback_types_ & kCFFileDescriptorWriteCallBack); - watcher_->OnFileCanWriteWithoutBlocking(fd); + watcher_->OnSocketReadyToWrite(fd); } MessagePumpIOSForIO::MessagePumpIOSForIO() : weak_factory_(this) { @@ -161,7 +161,7 @@ void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref, MessagePumpIOSForIO* pump = controller->pump().get(); DCHECK(pump); if (callback_types & kCFFileDescriptorWriteCallBack) - controller->OnFileCanWriteWithoutBlocking(fd, pump); + controller->OnSocketReadyToWrite(fd, pump); // Perform the read callback only if the file descriptor has not been // invalidated in the write callback. As |FdWatchController| invalidates @@ -170,7 +170,7 @@ void MessagePumpIOSForIO::HandleFdIOEvent(CFFileDescriptorRef fdref, if (callback_types & kCFFileDescriptorReadCallBack && CFFileDescriptorIsValid(fdref)) { DCHECK_EQ(fdref, controller->fdref_.get()); - controller->OnFileCanReadWithoutBlocking(fd, pump); + controller->OnSocketReadyToRead(fd, pump); } // Re-enable callbacks after the read/write if the file descriptor is still diff --git a/base/message_loop/message_pump_io_ios.h b/base/message_loop/message_pump_io_ios.h index 791cb9ea3c3d..b680bbded681 100644 --- a/base/message_loop/message_pump_io_ios.h +++ b/base/message_loop/message_pump_io_ios.h @@ -49,8 +49,8 @@ class BASE_EXPORT MessagePumpIOSForIO : public MessagePumpNSRunLoop, void set_watcher(FdWatcher* watcher) { watcher_ = watcher; } - void OnFileCanReadWithoutBlocking(int fd, MessagePumpIOSForIO* pump); - void OnFileCanWriteWithoutBlocking(int fd, MessagePumpIOSForIO* pump); + void OnSocketReadyToRead(int fd, MessagePumpIOSForIO* pump); + void OnSocketReadyToWrite(int fd, MessagePumpIOSForIO* pump); bool is_persistent_ = false; // false if this event is one-shot. base::mac::ScopedCFFileDescriptorRef fdref_; diff --git a/base/message_loop/message_pump_io_ios_unittest.cc b/base/message_loop/message_pump_io_ios_unittest.cc index e067195dd815..6ea1ac630aa9 100644 --- a/base/message_loop/message_pump_io_ios_unittest.cc +++ b/base/message_loop/message_pump_io_ios_unittest.cc @@ -57,8 +57,8 @@ class StupidWatcher : public MessagePumpIOSForIO::FdWatcher { ~StupidWatcher() override {} // base:MessagePumpIOSForIO::FdWatcher interface - void OnFileCanReadWithoutBlocking(int fd) override {} - void OnFileCanWriteWithoutBlocking(int fd) override {} + void OnSocketReadyToRead(int fd) override {} + void OnSocketReadyToWrite(int fd) override {} }; class BaseWatcher : public MessagePumpIOSForIO::FdWatcher { @@ -70,9 +70,9 @@ class BaseWatcher : public MessagePumpIOSForIO::FdWatcher { ~BaseWatcher() override {} // MessagePumpIOSForIO::FdWatcher interface - void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); } + void OnSocketReadyToRead(int /* fd */) override { NOTREACHED(); } - void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); } + void OnSocketReadyToWrite(int /* fd */) override { NOTREACHED(); } protected: MessagePumpIOSForIO::FdWatchController* controller_; @@ -85,7 +85,7 @@ class DeleteWatcher : public BaseWatcher { ~DeleteWatcher() override { DCHECK(!controller_); } - void OnFileCanWriteWithoutBlocking(int /* fd */) override { + void OnSocketReadyToWrite(int /* fd */) override { DCHECK(controller_); delete controller_; controller_ = NULL; @@ -115,7 +115,7 @@ class StopWatcher : public BaseWatcher { ~StopWatcher() override {} - void OnFileCanWriteWithoutBlocking(int /* fd */) override { + void OnSocketReadyToWrite(int /* fd */) override { controller_->StopWatchingFileDescriptor(); if (fd_to_start_watching_ >= 0) { pump_->WatchFileDescriptor(fd_to_start_watching_, diff --git a/base/message_loop/message_pump_io_starboard.cc b/base/message_loop/message_pump_io_starboard.cc index d9ce934a2046..4450c9a8300f 100644 --- a/base/message_loop/message_pump_io_starboard.cc +++ b/base/message_loop/message_pump_io_starboard.cc @@ -14,6 +14,12 @@ #include "base/message_loop/message_pump_io_starboard.h" +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) +#include +#include +#include +#endif + #include "base/auto_reset.h" #include "base/compiler_specific.h" #include "base/logging.h" @@ -23,18 +29,28 @@ #include "starboard/common/socket.h" #include "starboard/socket_waiter.h" +#include "third_party/musl/src/starboard/network/posix_socket.h" + namespace base { MessagePumpIOStarboard::SocketWatcher::SocketWatcher(const Location& from_here) : created_from_location_(from_here), interests_(kSbSocketWaiterInterestNone), +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + socket_(-1), +#else socket_(kSbSocketInvalid), +#endif pump_(nullptr), watcher_(nullptr), weak_factory_(this) {} MessagePumpIOStarboard::SocketWatcher::~SocketWatcher() { +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + if (socket_ >= 0) { +#else if (SbSocketIsValid(socket_)) { +#endif StopWatchingSocket(); } } @@ -42,15 +58,25 @@ MessagePumpIOStarboard::SocketWatcher::~SocketWatcher() { bool MessagePumpIOStarboard::SocketWatcher::StopWatchingSocket() { watcher_ = nullptr; interests_ = kSbSocketWaiterInterestNone; +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + if (socket_ < 0) { +#else if (!SbSocketIsValid(socket_)) { +#endif pump_ = nullptr; // If this watcher is not watching anything, no-op and return success. return true; } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + int socket = Release(); + bool result = true; + if (socket >= 0) { +#else SbSocket socket = Release(); bool result = true; if (SbSocketIsValid(socket)) { +#endif DCHECK(pump_); result = pump_->StopWatching(socket); } @@ -58,22 +84,42 @@ bool MessagePumpIOStarboard::SocketWatcher::StopWatchingSocket() { return result; } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) +void MessagePumpIOStarboard::SocketWatcher::Init(int socket, + bool persistent) { + DCHECK(socket >= 0); + DCHECK(socket_ < 0); +#else void MessagePumpIOStarboard::SocketWatcher::Init(SbSocket socket, bool persistent) { DCHECK(socket); DCHECK(!socket_); +#endif + socket_ = socket; persistent_ = persistent; } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) +int MessagePumpIOStarboard::SocketWatcher::Release() { + int socket = socket_; + socket_ = -1; + return socket; +} +#else SbSocket MessagePumpIOStarboard::SocketWatcher::Release() { SbSocket socket = socket_; socket_ = kSbSocketInvalid; return socket; } +#endif void MessagePumpIOStarboard::SocketWatcher::OnSocketReadyToRead( +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + int socket, +#else SbSocket socket, +#endif MessagePumpIOStarboard* pump) { if (!watcher_) return; @@ -83,7 +129,11 @@ void MessagePumpIOStarboard::SocketWatcher::OnSocketReadyToRead( } void MessagePumpIOStarboard::SocketWatcher::OnSocketReadyToWrite( +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + int socket, +#else SbSocket socket, +#endif MessagePumpIOStarboard* pump) { if (!watcher_) return; @@ -102,12 +152,21 @@ MessagePumpIOStarboard::~MessagePumpIOStarboard() { SbSocketWaiterDestroy(waiter_); } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) +bool MessagePumpIOStarboard::Watch(int socket, + bool persistent, + int mode, + SocketWatcher* controller, + Watcher* delegate) { + DCHECK(socket >= 0); +#else bool MessagePumpIOStarboard::Watch(SbSocket socket, bool persistent, int mode, SocketWatcher* controller, Watcher* delegate) { DCHECK(SbSocketIsValid(socket)); +#endif DCHECK(controller); DCHECK(delegate); DCHECK(mode == WATCH_READ || mode == WATCH_WRITE || mode == WATCH_READ_WRITE); @@ -123,8 +182,13 @@ bool MessagePumpIOStarboard::Watch(SbSocket socket, interests |= kSbSocketWaiterInterestWrite; } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + int old_socket = controller->Release(); + if (old_socket >= 0) { +#else SbSocket old_socket = controller->Release(); if (SbSocketIsValid(old_socket)) { +#endif // It's illegal to use this function to listen on 2 separate fds with the // same |controller|. if (old_socket != socket) { @@ -141,12 +205,28 @@ bool MessagePumpIOStarboard::Watch(SbSocket socket, interests |= old_interest_mask; // Must disarm the event before we can reuse it. +#if SB_API_VERSION >= 16 + SbPosixSocketWaiterRemove(waiter_, old_socket); +/*#elif SB_IS(MODULAR) + SbSocket sb_old_socket = posixSocketGetSbFromFd(old_socket); + SbSocketWaiterRemove(waiter_, sb_old_socket); +*/ +#else SbSocketWaiterRemove(waiter_, old_socket); +#endif // SB_API_VERSION >= 16 } // Set current interest mask and waiter for this event. - if (!SbSocketWaiterAdd(waiter_, socket, controller, - OnSocketWaiterNotification, interests, persistent)) { + bool result = false; +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + result = SbPosixSocketWaiterAdd(waiter_, socket, controller, + OnPosixSocketWaiterNotification, interests, persistent); + +#else + result = SbSocketWaiterAdd(waiter_, socket, controller, + OnSocketWaiterNotification, interests, persistent); +#endif // SB_API_VERSION >= 16 + if (result == false) { return false; } @@ -157,9 +237,15 @@ bool MessagePumpIOStarboard::Watch(SbSocket socket, return true; } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) +bool MessagePumpIOStarboard::StopWatching(int socket) { + return SbPosixSocketWaiterRemove(waiter_, socket); +} +#else bool MessagePumpIOStarboard::StopWatching(SbSocket socket) { return SbSocketWaiterRemove(waiter_, socket); } +#endif // SB_API_VERSION >= 16 || SB_IS(MODULAR) void MessagePumpIOStarboard::AddIOObserver(IOObserver* obs) { io_observers_.AddObserver(obs); @@ -252,6 +338,36 @@ void MessagePumpIOStarboard::DidProcessIOEvent() { } } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + +// static +void MessagePumpIOStarboard::OnPosixSocketWaiterNotification(SbSocketWaiter waiter, + int socket, + void* context, + int ready_interests) { + base::WeakPtr controller = + static_cast(context)->weak_factory_.GetWeakPtr(); + DCHECK(controller.get()); + + MessagePumpIOStarboard* pump = controller->pump(); + pump->processed_io_events_ = true; + + // If not persistent, the watch has been released at this point. + if (!controller->persistent()) { + controller->Release(); + } + + if (ready_interests & kSbSocketWaiterInterestWrite) { + controller->OnSocketReadyToWrite(socket, pump); + } + + // Check |controller| in case it's been deleted previously. + if (controller.get() && ready_interests & kSbSocketWaiterInterestRead) { + controller->OnSocketReadyToRead(socket, pump); + } +} + +#else // static void MessagePumpIOStarboard::OnSocketWaiterNotification(SbSocketWaiter waiter, SbSocket socket, @@ -278,5 +394,5 @@ void MessagePumpIOStarboard::OnSocketWaiterNotification(SbSocketWaiter waiter, controller->OnSocketReadyToRead(socket, pump); } } - +#endif // SB_API_VERSION >= 16 } // namespace base diff --git a/base/message_loop/message_pump_io_starboard.h b/base/message_loop/message_pump_io_starboard.h index b7d807dec914..2d9d4e3bfbfe 100644 --- a/base/message_loop/message_pump_io_starboard.h +++ b/base/message_loop/message_pump_io_starboard.h @@ -15,6 +15,8 @@ #ifndef BASE_MESSAGE_PUMP_IO_STARBOARD_H_ #define BASE_MESSAGE_PUMP_IO_STARBOARD_H_ +#include "starboard/configuration.h" + #include "base/compiler_specific.h" #include "base/memory/weak_ptr.h" #include "base/message_loop/message_pump.h" @@ -49,8 +51,13 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump { public: // These methods are called from MessageLoop::Run when a socket can be // interacted with without blocking. +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + virtual void OnSocketReadyToRead(int socket) {} + virtual void OnSocketReadyToWrite(int socket) {} +#else virtual void OnSocketReadyToRead(SbSocket socket) {} virtual void OnSocketReadyToWrite(SbSocket socket) {} +#endif protected: virtual ~Watcher() {} @@ -79,8 +86,13 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump { friend class MessagePumpIOStarboardTest; // Called by MessagePumpIOStarboard. +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + void Init(int socket, bool persistent); + int Release(); +#else void Init(SbSocket socket, bool persistent); SbSocket Release(); +#endif int interests() const { return interests_; } void set_interests(int interests) { interests_ = interests; } @@ -90,12 +102,21 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump { void set_watcher(Watcher* watcher) { watcher_ = watcher; } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + void OnSocketReadyToRead(int socket, MessagePumpIOStarboard* pump); + void OnSocketReadyToWrite(int socket, MessagePumpIOStarboard* pump); +#else void OnSocketReadyToRead(SbSocket socket, MessagePumpIOStarboard* pump); void OnSocketReadyToWrite(SbSocket socket, MessagePumpIOStarboard* pump); +#endif const Location created_from_location_; int interests_; +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + int socket_; +#else SbSocket socket_; +#endif bool persistent_; MessagePumpIOStarboard* pump_; Watcher* watcher_; @@ -123,6 +144,16 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump { // If an error occurs while calling this method in a cumulative fashion, the // event previously attached to |controller| is aborted. Returns true on // success. Must be called on the same thread the message_pump is running on. +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + bool Watch(int socket, + bool persistent, + int mode, + SocketWatcher* controller, + Watcher* delegate); + + // Stops watching the socket. + bool StopWatching(int socket); +#else bool Watch(SbSocket socket, bool persistent, int mode, @@ -131,6 +162,7 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump { // Stops watching the socket. bool StopWatching(SbSocket socket); +#endif void AddIOObserver(IOObserver* obs); void RemoveIOObserver(IOObserver* obs); @@ -149,10 +181,17 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump { // Called by SbSocketWaiter to tell us a registered socket can be read and/or // written to. +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + static void OnPosixSocketWaiterNotification(SbSocketWaiter waiter, + int socket, + void* context, + int ready_interests); +#else static void OnSocketWaiterNotification(SbSocketWaiter waiter, SbSocket socket, void* context, int ready_interests); +#endif bool should_quit() const { return !keep_running_; } diff --git a/base/message_loop/message_pump_io_starboard_unittest.cc b/base/message_loop/message_pump_io_starboard_unittest.cc index c6231f70d882..4af4952d1733 100644 --- a/base/message_loop/message_pump_io_starboard_unittest.cc +++ b/base/message_loop/message_pump_io_starboard_unittest.cc @@ -18,6 +18,12 @@ #include +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) +#include +#include +#include +#endif + #include "base/functional/bind.h" #include "base/run_loop.h" #include "base/synchronization/waitable_event.h" @@ -42,8 +48,12 @@ class MessagePumpIOStarboardTest : public testing::Test { void SetUp() override { Thread::Options options(MessagePumpType::IO, 0); ASSERT_TRUE(io_thread_.StartWithOptions(std::move(options))); +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + socket_ = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); +#else socket_ = SbSocketCreate(SbSocketAddressType::kSbSocketAddressTypeIpv4, SbSocketProtocol::kSbSocketProtocolTcp); SbSocketIsValid(socket_); +#endif } void TearDown() override { @@ -52,33 +62,56 @@ class MessagePumpIOStarboardTest : public testing::Test { // pipe. io_thread_.Stop(); +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + close(socket_); +#else SbSocketDestroy(socket_); +#endif } std::unique_ptr CreateMessagePump() { return std::make_unique(); } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + int socket() { + return socket_; + } +#else SbSocket socket() { return socket_; } +#endif scoped_refptr io_runner() const { return io_thread_.task_runner(); } void SimulateIOEvent(MessagePumpIOStarboard::SocketWatcher* controller) { +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + MessagePumpIOStarboard::OnPosixSocketWaiterNotification(nullptr, + -1, + controller, + (kSbSocketWaiterInterestRead | kSbSocketWaiterInterestWrite)); + +#else + MessagePumpIOStarboard::OnSocketWaiterNotification(nullptr, nullptr, controller, (kSbSocketWaiterInterestRead | kSbSocketWaiterInterestWrite)); +#endif } std::unique_ptr task_environment_; private: Thread io_thread_; +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + int socket_; +#else SbSocket socket_; +#endif }; namespace { @@ -90,8 +123,13 @@ class StupidWatcher : public MessagePumpIOStarboard::Watcher { ~StupidWatcher() override = default; // MessagePumpIOStarboard::Watcher interface +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + void OnSocketReadyToRead(int socket) override {} + void OnSocketReadyToWrite(int socket) override {} +#else void OnSocketReadyToRead(SbSocket socket) override {} void OnSocketReadyToWrite(SbSocket socket) override {} +#endif }; // Death tests not supported. @@ -106,8 +144,13 @@ class BaseWatcher : public MessagePumpIOStarboard::Watcher { ~BaseWatcher() override = default; // MessagePumpIOStarboard::Watcher interface +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + void OnSocketReadyToRead(int socket) override { NOTREACHED(); } + void OnSocketReadyToWrite(int socket) override { NOTREACHED(); } +#else void OnSocketReadyToRead(SbSocket socket) override { NOTREACHED(); } void OnSocketReadyToWrite(SbSocket socket) override { NOTREACHED(); } +#endif }; class DeleteWatcher : public BaseWatcher { @@ -122,7 +165,11 @@ class DeleteWatcher : public BaseWatcher { return controller_.get(); } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + void OnSocketReadyToWrite(int socket) override { +#else void OnSocketReadyToWrite(SbSocket socket) override { +#endif DCHECK(controller_); controller_.reset(); } @@ -151,7 +198,11 @@ class StopWatcher : public BaseWatcher { ~StopWatcher() override = default; +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + void OnSocketReadyToWrite(int socket) override { +#else void OnSocketReadyToWrite(SbSocket socket) override { +#endif controller_->StopWatchingSocket(); } @@ -186,15 +237,24 @@ class NestedPumpWatcher : public MessagePumpIOStarboard::Watcher { NestedPumpWatcher() = default; ~NestedPumpWatcher() override = default; +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + void OnSocketReadyToRead(int socket) override { +#else void OnSocketReadyToRead(SbSocket socket) override { +#endif RunLoop runloop; SingleThreadTaskRunner::GetCurrentDefault()->PostTask( FROM_HERE, BindOnce(&QuitMessageLoopAndStart, runloop.QuitClosure())); runloop.Run(); } +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + void OnSocketReadyToWrite(int socket) override {} +}; +#else void OnSocketReadyToWrite(SbSocket socket) override {} }; +#endif // Fails on some platforms. TEST_F(MessagePumpIOStarboardTest, DISABLED_NestedPumpWatcher) { @@ -218,7 +278,11 @@ class QuitWatcher : public BaseWatcher { QuitWatcher(base::OnceClosure quit_closure) : quit_closure_(std::move(quit_closure)) {} +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + void OnSocketReadyToRead(int socket) override { +#else void OnSocketReadyToRead(SbSocket socket) override { +#endif // Post a fatal closure to the MessageLoop before we quit it. SingleThreadTaskRunner::GetCurrentDefault()->PostTask( FROM_HERE, BindOnce(&FatalClosure)); diff --git a/base/message_loop/message_pump_kqueue.cc b/base/message_loop/message_pump_kqueue.cc index 2d257326deac..0de032e9fef7 100644 --- a/base/message_loop/message_pump_kqueue.cc +++ b/base/message_loop/message_pump_kqueue.cc @@ -463,10 +463,10 @@ bool MessagePumpKqueue::ProcessEvents(Delegate* delegate, size_t count) { auto scoped_do_work_item = delegate->BeginWorkItem(); // WatchFileDescriptor() originally upcasts event->ident from an int. if (event->filter == EVFILT_READ) { - fd_watcher->OnFileCanReadWithoutBlocking( + fd_watcher->OnSocketReadyToRead( static_cast(event->ident)); } else if (event->filter == EVFILT_WRITE) { - fd_watcher->OnFileCanWriteWithoutBlocking( + fd_watcher->OnSocketReadyToWrite( static_cast(event->ident)); } } else if (event->filter == EVFILT_MACHPORT) { diff --git a/base/message_loop/message_pump_libevent.cc b/base/message_loop/message_pump_libevent.cc index 72f67b836c6f..c84059a02727 100644 --- a/base/message_loop/message_pump_libevent.cc +++ b/base/message_loop/message_pump_libevent.cc @@ -94,21 +94,21 @@ std::unique_ptr MessagePumpLibevent::FdWatchController::ReleaseEvent() { return std::move(event_); } -void MessagePumpLibevent::FdWatchController::OnFileCanReadWithoutBlocking( +void MessagePumpLibevent::FdWatchController::OnSocketReadyToRead( int fd, MessagePumpLibevent* pump) { - // Since OnFileCanWriteWithoutBlocking() gets called first, it can stop + // Since OnSocketReadyToWrite() gets called first, it can stop // watching the file descriptor. if (!watcher_) return; - watcher_->OnFileCanReadWithoutBlocking(fd); + watcher_->OnSocketReadyToRead(fd); } -void MessagePumpLibevent::FdWatchController::OnFileCanWriteWithoutBlocking( +void MessagePumpLibevent::FdWatchController::OnSocketReadyToWrite( int fd, MessagePumpLibevent* pump) { DCHECK(watcher_); - watcher_->OnFileCanWriteWithoutBlocking(fd); + watcher_->OnSocketReadyToWrite(fd); } const scoped_refptr& @@ -128,12 +128,12 @@ void MessagePumpLibevent::FdWatchController::OnFdReadable() { // don't actually want to call the client. return; } - watcher_->OnFileCanReadWithoutBlocking(epoll_interest_->params().fd); + watcher_->OnSocketReadyToRead(epoll_interest_->params().fd); } void MessagePumpLibevent::FdWatchController::OnFdWritable() { DCHECK(watcher_); - watcher_->OnFileCanWriteWithoutBlocking(epoll_interest_->params().fd); + watcher_->OnSocketReadyToWrite(epoll_interest_->params().fd); } MessagePumpLibevent::MessagePumpLibevent() { @@ -448,15 +448,15 @@ void MessagePumpLibevent::OnLibeventNotification(int fd, // is not destroyed. bool controller_was_destroyed = false; controller->was_destroyed_ = &controller_was_destroyed; - controller->OnFileCanWriteWithoutBlocking(fd, pump); + controller->OnSocketReadyToWrite(fd, pump); if (!controller_was_destroyed) - controller->OnFileCanReadWithoutBlocking(fd, pump); + controller->OnSocketReadyToRead(fd, pump); if (!controller_was_destroyed) controller->was_destroyed_ = nullptr; } else if (flags & EV_WRITE) { - controller->OnFileCanWriteWithoutBlocking(fd, pump); + controller->OnSocketReadyToWrite(fd, pump); } else if (flags & EV_READ) { - controller->OnFileCanReadWithoutBlocking(fd, pump); + controller->OnSocketReadyToRead(fd, pump); } } diff --git a/base/message_loop/message_pump_libevent.h b/base/message_loop/message_pump_libevent.h index 0389de964599..6a53810f32cc 100644 --- a/base/message_loop/message_pump_libevent.h +++ b/base/message_loop/message_pump_libevent.h @@ -133,8 +133,8 @@ class BASE_EXPORT MessagePumpLibevent : public MessagePump, void Init(std::unique_ptr e); std::unique_ptr ReleaseEvent(); - void OnFileCanReadWithoutBlocking(int fd, MessagePumpLibevent* pump); - void OnFileCanWriteWithoutBlocking(int fd, MessagePumpLibevent* pump); + void OnSocketReadyToRead(int fd, MessagePumpLibevent* pump); + void OnSocketReadyToWrite(int fd, MessagePumpLibevent* pump); // Methods called only by MessagePumpEpoll void set_epoll_pump(WeakPtr pump) { diff --git a/base/message_loop/message_pump_libevent_unittest.cc b/base/message_loop/message_pump_libevent_unittest.cc index 483d9fa64d09..31e9656bf30f 100644 --- a/base/message_loop/message_pump_libevent_unittest.cc +++ b/base/message_loop/message_pump_libevent_unittest.cc @@ -113,8 +113,8 @@ class StupidWatcher : public MessagePumpLibevent::FdWatcher { ~StupidWatcher() override = default; // base:MessagePumpLibevent::FdWatcher interface - void OnFileCanReadWithoutBlocking(int fd) override {} - void OnFileCanWriteWithoutBlocking(int fd) override {} + void OnSocketReadyToRead(int fd) override {} + void OnSocketReadyToWrite(int fd) override {} }; TEST_P(MessagePumpLibeventTest, QuitOutsideOfRun) { @@ -128,9 +128,9 @@ class BaseWatcher : public MessagePumpLibevent::FdWatcher { ~BaseWatcher() override = default; // base:MessagePumpLibevent::FdWatcher interface - void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); } + void OnSocketReadyToRead(int /* fd */) override { NOTREACHED(); } - void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); } + void OnSocketReadyToWrite(int /* fd */) override { NOTREACHED(); } }; class DeleteWatcher : public BaseWatcher { @@ -145,7 +145,7 @@ class DeleteWatcher : public BaseWatcher { return controller_.get(); } - void OnFileCanWriteWithoutBlocking(int /* fd */) override { + void OnSocketReadyToWrite(int /* fd */) override { DCHECK(controller_); controller_.reset(); } @@ -171,7 +171,7 @@ class StopWatcher : public BaseWatcher { ~StopWatcher() override = default; - void OnFileCanWriteWithoutBlocking(int /* fd */) override { + void OnSocketReadyToWrite(int /* fd */) override { controller_->StopWatchingFileDescriptor(); } @@ -203,14 +203,14 @@ class NestedPumpWatcher : public MessagePumpLibevent::FdWatcher { NestedPumpWatcher() = default; ~NestedPumpWatcher() override = default; - void OnFileCanReadWithoutBlocking(int /* fd */) override { + void OnSocketReadyToRead(int /* fd */) override { RunLoop runloop; SingleThreadTaskRunner::GetCurrentDefault()->PostTask( FROM_HERE, BindOnce(&QuitMessageLoopAndStart, runloop.QuitClosure())); runloop.Run(); } - void OnFileCanWriteWithoutBlocking(int /* fd */) override {} + void OnSocketReadyToWrite(int /* fd */) override {} }; TEST_P(MessagePumpLibeventTest, NestedPumpWatcher) { @@ -231,7 +231,7 @@ class QuitWatcher : public BaseWatcher { QuitWatcher(base::OnceClosure quit_closure) : quit_closure_(std::move(quit_closure)) {} - void OnFileCanReadWithoutBlocking(int /* fd */) override { + void OnSocketReadyToRead(int /* fd */) override { // Post a fatal closure to the MessageLoop before we quit it. SingleThreadTaskRunner::GetCurrentDefault()->PostTask( FROM_HERE, BindOnce(&FatalClosure)); diff --git a/base/message_loop/watchable_io_message_pump_posix.h b/base/message_loop/watchable_io_message_pump_posix.h index 6b40522bb02b..b499fdc5b072 100644 --- a/base/message_loop/watchable_io_message_pump_posix.h +++ b/base/message_loop/watchable_io_message_pump_posix.h @@ -15,8 +15,8 @@ class WatchableIOMessagePumpPosix { // of a file descriptor. class FdWatcher { public: - virtual void OnFileCanReadWithoutBlocking(int fd) = 0; - virtual void OnFileCanWriteWithoutBlocking(int fd) = 0; + virtual void OnSocketReadyToRead(int fd) = 0; + virtual void OnSocketReadyToWrite(int fd) = 0; protected: virtual ~FdWatcher() = default; diff --git a/base/task/current_thread.cc b/base/task/current_thread.cc index 673e2028d4a2..a3a777fadecb 100644 --- a/base/task/current_thread.cc +++ b/base/task/current_thread.cc @@ -212,11 +212,19 @@ MessagePumpForIO* CurrentIOThread::GetMessagePumpForIO() const { #if !BUILDFLAG(IS_NACL) #if defined(STARBOARD) +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) +bool CurrentIOThread::Watch(int socket, + bool persistent, + int mode, + SocketWatcher* controller, + Watcher* delegate) { +#else bool CurrentIOThread::Watch(SbSocket socket, bool persistent, int mode, SocketWatcher* controller, Watcher* delegate) { +#endif return static_cast(GetMessagePumpForIO()) ->Watch(socket, persistent, mode, controller, delegate); } diff --git a/base/task/current_thread.h b/base/task/current_thread.h index 5106c5e9fd71..7834785d938c 100644 --- a/base/task/current_thread.h +++ b/base/task/current_thread.h @@ -279,11 +279,20 @@ class BASE_EXPORT CurrentIOThread : public CurrentThread { WATCH_WRITE = base::MessagePumpIOStarboard::WATCH_WRITE, WATCH_READ_WRITE = base::MessagePumpIOStarboard::WATCH_READ_WRITE}; +#if SB_API_VERSION >= 16 //--|| SB_IS(MODULAR) + bool Watch(int socket, + bool persistent, + int mode, + SocketWatcher* controller, + Watcher* delegate); +#else bool Watch(SbSocket socket, bool persistent, int mode, SocketWatcher* controller, Watcher* delegate); +#endif + #elif BUILDFLAG(IS_WIN) // Please see MessagePumpWin for definitions of these methods. HRESULT RegisterIOHandler(HANDLE file, MessagePumpForIO::IOHandler* handler); diff --git a/media/cast/test/utility/tap_proxy.cc b/media/cast/test/utility/tap_proxy.cc index 7c61a7546c9f..8913dfc071cb 100644 --- a/media/cast/test/utility/tap_proxy.cc +++ b/media/cast/test/utility/tap_proxy.cc @@ -79,7 +79,7 @@ class QueueManager { : input_fd_(input_fd), packet_pipe_(std::move(pipe)) { read_socket_watch_controller_ = base::FileDescriptorWatcher::WatchReadable( input_fd_, - base::BindRepeating(&QueueManager::OnFileCanReadWithoutBlocking, + base::BindRepeating(&QueueManager::OnSocketReadyToRead, base::Unretained(this))); std::unique_ptr tmp(new SendToFDPipe(output_fd)); @@ -94,7 +94,7 @@ class QueueManager { } private: - void OnFileCanReadWithoutBlocking() { + void OnSocketReadyToRead() { std::unique_ptr packet(new Packet(kMaxPacketSize)); int nread = read(input_fd_, reinterpret_cast(&packet->front()), diff --git a/net/BUILD.gn b/net/BUILD.gn index ec5057e993fc..68bb0456c053 100644 --- a/net/BUILD.gn +++ b/net/BUILD.gn @@ -1171,10 +1171,22 @@ component("net") { if (is_starboard && is_starboardized_toolchain) { sources += [ "cert/test_root_certs_builtin.cc", + + "base/file_stream_context_posix.cc", + "base/sockaddr_util_posix.cc", + "base/sockaddr_util_posix.h", + "socket/socket_posix.cc", + "socket/socket_posix.h", "socket/tcp_socket_starboard.cc", "socket/tcp_socket_starboard.h", "socket/udp_socket_starboard.cc", "socket/udp_socket_starboard.h", + # Use POSIX socket for SB16 + "base/net_errors_posix.cc", + "socket/tcp_socket_posix.cc", + "socket/tcp_socket_posix.h", + "socket/udp_socket_posix.cc", + "socket/udp_socket_posix.h", ] deps += [ "//starboard:starboard_group" ] } diff --git a/net/base/address_family.cc b/net/base/address_family.cc index d6db1ffc3087..54e9dd0b3d5a 100644 --- a/net/base/address_family.cc +++ b/net/base/address_family.cc @@ -20,7 +20,7 @@ AddressFamily GetAddressFamily(const IPAddress& address) { } } -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 SbSocketAddressType ConvertAddressFamily(AddressFamily address_family) { switch (address_family) { case ADDRESS_FAMILY_IPV4: diff --git a/net/base/address_family.h b/net/base/address_family.h index 90f2be5181ff..0de19b7e277c 100644 --- a/net/base/address_family.h +++ b/net/base/address_family.h @@ -42,7 +42,7 @@ typedef int HostResolverFlags; // Returns AddressFamily for |address|. NET_EXPORT AddressFamily GetAddressFamily(const IPAddress& address); -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 NET_EXPORT SbSocketAddressType ConvertAddressFamily( AddressFamily address_family); #else diff --git a/net/base/address_tracker_linux.cc b/net/base/address_tracker_linux.cc index b9c8f8e46f9e..5e618ae72a67 100644 --- a/net/base/address_tracker_linux.cc +++ b/net/base/address_tracker_linux.cc @@ -382,7 +382,7 @@ void AddressTrackerLinux::DumpInitialAddressesAndWatch() { watcher_ = base::FileDescriptorWatcher::WatchReadable( netlink_fd_.get(), - base::BindRepeating(&AddressTrackerLinux::OnFileCanReadWithoutBlocking, + base::BindRepeating(&AddressTrackerLinux::OnSocketReadyToRead, base::Unretained(this))); } } @@ -586,7 +586,7 @@ void AddressTrackerLinux::HandleMessage(const char* buffer, } } -void AddressTrackerLinux::OnFileCanReadWithoutBlocking() { +void AddressTrackerLinux::OnSocketReadyToRead() { DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); bool address_changed; bool link_changed; diff --git a/net/base/address_tracker_linux.h b/net/base/address_tracker_linux.h index b6689733d687..95c154b33eef 100644 --- a/net/base/address_tracker_linux.h +++ b/net/base/address_tracker_linux.h @@ -207,7 +207,7 @@ class NET_EXPORT_PRIVATE AddressTrackerLinux : public AddressMapOwnerLinux { void AbortAndForceOnline(); // Called by |watcher_| when |netlink_fd_| can be read without blocking. - void OnFileCanReadWithoutBlocking(); + void OnSocketReadyToRead(); // Does |interface_index| refer to a tunnel interface? bool IsTunnelInterface(int interface_index) const; diff --git a/net/base/features.cc b/net/base/features.cc index f554886495be..1f1e5baf2e04 100644 --- a/net/base/features.cc +++ b/net/base/features.cc @@ -212,7 +212,7 @@ BASE_FEATURE(kDocumentReporting, base::FEATURE_ENABLED_BY_DEFAULT); #endif // BUILDFLAG(ENABLE_REPORTING) -#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 BASE_FEATURE(kUdpSocketPosixAlwaysUpdateBytesReceived, "UdpSocketPosixAlwaysUpdateBytesReceived", base::FEATURE_ENABLED_BY_DEFAULT); diff --git a/net/base/features.h b/net/base/features.h index 2c235b37f768..dd6a84f0c8ef 100644 --- a/net/base/features.h +++ b/net/base/features.h @@ -261,14 +261,14 @@ NET_EXPORT extern const base::FeatureParam NET_EXPORT BASE_DECLARE_FEATURE(kDocumentReporting); #endif // BUILDFLAG(ENABLE_REPORTING) -#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 // When enabled, UDPSocketPosix increments the global counter of bytes received // every time bytes are received, instead of using a timer to batch updates. // This should reduce the number of wake ups and improve battery consumption. // TODO(https://crbug.com/1189805): Cleanup the feature after verifying that it // doesn't negatively affect performance. NET_EXPORT BASE_DECLARE_FEATURE(kUdpSocketPosixAlwaysUpdateBytesReceived); -#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 // When this feature is enabled, redirected requests will be considered // cross-site for the purpose of SameSite cookies if any redirect hop was diff --git a/net/base/net_errors_posix.cc b/net/base/net_errors_posix.cc index df49829735bd..d8b88bee0a24 100644 --- a/net/base/net_errors_posix.cc +++ b/net/base/net_errors_posix.cc @@ -15,6 +15,8 @@ namespace net { +#if SB_API_VERSION >= 16 + Error MapSystemError(logging::SystemErrorCode os_error) { if (os_error != 0) DVLOG(2) << "Error " << os_error << ": " @@ -124,10 +126,12 @@ Error MapSystemError(logging::SystemErrorCode os_error) { case 0: return OK; default: - LOG(WARNING) << "Unknown error " << base::safe_strerror(os_error) << " (" + LOG(WARNING) << "Unknown error " << " (" << os_error << ") mapped to net::ERR_FAILED"; return ERR_FAILED; } } +#endif // SB_API_VERSION >= 16 + } // namespace net diff --git a/net/base/net_errors_starboard.cc b/net/base/net_errors_starboard.cc index 7ed4aa304caa..329ec117d82a 100644 --- a/net/base/net_errors_starboard.cc +++ b/net/base/net_errors_starboard.cc @@ -24,6 +24,8 @@ namespace net { +#if SB_API_VERSION <= 15 + Error MapSystemError(logging::SystemErrorCode error) { if (error != 0) { char error_string[256]; @@ -58,4 +60,6 @@ Error MapSocketError(SbSocketError error) { } } +#endif // SB_API_VERSION <= 15 + } // namespace net diff --git a/net/dns/notify_watcher_mac.cc b/net/dns/notify_watcher_mac.cc index b7034038751d..5506d830492d 100644 --- a/net/dns/notify_watcher_mac.cc +++ b/net/dns/notify_watcher_mac.cc @@ -30,7 +30,7 @@ bool NotifyWatcherMac::Watch(const char* key, const CallbackType& callback) { DCHECK_GE(notify_fd_, 0); watcher_ = base::FileDescriptorWatcher::WatchReadable( notify_fd_, - base::BindRepeating(&NotifyWatcherMac::OnFileCanReadWithoutBlocking, + base::BindRepeating(&NotifyWatcherMac::OnSocketReadyToRead, base::Unretained(this))); callback_ = callback; return true; @@ -45,7 +45,7 @@ void NotifyWatcherMac::Cancel() { } } -void NotifyWatcherMac::OnFileCanReadWithoutBlocking() { +void NotifyWatcherMac::OnSocketReadyToRead() { int token; int status = HANDLE_EINTR(read(notify_fd_, &token, sizeof(token))); if (status != sizeof(token)) { diff --git a/net/dns/notify_watcher_mac.h b/net/dns/notify_watcher_mac.h index dc06e426a250..a89fa5d944b5 100644 --- a/net/dns/notify_watcher_mac.h +++ b/net/dns/notify_watcher_mac.h @@ -36,7 +36,7 @@ class NotifyWatcherMac { private: // Called by |watcher_| when |notify_fd_| can be read without blocking. - void OnFileCanReadWithoutBlocking(); + void OnSocketReadyToRead(); int notify_fd_; int notify_token_; diff --git a/net/proxy_resolution/proxy_config_service_linux.cc b/net/proxy_resolution/proxy_config_service_linux.cc index 7a9085686784..6db6871992d8 100644 --- a/net/proxy_resolution/proxy_config_service_linux.cc +++ b/net/proxy_resolution/proxy_config_service_linux.cc @@ -964,7 +964,7 @@ Cobalt */ notify_delegate_->OnCheckProxyConfigSettings(); } - // Called by OnFileCanReadWithoutBlocking() on the file thread. Reads + // Called by OnSocketReadyToRead() on the file thread. Reads // from the inotify file descriptor and starts up a debounce timer if // an event for kioslaverc is seen. void OnChangeNotification() { diff --git a/net/socket/socket_descriptor.cc b/net/socket/socket_descriptor.cc index 4880f0ee1b57..649bd77e373a 100644 --- a/net/socket/socket_descriptor.cc +++ b/net/socket/socket_descriptor.cc @@ -6,13 +6,13 @@ #include "build/build_config.h" -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 #include "starboard/socket.h" #include "base/notreached.h" #elif BUILDFLAG(IS_WIN) #include #include "net/base/winsock_init.h" -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 #include #include #endif @@ -24,7 +24,7 @@ namespace net { SocketDescriptor CreatePlatformSocket(int family, int type, int protocol) { -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 NOTREACHED(); return kSbSocketInvalid; #elif BUILDFLAG(IS_WIN) @@ -40,7 +40,7 @@ SocketDescriptor CreatePlatformSocket(int family, int type, int protocol) { } } return result; -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 SocketDescriptor result = ::socket(family, type, protocol); #if BUILDFLAG(IS_APPLE) // Disable SIGPIPE on this socket. Although Chromium globally disables diff --git a/net/socket/socket_descriptor.h b/net/socket/socket_descriptor.h index 032cb73d5cb1..5e96cbdd8ea6 100644 --- a/net/socket/socket_descriptor.h +++ b/net/socket/socket_descriptor.h @@ -17,12 +17,12 @@ namespace net { -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 typedef SbSocket SocketDescriptor; #elif BUILDFLAG(IS_WIN) typedef UINT_PTR SocketDescriptor; const SocketDescriptor kInvalidSocket = (SocketDescriptor)(~0); -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 typedef int SocketDescriptor; const SocketDescriptor kInvalidSocket = -1; #endif diff --git a/net/socket/socket_options.cc b/net/socket/socket_options.cc index 7f23a600a2da..42cc6c25aba9 100644 --- a/net/socket/socket_options.cc +++ b/net/socket/socket_options.cc @@ -9,12 +9,12 @@ #include "build/build_config.h" #include "net/base/net_errors.h" -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 #include "base/notreached.h" #elif BUILDFLAG(IS_WIN) #include #include -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 #include #include #include @@ -23,12 +23,13 @@ namespace net { int SetTCPNoDelay(SocketDescriptor fd, bool no_delay) { -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 return SbSocketSetTcpNoDelay(fd, no_delay) ? OK : ERR_FAILED; #else + #if BUILDFLAG(IS_WIN) BOOL on = no_delay ? TRUE : FALSE; -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 int on = no_delay ? 1 : 0; #endif int rv = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, @@ -38,7 +39,7 @@ int SetTCPNoDelay(SocketDescriptor fd, bool no_delay) { } int SetReuseAddr(SocketDescriptor fd, bool reuse) { -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 return SbSocketSetReuseAddress(fd, reuse) ? OK : ERR_FAILED; #else // SO_REUSEADDR is useful for server sockets to bind to a recently unbound @@ -56,7 +57,7 @@ int SetReuseAddr(SocketDescriptor fd, bool reuse) { // SO_REUSEPORT is provided in MacOS X and iOS. #if BUILDFLAG(IS_WIN) BOOL boolean_value = reuse ? TRUE : FALSE; -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 int boolean_value = reuse ? 1 : 0; #endif int rv = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, @@ -67,14 +68,14 @@ int SetReuseAddr(SocketDescriptor fd, bool reuse) { } int SetSocketReceiveBufferSize(SocketDescriptor fd, int32_t size) { -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 return SbSocketSetReceiveBufferSize(fd, size) ? OK : ERR_FAILED; #else int rv = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, reinterpret_cast(&size), sizeof(size)); #if BUILDFLAG(IS_WIN) int os_error = WSAGetLastError(); -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 int os_error = errno; #endif int net_error = (rv == -1) ? MapSystemError(os_error) : OK; @@ -86,14 +87,14 @@ int SetSocketReceiveBufferSize(SocketDescriptor fd, int32_t size) { } int SetSocketSendBufferSize(SocketDescriptor fd, int32_t size) { -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 return SbSocketSetSendBufferSize(fd, size) ? OK : ERR_FAILED; #else int rv = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, reinterpret_cast(&size), sizeof(size)); #if BUILDFLAG(IS_WIN) int os_error = WSAGetLastError(); -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 int os_error = errno; #endif int net_error = (rv == -1) ? MapSystemError(os_error) : OK; @@ -105,13 +106,13 @@ int SetSocketSendBufferSize(SocketDescriptor fd, int32_t size) { } int SetIPv6Only(SocketDescriptor fd, bool ipv6_only) { -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 NOTREACHED(); return -1; #else #if BUILDFLAG(IS_WIN) DWORD on = ipv6_only ? 1 : 0; -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 int on = ipv6_only ? 1 : 0; #endif int rv = setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, diff --git a/net/socket/socket_posix.cc b/net/socket/socket_posix.cc index ea3d794d9858..9ee0097a86c3 100644 --- a/net/socket/socket_posix.cc +++ b/net/socket/socket_posix.cc @@ -33,6 +33,8 @@ namespace net { +#if SB_API_VERSION >= 16 + namespace { int MapAcceptError(int os_error) { @@ -177,10 +179,10 @@ int SocketPosix::Accept(std::unique_ptr* socket, if (rv != ERR_IO_PENDING) return rv; - if (!base::CurrentIOThread::Get()->WatchFileDescriptor( + if (!base::CurrentIOThread::Get()->Watch( socket_fd_, true, base::MessagePumpForIO::WATCH_READ, &accept_socket_watcher_, this)) { - PLOG(ERROR) << "WatchFileDescriptor failed on accept"; + PLOG(ERROR) << "Watch failed on accept"; return MapSystemError(errno); } @@ -202,10 +204,10 @@ int SocketPosix::Connect(const SockaddrStorage& address, if (rv != ERR_IO_PENDING) return rv; - if (!base::CurrentIOThread::Get()->WatchFileDescriptor( + if (!base::CurrentIOThread::Get()->Watch( socket_fd_, true, base::MessagePumpForIO::WATCH_WRITE, &write_socket_watcher_, this)) { - PLOG(ERROR) << "WatchFileDescriptor failed on connect"; + PLOG(ERROR) << "Watch failed on connect"; return MapSystemError(errno); } @@ -224,7 +226,7 @@ int SocketPosix::Connect(const SockaddrStorage& address, rv = MapConnectError(errno); if (rv != OK && rv != ERR_IO_PENDING) { - write_socket_watcher_.StopWatchingFileDescriptor(); + write_socket_watcher_.StopWatchingSocket(); return rv; } @@ -271,7 +273,7 @@ bool SocketPosix::IsConnectedAndIdle() const { int SocketPosix::Read(IOBuffer* buf, int buf_len, CompletionOnceCallback callback) { - // Use base::Unretained() is safe here because OnFileCanReadWithoutBlocking() + // Use base::Unretained() is safe here because OnSocketReadyToRead() // won't be called if |this| is gone. int rv = ReadIfReady( buf, buf_len, @@ -298,7 +300,7 @@ int SocketPosix::ReadIfReady(IOBuffer* buf, if (rv != ERR_IO_PENDING) return rv; - if (!base::CurrentIOThread::Get()->WatchFileDescriptor( + if (!base::CurrentIOThread::Get()->Watch( socket_fd_, true, base::MessagePumpForIO::WATCH_READ, &read_socket_watcher_, this)) { PLOG(ERROR) << "WatchFileDescriptor failed on read"; @@ -312,7 +314,7 @@ int SocketPosix::ReadIfReady(IOBuffer* buf, int SocketPosix::CancelReadIfReady() { DCHECK(read_if_ready_callback_); - bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); + bool ok = read_socket_watcher_.StopWatchingSocket(); DCHECK(ok); read_if_ready_callback_.Reset(); @@ -348,7 +350,7 @@ int SocketPosix::WaitForWrite(IOBuffer* buf, DCHECK(!callback.is_null()); DCHECK_LT(0, buf_len); - if (!base::CurrentIOThread::Get()->WatchFileDescriptor( + if (!base::CurrentIOThread::Get()->Watch( socket_fd_, true, base::MessagePumpForIO::WATCH_WRITE, &write_socket_watcher_, this)) { PLOG(ERROR) << "WatchFileDescriptor failed on write"; @@ -409,9 +411,9 @@ void SocketPosix::DetachFromThread() { thread_checker_.DetachFromThread(); } -void SocketPosix::OnFileCanReadWithoutBlocking(int fd) { +void SocketPosix::OnSocketReadyToRead(int fd) { TRACE_EVENT0(NetTracingCategory(), - "SocketPosix::OnFileCanReadWithoutBlocking"); + "SocketPosix::OnSocketReadyToRead"); if (!accept_callback_.is_null()) { AcceptCompleted(); } else { @@ -420,7 +422,7 @@ void SocketPosix::OnFileCanReadWithoutBlocking(int fd) { } } -void SocketPosix::OnFileCanWriteWithoutBlocking(int fd) { +void SocketPosix::OnSocketReadyToWrite(int fd) { DCHECK(!write_callback_.is_null()); if (waiting_connect_) { ConnectCompleted(); @@ -452,7 +454,7 @@ void SocketPosix::AcceptCompleted() { if (rv == ERR_IO_PENDING) return; - bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); + bool ok = accept_socket_watcher_.StopWatchingSocket(); DCHECK(ok); accept_socket_ = nullptr; std::move(accept_callback_).Run(rv); @@ -479,7 +481,7 @@ void SocketPosix::ConnectCompleted() { if (rv == ERR_IO_PENDING) return; - bool ok = write_socket_watcher_.StopWatchingFileDescriptor(); + bool ok = write_socket_watcher_.StopWatchingSocket(); DCHECK(ok); waiting_connect_ = false; std::move(write_callback_).Run(rv); @@ -510,7 +512,7 @@ void SocketPosix::RetryRead(int rv) { void SocketPosix::ReadCompleted() { DCHECK(read_if_ready_callback_); - bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); + bool ok = read_socket_watcher_.StopWatchingSocket(); DCHECK(ok); std::move(read_if_ready_callback_).Run(OK); } @@ -533,7 +535,7 @@ void SocketPosix::WriteCompleted() { if (rv == ERR_IO_PENDING) return; - bool ok = write_socket_watcher_.StopWatchingFileDescriptor(); + bool ok = write_socket_watcher_.StopWatchingSocket(); DCHECK(ok); write_buf_.reset(); write_buf_len_ = 0; @@ -541,14 +543,14 @@ void SocketPosix::WriteCompleted() { } void SocketPosix::StopWatchingAndCleanUp(bool close_socket) { - bool ok = accept_socket_watcher_.StopWatchingFileDescriptor(); + bool ok = accept_socket_watcher_.StopWatchingSocket(); DCHECK(ok); - ok = read_socket_watcher_.StopWatchingFileDescriptor(); + ok = read_socket_watcher_.StopWatchingSocket(); DCHECK(ok); - ok = write_socket_watcher_.StopWatchingFileDescriptor(); + ok = write_socket_watcher_.StopWatchingSocket(); DCHECK(ok); - // These needs to be done after the StopWatchingFileDescriptor() calls, but + // These needs to be done after the StopWatchingSocket() calls, but // before deleting the write buffer. if (close_socket) { if (socket_fd_ != kInvalidSocket) { @@ -581,4 +583,6 @@ void SocketPosix::StopWatchingAndCleanUp(bool close_socket) { peer_address_.reset(); } +#endif // SB_API_VERSION >= 16 + } // namespace net diff --git a/net/socket/socket_posix.h b/net/socket/socket_posix.h index f7155cab06ec..28a706c48eb5 100644 --- a/net/socket/socket_posix.h +++ b/net/socket/socket_posix.h @@ -19,13 +19,15 @@ namespace net { +#if SB_API_VERSION >= 16 + class IOBuffer; struct SockaddrStorage; // Socket class to provide asynchronous read/write operations on top of the // posix socket api. It supports AF_INET, AF_INET6, and AF_UNIX addresses. class NET_EXPORT_PRIVATE SocketPosix - : public base::MessagePumpForIO::FdWatcher { + : public base::MessagePumpForIO::Watcher { public: SocketPosix(); @@ -108,8 +110,8 @@ class NET_EXPORT_PRIVATE SocketPosix private: // base::MessagePumpForIO::FdWatcher methods. - void OnFileCanReadWithoutBlocking(int fd) override; - void OnFileCanWriteWithoutBlocking(int fd) override; + void OnSocketReadyToRead(int fd) override; + void OnSocketReadyToWrite(int fd) override; int DoAccept(std::unique_ptr* socket); void AcceptCompleted(); @@ -129,11 +131,11 @@ class NET_EXPORT_PRIVATE SocketPosix SocketDescriptor socket_fd_; - base::MessagePumpForIO::FdWatchController accept_socket_watcher_; + base::MessagePumpForIO::SocketWatcher accept_socket_watcher_; raw_ptr> accept_socket_; CompletionOnceCallback accept_callback_; - base::MessagePumpForIO::FdWatchController read_socket_watcher_; + base::MessagePumpForIO::SocketWatcher read_socket_watcher_; // Non-null when a Read() is in progress. scoped_refptr read_buf_; @@ -143,7 +145,7 @@ class NET_EXPORT_PRIVATE SocketPosix // Non-null when a ReadIfReady() is in progress. CompletionOnceCallback read_if_ready_callback_; - base::MessagePumpForIO::FdWatchController write_socket_watcher_; + base::MessagePumpForIO::SocketWatcher write_socket_watcher_; scoped_refptr write_buf_; int write_buf_len_ = 0; // External callback; called when write or connect is complete. @@ -158,6 +160,8 @@ class NET_EXPORT_PRIVATE SocketPosix base::ThreadChecker thread_checker_; }; +#endif // SB_API_VERSION >= 16 + } // namespace net #endif // NET_SOCKET_SOCKET_POSIX_H_ diff --git a/net/socket/tcp_socket.h b/net/socket/tcp_socket.h index 19cfc74ea910..708df595e0f1 100644 --- a/net/socket/tcp_socket.h +++ b/net/socket/tcp_socket.h @@ -9,11 +9,11 @@ #include "net/base/net_export.h" #include "net/socket/socket_descriptor.h" -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 #include "net/socket/tcp_socket_starboard.h" #elif BUILDFLAG(IS_WIN) #include "net/socket/tcp_socket_win.h" -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 #include "net/socket/tcp_socket_posix.h" #endif @@ -25,11 +25,11 @@ namespace net { // class, unless a clear separation of client and server socket functionality is // not suitable for your use case (e.g., a socket needs to be created and bound // before you know whether it is a client or server socket). -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 typedef TCPSocketStarboard TCPSocket; #elif BUILDFLAG(IS_WIN) typedef TCPSocketWin TCPSocket; -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 typedef TCPSocketPosix TCPSocket; #endif diff --git a/net/socket/tcp_socket_posix.cc b/net/socket/tcp_socket_posix.cc index 73d002d44281..a7b8ffb07f8c 100644 --- a/net/socket/tcp_socket_posix.cc +++ b/net/socket/tcp_socket_posix.cc @@ -62,6 +62,8 @@ namespace net { +#if SB_API_VERSION >= 16 + namespace { // SetTCPKeepAlive sets SO_KEEPALIVE. @@ -711,4 +713,6 @@ bool TCPSocketPosix::GetEstimatedRoundTripTime(base::TimeDelta* out_rtt) const { #endif // defined(TCP_INFO) } +#endif // SB_API_VERSION >= 16 + } // namespace net diff --git a/net/socket/tcp_socket_posix.h b/net/socket/tcp_socket_posix.h index b1798a0494d2..a84cf61eb469 100644 --- a/net/socket/tcp_socket_posix.h +++ b/net/socket/tcp_socket_posix.h @@ -26,6 +26,8 @@ class TimeDelta; namespace net { +#if SB_API_VERSION >= 16 + class AddressList; class IOBuffer; class IPEndPoint; @@ -227,6 +229,8 @@ class NET_EXPORT TCPSocketPosix { SocketTag tag_; }; +#endif // SB_API_VERSION >= 16 + } // namespace net #endif // NET_SOCKET_TCP_SOCKET_POSIX_H_ diff --git a/net/socket/tcp_socket_starboard.cc b/net/socket/tcp_socket_starboard.cc index e87f81520d6a..f8458be61aad 100644 --- a/net/socket/tcp_socket_starboard.cc +++ b/net/socket/tcp_socket_starboard.cc @@ -28,6 +28,8 @@ namespace net { +#if SB_API_VERSION <= 15 + TCPSocketStarboard::TCPSocketStarboard( std::unique_ptr socket_performance_watcher, NetLog* net_log, @@ -683,4 +685,6 @@ int TCPSocketStarboard::SetIPv6Only(bool ipv6_only) { return 0; } +#endif // SB_API_VERSION <= 15 + } // namespace net diff --git a/net/socket/tcp_socket_starboard.h b/net/socket/tcp_socket_starboard.h index 876dac02d8a5..329a5d0f278d 100644 --- a/net/socket/tcp_socket_starboard.h +++ b/net/socket/tcp_socket_starboard.h @@ -33,6 +33,8 @@ namespace net { +#if SB_API_VERSION <= 15 + class NET_EXPORT TCPSocketStarboard : public base::MessagePumpIOStarboard::Watcher { public: TCPSocketStarboard( @@ -213,6 +215,8 @@ class NET_EXPORT TCPSocketStarboard : public base::MessagePumpIOStarboard::Watch // DISALLOW_COPY_AND_ASSIGN(TCPSocketStarboard); }; +#endif // SB_API_VERSION <= 15 + } // namespace net #endif // NET_SOCKET_TCP_SOCKET_STARBOARD_H_ diff --git a/net/socket/udp_socket.h b/net/socket/udp_socket.h index e7b897f09ec2..f57366dab4e5 100644 --- a/net/socket/udp_socket.h +++ b/net/socket/udp_socket.h @@ -7,11 +7,11 @@ #include "build/build_config.h" -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 #include "net/socket/udp_socket_starboard.h" #elif BUILDFLAG(IS_WIN) #include "net/socket/udp_socket_win.h" -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 #include "net/socket/udp_socket_posix.h" #endif @@ -37,11 +37,11 @@ namespace net { // RecvFrom/SendTo // Each read can come from a different client // // Writes need to be directed to a specific // // address. -#if defined(STARBOARD) +#if defined(STARBOARD) && SB_API_VERSION <= 15 typedef UDPSocketStarboard UDPSocket; #elif BUILDFLAG(IS_WIN) typedef UDPSocketWin UDPSocket; -#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) +#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16 typedef UDPSocketPosix UDPSocket; #endif diff --git a/net/socket/udp_socket_posix.cc b/net/socket/udp_socket_posix.cc index d7fe60ca6b77..9835266c6c88 100644 --- a/net/socket/udp_socket_posix.cc +++ b/net/socket/udp_socket_posix.cc @@ -66,6 +66,8 @@ namespace net { +#if SB_API_VERSION >= 16 + namespace { const int kBindRetries = 10; @@ -259,9 +261,9 @@ void UDPSocketPosix::Close() { write_callback_.Reset(); send_to_address_.reset(); - bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); + bool ok = read_socket_watcher_.StopWatchingSocket(); DCHECK(ok); - ok = write_socket_watcher_.StopWatchingFileDescriptor(); + ok = write_socket_watcher_.StopWatchingSocket(); DCHECK(ok); // Verify that |socket_| hasn't been corrupted. Needed to debug @@ -368,7 +370,7 @@ int UDPSocketPosix::RecvFrom(IOBuffer* buf, if (nread != ERR_IO_PENDING) return nread; - if (!base::CurrentIOThread::Get()->WatchFileDescriptor( + if (!base::CurrentIOThread::Get()->Watch( socket_, true, base::MessagePumpForIO::WATCH_READ, &read_socket_watcher_, &read_watcher_)) { PLOG(ERROR) << "WatchFileDescriptor failed on read"; @@ -414,7 +416,7 @@ int UDPSocketPosix::SendToOrWrite(IOBuffer* buf, return result; } - if (!base::CurrentIOThread::Get()->WatchFileDescriptor( + if (!base::CurrentIOThread::Get()->Watch( socket_, true, base::MessagePumpForIO::WATCH_WRITE, &write_socket_watcher_, &write_watcher_)) { DVPLOG(1) << "WatchFileDescriptor failed on write"; @@ -636,14 +638,14 @@ int UDPSocketPosix::AllowAddressSharingForMulticast() { return OK; } -void UDPSocketPosix::ReadWatcher::OnFileCanReadWithoutBlocking(int) { +void UDPSocketPosix::ReadWatcher::OnSocketReadyToRead(int) { TRACE_EVENT(NetTracingCategory(), - "UDPSocketPosix::ReadWatcher::OnFileCanReadWithoutBlocking"); + "UDPSocketPosix::ReadWatcher::OnSocketReadyToRead"); if (!socket_->read_callback_.is_null()) socket_->DidCompleteRead(); } -void UDPSocketPosix::WriteWatcher::OnFileCanWriteWithoutBlocking(int) { +void UDPSocketPosix::WriteWatcher::OnSocketReadyToWrite(int) { if (!socket_->write_callback_.is_null()) socket_->DidCompleteWrite(); } @@ -673,7 +675,7 @@ void UDPSocketPosix::DidCompleteRead() { read_buf_.reset(); read_buf_len_ = 0; recv_from_address_ = nullptr; - bool ok = read_socket_watcher_.StopWatchingFileDescriptor(); + bool ok = read_socket_watcher_.StopWatchingSocket(); DCHECK(ok); DoReadCallback(result); } @@ -713,7 +715,7 @@ void UDPSocketPosix::DidCompleteWrite() { write_buf_.reset(); write_buf_len_ = 0; send_to_address_.reset(); - write_socket_watcher_.StopWatchingFileDescriptor(); + write_socket_watcher_.StopWatchingSocket(); DoWriteCallback(result); } } @@ -1096,4 +1098,6 @@ int UDPSocketPosix::SetIOSNetworkServiceType(int ios_network_service_type) { return OK; } +#endif // SB_API_VERSION >= 16 + } // namespace net diff --git a/net/socket/udp_socket_posix.h b/net/socket/udp_socket_posix.h index ed9c7549c1fc..0107af7b5627 100644 --- a/net/socket/udp_socket_posix.h +++ b/net/socket/udp_socket_posix.h @@ -33,6 +33,8 @@ namespace net { +#if SB_API_VERSION >= 16 + class IPAddress; class NetLog; struct NetLogSource; @@ -285,7 +287,7 @@ class NET_EXPORT UDPSocketPosix { SOCKET_OPTION_MULTICAST_LOOP = 1 << 0 }; - class ReadWatcher : public base::MessagePumpForIO::FdWatcher { + class ReadWatcher : public base::MessagePumpForIO::Watcher { public: explicit ReadWatcher(UDPSocketPosix* socket) : socket_(socket) {} @@ -294,15 +296,15 @@ class NET_EXPORT UDPSocketPosix { // MessagePumpForIO::FdWatcher methods - void OnFileCanReadWithoutBlocking(int /* fd */) override; + void OnSocketReadyToRead(int /* fd */) override; - void OnFileCanWriteWithoutBlocking(int /* fd */) override {} + void OnSocketReadyToWrite(int /* fd */) override {} private: const raw_ptr socket_; }; - class WriteWatcher : public base::MessagePumpForIO::FdWatcher { + class WriteWatcher : public base::MessagePumpForIO::Watcher { public: explicit WriteWatcher(UDPSocketPosix* socket) : socket_(socket) {} @@ -311,9 +313,9 @@ class NET_EXPORT UDPSocketPosix { // MessagePumpForIO::FdWatcher methods - void OnFileCanReadWithoutBlocking(int /* fd */) override {} + void OnSocketReadyToRead(int /* fd */) override {} - void OnFileCanWriteWithoutBlocking(int /* fd */) override; + void OnSocketReadyToWrite(int /* fd */) override; private: const raw_ptr socket_; @@ -410,8 +412,8 @@ class NET_EXPORT UDPSocketPosix { mutable std::unique_ptr remote_address_; // The socket's posix wrappers - base::MessagePumpForIO::FdWatchController read_socket_watcher_; - base::MessagePumpForIO::FdWatchController write_socket_watcher_; + base::MessagePumpForIO::SocketWatcher read_socket_watcher_; + base::MessagePumpForIO::SocketWatcher write_socket_watcher_; // The corresponding watchers for reads and writes. ReadWatcher read_watcher_; @@ -465,6 +467,8 @@ class NET_EXPORT UDPSocketPosix { THREAD_CHECKER(thread_checker_); }; +#endif // SB_API_VERSION >= 16 + } // namespace net #endif // NET_SOCKET_UDP_SOCKET_POSIX_H_ diff --git a/net/socket/udp_socket_starboard.cc b/net/socket/udp_socket_starboard.cc index 7e51b873276a..89218e62b0bc 100644 --- a/net/socket/udp_socket_starboard.cc +++ b/net/socket/udp_socket_starboard.cc @@ -35,6 +35,8 @@ namespace net { +#if SB_API_VERSION <= 15 + UDPSocketStarboard::UDPSocketStarboard(DatagramSocket::BindType bind_type, net::NetLog* net_log, const net::NetLogSource& source) @@ -900,4 +902,6 @@ int UDPSocketStarboard::ResetWrittenBytes() { return bytes; } +#endif // SB_API_VERSION <= 15 + } // namespace net diff --git a/net/socket/udp_socket_starboard.h b/net/socket/udp_socket_starboard.h index 905964eccc67..cb8101670e67 100644 --- a/net/socket/udp_socket_starboard.h +++ b/net/socket/udp_socket_starboard.h @@ -42,6 +42,8 @@ namespace net { +#if SB_API_VERSION <= 15 + // Sendresult is inspired by sendmmsg, but unlike sendmmsg it is not // convenient to require that a positive |write_count| and a negative // error code are mutually exclusive. @@ -491,6 +493,8 @@ class NET_EXPORT UDPSocketStarboard // DISALLOW_COPY_AND_ASSIGN(UDPSocketStarboard); }; +#endif // SB_API_VERSION <= 15 + } // namespace net #endif // NET_SOCKET_UDP_SOCKET_STARBOARD_H_ diff --git a/starboard/elf_loader/exported_symbols.cc b/starboard/elf_loader/exported_symbols.cc index e073e8641e85..6ad1e43e2382 100644 --- a/starboard/elf_loader/exported_symbols.cc +++ b/starboard/elf_loader/exported_symbols.cc @@ -483,7 +483,9 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(ftruncate); REGISTER_SYMBOL(getaddrinfo); REGISTER_SYMBOL(getifaddrs); + REGISTER_SYMBOL(getpeername); REGISTER_SYMBOL(getsockname); + REGISTER_SYMBOL(getsockopt); REGISTER_SYMBOL(listen); REGISTER_SYMBOL(lseek); REGISTER_SYMBOL(malloc); @@ -498,10 +500,11 @@ ExportedSymbols::ExportedSymbols() { REGISTER_SYMBOL(readdir_r); REGISTER_SYMBOL(realloc); REGISTER_SYMBOL(recv); - REGISTER_SYMBOL(send); + REGISTER_SYMBOL(recvmsg); REGISTER_SYMBOL(recvfrom); REGISTER_SYMBOL(rmdir); REGISTER_SYMBOL(sched_yield); + REGISTER_SYMBOL(send); REGISTER_SYMBOL(sendto); REGISTER_SYMBOL(setsockopt); REGISTER_SYMBOL(socket); diff --git a/starboard/tools/api_leak_detector/api_leak_detector.py b/starboard/tools/api_leak_detector/api_leak_detector.py index ab667b620b77..c88a1b649bab 100755 --- a/starboard/tools/api_leak_detector/api_leak_detector.py +++ b/starboard/tools/api_leak_detector/api_leak_detector.py @@ -94,6 +94,7 @@ 'connect', 'clock_gettime', 'close', + 'fcntl', 'free', 'freeifaddrs', 'freeaddrinfo', @@ -103,14 +104,23 @@ 'gettimeofday', 'getifaddrs', 'getaddrinfo', + 'getpeername', + 'getsockname', + 'getsockopt', 'gmtime_r', 'inet_ntop', 'listen', 'lseek', 'malloc', 'posix_memalign', + 'posixSocketGetFdFromSb', + 'posixSocketGetSbFromFd', + 'SbPosixSocketWaiterAdd', + 'SbPosixSocketWaiterRemove', + 'puts', 'recv', 'recvfrom', + 'recvmsg', 'realloc', 'rmdir', 'setsockopt', diff --git a/third_party/musl/src/starboard/network/posix_socket.h b/third_party/musl/src/starboard/network/posix_socket.h new file mode 100644 index 000000000000..3f15b832d6a9 --- /dev/null +++ b/third_party/musl/src/starboard/network/posix_socket.h @@ -0,0 +1,35 @@ +// Copyright 2024 The Cobalt Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef THIRD_PARTY_MUSL_SRC_STARBOARD_POSIX_SOCKET_H_ +#define THIRD_PARTY_MUSL_SRC_STARBOARD_POSIX_SOCKET_H_ + +#include +#include "starboard/socket.h" + +#define PTHREAD_CREATE_JOINABLE 0 +#define PTHREAD_CREATE_DETACHED 1 + +#ifdef __cplusplus +extern "C" { +#endif + +int posixSocketGetFdFromSb(SbSocket socket); +SbSocket posixSocketGetSbFromFd(int socket); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // THIRD_PARTY_MUSL_SRC_STARBOARD_POSIX_SOCKET_H_ diff --git a/third_party/musl/src/starboard/network/socket.c b/third_party/musl/src/starboard/network/socket.c index a14e0cc0835e..0a15658e663a 100644 --- a/third_party/musl/src/starboard/network/socket.c +++ b/third_party/musl/src/starboard/network/socket.c @@ -152,6 +152,40 @@ static int get(int key, bool take, FileOrSocket** valuePtr) { return status; } +/****************************************************** +* This function looks for the sbSocket in the database. +* Return +* -1 - not found +* positive value - the file descriptor/key of sbSocket +******************************************************/ +static int getBySocket(SbSocket sbSocket) { + int index = 0; + int key = -1; + if (map == NULL) { + return -1; + } + + pthread_mutex_lock(&lock); + + while (index < mapSize){ + Node* node = map->array[index]; + if (node != NULL){ + FileOrSocket* pValue = node->value; + if (pValue != NULL) { + if ((pValue->is_file == false) && (pValue->socket == sbSocket)){ + key = node->key; + break; + } + } + } + index++; + } + + pthread_mutex_unlock(&lock); + + return key; +} + static SB_C_FORCE_INLINE time_t WindowsUsecToTimeT(int64_t time) { int64_t posix_time = time - 11644473600000000ULL; posix_time = posix_time / 1000000; @@ -761,13 +795,6 @@ int getsockname(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict } SbSocketAddress out_address = {0}; int result = SbSocketGetLocalAddress(fileOrSock->socket, &out_address)? 0: -1; - - struct sockaddr_in* addr_in = (struct sockaddr_in*)addr; - addr_in->sin_family = AF_INET; - addr_in->sin_addr.s_addr = out_address.address; - addr_in->sin_port = out_address.port; - *addrlen = sizeof(struct sockaddr_in); - return result; } @@ -787,7 +814,8 @@ int setsockopt (int sockfd, int level, int optname, const void* optval, return -1; } - if (level == SOL_SOCKET || level == SOL_TCP || level == IPPROTO_TCP || level == IPPROTO_IP) { + if (level == SOL_SOCKET || level == SOL_TCP || level == IPPROTO_TCP) { + int* operation = (int*)optval; switch (optname){ case SO_BROADCAST:{ @@ -829,18 +857,12 @@ int setsockopt (int sockfd, int level, int optname, const void* optval, return 0; } case TCP_NODELAY: { + int* operation = (int*)optval; bool bool_value = *operation == 1? true:false; return SbSocketSetTcpNoDelay(fileOrSock->socket, bool_value) == true? 0:-1; } case IP_ADD_MEMBERSHIP: { - if (optval == NULL) { - errno = EFAULT; - return -1; - } - const struct ip_mreq* imreq = (const struct ip_mreq*)optval; - SbSocketAddress* addr = (SbSocketAddress*)malloc(sizeof(SbSocketAddress)); - memcpy(addr->address, &(imreq->imr_multiaddr.s_addr), sizeof(imreq->imr_multiaddr.s_addr)); - + SbSocketAddress* addr = (SbSocketAddress*)optval; return SbSocketJoinMulticastGroup(fileOrSock->socket, addr) == true? 0:-1; } default: @@ -956,4 +978,26 @@ int getifaddrs(struct ifaddrs** ifap) { return 0; } + +int posixSocketGetFdFromSb(SbSocket socket){ + return getBySocket(socket); +} + +SbSocket posixSocketGetSbFromFd(int socket){ + if (socket <= 0){ + return NULL; + } + FileOrSocket *fileOrSock = NULL; + if (get(socket, false, &fileOrSock) != 0){ + errno = EBADF; + return NULL; + } + if (fileOrSock == NULL || fileOrSock->is_file == true) { + errno = EBADF; + return NULL; + } + + return fileOrSock->socket; +} + #endif // SB_API_VERSION < 16