webrtc_m130/webrtc/base/macsocketserver.cc
magjed 4e0581f543 Revert of move all reference to carbon api (patchset #2 id:300001 of https://codereview.webrtc.org/2321493002/ )
Reason for revert:
Still breaks Chromium mac compile:
[4542/22193] CXX obj/third_party/webrtc/base/rtc_base/unixfilesystem.o
FAILED: obj/third_party/webrtc/base/rtc_base/unixfilesystem.o
/b/c/cipd/goma/gomacc ../../third_party/llvm-build/Release+Asserts/bin/clang++ -MMD -MF obj/third_party/webrtc/base/rtc_base/unixfilesystem.o.d -DLOGGING=1 -DV8_DEPRECATION_WARNINGS -DENABLE_NOTIFICATIONS -DENABLE_PEPPER_CDMS -DENABLE_PLUGINS=1 -DENABLE_PDF=1 -DENABLE_PRINTING=1 -DENABLE_BASIC_PRINTING=1 -DENABLE_PRINT_PREVIEW=1 -DENABLE_SPELLCHECK=1 -DUSE_BROWSER_SPELLCHECKER=1 -DDCHECK_ALWAYS_ON=1 -DNO_TCMALLOC -DUSE_EXTERNAL_POPUP_MENU=1 -DENABLE_WEBRTC=1 -DENABLE_EXTENSIONS=1 -DENABLE_TASK_MANAGER=1 -DENABLE_THEMES=1 -DENABLE_CAPTIVE_PORTAL_DETECTION=1 -DENABLE_SESSION_SERVICE=1 -DENABLE_PLUGIN_INSTALLATION=1 -DENABLE_SUPERVISED_USERS=1 -DENABLE_SERVICE_DISCOVERY=1 -DUSE_PROPRIETARY_CODECS -DFULL_SAFE_BROWSING -DSAFE_BROWSING_CSD -DSAFE_BROWSING_DB_LOCAL -DCHROMIUM_BUILD -DENABLE_MEDIA_ROUTER=1 -DFIELDTRIAL_TESTING_ENABLED -DCR_CLANG_REVISION=280106-1 -DCR_XCODE_VERSION=0511 -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORE=0 -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DWEBRTC_RESTRICT_LOGGING -DEXPAT_RELATIVE_PATH -DENABLE_EXTERNAL_AUTH -DHAVE_OPENSSL_SSL_H -DHAVE_SCTP -DHAVE_SRTP -DHAVE_WEBRTC_VIDEO -DHAVE_WEBRTC_VOICE -DLOGGING_INSIDE_WEBRTC -DSRTP_RELATIVE_PATH -DSSL_USE_OPENSSL -DUSE_WEBRTC_DEV_BRANCH -DSSL_USE_OPENSSL -DHAVE_OPENSSL_SSL_H -DFEATURE_ENABLE_SSL -DLOGGING=1 -DFEATURE_ENABLE_SSL -DFEATURE_ENABLE_VOICEMAIL -DEXPAT_RELATIVE_PATH -DGTEST_RELATIVE_PATH -DNO_MAIN_THREAD_WRAPPING -DNO_SOUND_SYSTEM -DWEBRTC_CHROMIUM_BUILD -DWEBRTC_POSIX -DWEBRTC_MAC -DNO_MAIN_THREAD_WRAPPING -I../../third_party/webrtc_overrides -I../../third_party/boringssl/src/include -I../.. -Igen -I../../third_party/jsoncpp/overrides/include -I../../third_party/jsoncpp/source/include -I../../third_party/webrtc_overrides -I../../third_party -I../../third_party/boringssl/src/include -Wno-uninitialized -fno-strict-aliasing -fstack-protector -fcolor-diagnostics -arch x86_64 -O2 -g1 -isysroot /Applications/Xcode5.1.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -mmacosx-version-min=10.7 -fvisibility=hidden -Xclang -load -Xclang ../../third_party/llvm-build/Release+Asserts/lib/libFindBadConstructs.dylib -Xclang -add-plugin -Xclang find-bad-constructs -Wheader-hygiene -Wstring-conversion -Werror -Wall -Wno-unused-variable -Wpartial-availability -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-covered-switch-default -Wno-deprecated-register -Wno-unneeded-internal-declaration -Wno-inconsistent-missing-override -Wno-shift-negative-value -Wno-undefined-var-template -Wno-nonportable-include-path -Wno-address-of-packed-member -fno-threadsafe-statics -fvisibility-inlines-hidden -std=c++11 -stdlib=libc++ -fno-rtti -fno-exceptions -c ../../third_party/webrtc/base/unixfilesystem.cc -o obj/third_party/webrtc/base/rtc_base/unixfilesystem.o
../../third_party/webrtc/base/unixfilesystem.cc:375:11: error: variable has incomplete type 'FSRef'
    FSRef fr;
          ^
/Applications/Xcode5.1.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/CoreFoundation.framework/Headers/CFURL.h:467:8: note: forward declaration of 'FSRef'
struct FSRef;
       ^
../../third_party/webrtc/base/unixfilesystem.cc:376:27: error: use of undeclared identifier 'kUserDomain'
    if (0 != FSFindFolder(kUserDomain, kApplicationSupportFolderType,
                          ^
../../third_party/webrtc/base/unixfilesystem.cc:376:40: error: use of undeclared identifier 'kApplicationSupportFolderType'
    if (0 != FSFindFolder(kUserDomain, kApplicationSupportFolderType,
                                       ^
../../third_party/webrtc/base/unixfilesystem.cc:377:27: error: use of undeclared identifier 'kCreateFolder'; did you mean 'CreateFolder'?
                          kCreateFolder, &fr))
                          ^~~~~~~~~~~~~
                          CreateFolder
../../third_party/webrtc/base/unixfilesystem.cc:92:22: note: 'CreateFolder' declared here
bool UnixFilesystem::CreateFolder(const Pathname &path, mode_t mode) {

Original issue's description:
> Reland of move all reference to carbon api (patchset #1 id:1 of https://codereview.webrtc.org/2316563002/ )
>
> Reason for revert:
> Chromium build issues have been resolved.
>
> Original issue's description:
> > Revert of Remove all reference to carbon api (patchset #2 id:20001 of https://codereview.webrtc.org/2299633002/ )
> >
> > Reason for revert:
> > Breaks chromium build
> >
> > Original issue's description:
> > > Remove all reference to carbon api
> > >
> > > BUG=webrtc:6282
> > >
> > > Committed: https://crrev.com/dbd8b6bec4143c940b2f2ca8cd85c25d17327964
> > > Cr-Commit-Position: refs/heads/master@{#14080}
> >
> > TBR=magjed@webrtc.org,mflodman@webrtc.org
> > # Skipping CQ checks because original CL landed less than 1 days ago.
> > NOPRESUBMIT=true
> > NOTREECHECKS=true
> > NOTRY=true
> > BUG=webrtc:6282
> >
> > Committed: https://crrev.com/b096aa7fd375a980daab3a986596548ca5de2a1c
> > Cr-Commit-Position: refs/heads/master@{#14081}
>
> TBR=magjed@webrtc.org,mflodman@webrtc.org
> # Not skipping CQ checks because original CL landed more than 1 days ago.
> BUG=webrtc:6282
>
> Committed: https://crrev.com/a90879b64fd8db57c0274169311d6b51dd59f1a0
> Cr-Commit-Position: refs/heads/master@{#14125}

TBR=mflodman@webrtc.org,kthelgason@webrtc.org
# Skipping CQ checks because original CL landed less than 1 days ago.
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=webrtc:6282

Review-Url: https://codereview.webrtc.org/2317343003
Cr-Commit-Position: refs/heads/master@{#14133}
2016-09-08 12:35:00 +00:00

387 lines
12 KiB
C++

/*
* Copyright 2007 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/base/macsocketserver.h"
#include "webrtc/base/common.h"
#include "webrtc/base/logging.h"
#include "webrtc/base/macasyncsocket.h"
#include "webrtc/base/macutils.h"
#include "webrtc/base/thread.h"
namespace rtc {
///////////////////////////////////////////////////////////////////////////////
// MacBaseSocketServer
///////////////////////////////////////////////////////////////////////////////
MacBaseSocketServer::MacBaseSocketServer() {
}
MacBaseSocketServer::~MacBaseSocketServer() {
}
Socket* MacBaseSocketServer::CreateSocket(int type) {
return NULL;
}
Socket* MacBaseSocketServer::CreateSocket(int family, int type) {
return NULL;
}
AsyncSocket* MacBaseSocketServer::CreateAsyncSocket(int type) {
return CreateAsyncSocket(AF_INET, type);
}
AsyncSocket* MacBaseSocketServer::CreateAsyncSocket(int family, int type) {
if (SOCK_STREAM != type)
return NULL;
MacAsyncSocket* socket = new MacAsyncSocket(this, family);
if (!socket->valid()) {
delete socket;
return NULL;
}
return socket;
}
void MacBaseSocketServer::RegisterSocket(MacAsyncSocket* s) {
sockets_.insert(s);
}
void MacBaseSocketServer::UnregisterSocket(MacAsyncSocket* s) {
VERIFY(1 == sockets_.erase(s)); // found 1
}
bool MacBaseSocketServer::SetPosixSignalHandler(int signum,
void (*handler)(int)) {
Dispatcher* dispatcher = signal_dispatcher();
if (!PhysicalSocketServer::SetPosixSignalHandler(signum, handler)) {
return false;
}
// Only register the FD once, when the first custom handler is installed.
if (!dispatcher && (dispatcher = signal_dispatcher())) {
CFFileDescriptorContext ctx = { 0 };
ctx.info = this;
CFFileDescriptorRef desc = CFFileDescriptorCreate(
kCFAllocatorDefault,
dispatcher->GetDescriptor(),
false,
&MacBaseSocketServer::FileDescriptorCallback,
&ctx);
if (!desc) {
return false;
}
CFFileDescriptorEnableCallBacks(desc, kCFFileDescriptorReadCallBack);
CFRunLoopSourceRef ref =
CFFileDescriptorCreateRunLoopSource(kCFAllocatorDefault, desc, 0);
if (!ref) {
CFRelease(desc);
return false;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), ref, kCFRunLoopCommonModes);
CFRelease(desc);
CFRelease(ref);
}
return true;
}
// Used to disable socket events from waking our message queue when
// process_io is false. Does not disable signal event handling though.
void MacBaseSocketServer::EnableSocketCallbacks(bool enable) {
for (std::set<MacAsyncSocket*>::iterator it = sockets().begin();
it != sockets().end(); ++it) {
if (enable) {
(*it)->EnableCallbacks();
} else {
(*it)->DisableCallbacks();
}
}
}
void MacBaseSocketServer::FileDescriptorCallback(CFFileDescriptorRef fd,
CFOptionFlags flags,
void* context) {
MacBaseSocketServer* this_ss =
reinterpret_cast<MacBaseSocketServer*>(context);
ASSERT(this_ss);
Dispatcher* signal_dispatcher = this_ss->signal_dispatcher();
ASSERT(signal_dispatcher);
signal_dispatcher->OnPreEvent(DE_READ);
signal_dispatcher->OnEvent(DE_READ, 0);
CFFileDescriptorEnableCallBacks(fd, kCFFileDescriptorReadCallBack);
}
///////////////////////////////////////////////////////////////////////////////
// MacCFSocketServer
///////////////////////////////////////////////////////////////////////////////
void WakeUpCallback(void* info) {
MacCFSocketServer* server = static_cast<MacCFSocketServer*>(info);
ASSERT(NULL != server);
server->OnWakeUpCallback();
}
MacCFSocketServer::MacCFSocketServer()
: run_loop_(CFRunLoopGetCurrent()),
wake_up_(NULL) {
CFRunLoopSourceContext ctx;
memset(&ctx, 0, sizeof(ctx));
ctx.info = this;
ctx.perform = &WakeUpCallback;
wake_up_ = CFRunLoopSourceCreate(NULL, 0, &ctx);
ASSERT(NULL != wake_up_);
if (wake_up_) {
CFRunLoopAddSource(run_loop_, wake_up_, kCFRunLoopCommonModes);
}
}
MacCFSocketServer::~MacCFSocketServer() {
if (wake_up_) {
CFRunLoopSourceInvalidate(wake_up_);
CFRelease(wake_up_);
}
}
bool MacCFSocketServer::Wait(int cms, bool process_io) {
ASSERT(CFRunLoopGetCurrent() == run_loop_);
if (!process_io && cms == 0) {
// No op.
return true;
}
if (!process_io) {
// No way to listen to common modes and not get socket events, unless
// we disable each one's callbacks.
EnableSocketCallbacks(false);
}
SInt32 result;
if (kForever == cms) {
do {
// Would prefer to run in a custom mode that only listens to wake_up,
// but we have qtkit sending work to the main thread which is effectively
// blocked here, causing deadlock. Thus listen to the common modes.
// TODO: If QTKit becomes thread safe, do the above.
result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, 10000000, false);
} while (result != kCFRunLoopRunFinished && result != kCFRunLoopRunStopped);
} else {
// TODO: In the case of 0ms wait, this will only process one event, so we
// may want to loop until it returns TimedOut.
CFTimeInterval seconds = cms / 1000.0;
result = CFRunLoopRunInMode(kCFRunLoopDefaultMode, seconds, false);
}
if (!process_io) {
// Reenable them. Hopefully this won't cause spurious callbacks or
// missing ones while they were disabled.
EnableSocketCallbacks(true);
}
if (kCFRunLoopRunFinished == result) {
return false;
}
return true;
}
void MacCFSocketServer::WakeUp() {
if (wake_up_) {
CFRunLoopSourceSignal(wake_up_);
CFRunLoopWakeUp(run_loop_);
}
}
void MacCFSocketServer::OnWakeUpCallback() {
ASSERT(run_loop_ == CFRunLoopGetCurrent());
CFRunLoopStop(run_loop_);
}
///////////////////////////////////////////////////////////////////////////////
// MacCarbonSocketServer
///////////////////////////////////////////////////////////////////////////////
#ifndef CARBON_DEPRECATED
const UInt32 kEventClassSocketServer = 'MCSS';
const UInt32 kEventWakeUp = 'WAKE';
const EventTypeSpec kEventWakeUpSpec[] = {
{ kEventClassSocketServer, kEventWakeUp }
};
std::string DecodeEvent(EventRef event) {
std::string str;
DecodeFourChar(::GetEventClass(event), &str);
str.push_back(':');
DecodeFourChar(::GetEventKind(event), &str);
return str;
}
MacCarbonSocketServer::MacCarbonSocketServer()
: event_queue_(GetCurrentEventQueue()), wake_up_(NULL) {
VERIFY(noErr == CreateEvent(NULL, kEventClassSocketServer, kEventWakeUp, 0,
kEventAttributeUserEvent, &wake_up_));
}
MacCarbonSocketServer::~MacCarbonSocketServer() {
if (wake_up_) {
ReleaseEvent(wake_up_);
}
}
bool MacCarbonSocketServer::Wait(int cms, bool process_io) {
ASSERT(GetCurrentEventQueue() == event_queue_);
// Listen to all events if we're processing I/O.
// Only listen for our wakeup event if we're not.
UInt32 num_types = 0;
const EventTypeSpec* events = NULL;
if (!process_io) {
num_types = GetEventTypeCount(kEventWakeUpSpec);
events = kEventWakeUpSpec;
}
EventTargetRef target = GetEventDispatcherTarget();
EventTimeout timeout =
(kForever == cms) ? kEventDurationForever : cms / 1000.0;
EventTimeout end_time = GetCurrentEventTime() + timeout;
bool done = false;
while (!done) {
EventRef event;
OSStatus result = ReceiveNextEvent(num_types, events, timeout, true,
&event);
if (noErr == result) {
if (wake_up_ != event) {
LOG_F(LS_VERBOSE) << "Dispatching event: " << DecodeEvent(event);
result = SendEventToEventTarget(event, target);
if ((noErr != result) && (eventNotHandledErr != result)) {
LOG_E(LS_ERROR, OS, result) << "SendEventToEventTarget";
}
} else {
done = true;
}
ReleaseEvent(event);
} else if (eventLoopTimedOutErr == result) {
ASSERT(cms != kForever);
done = true;
} else if (eventLoopQuitErr == result) {
// Ignore this... we get spurious quits for a variety of reasons.
LOG_E(LS_VERBOSE, OS, result) << "ReceiveNextEvent";
} else {
// Some strange error occurred. Log it.
LOG_E(LS_WARNING, OS, result) << "ReceiveNextEvent";
return false;
}
if (kForever != cms) {
timeout = end_time - GetCurrentEventTime();
}
}
return true;
}
void MacCarbonSocketServer::WakeUp() {
if (!IsEventInQueue(event_queue_, wake_up_)) {
RetainEvent(wake_up_);
OSStatus result = PostEventToQueue(event_queue_, wake_up_,
kEventPriorityStandard);
if (noErr != result) {
LOG_E(LS_ERROR, OS, result) << "PostEventToQueue";
}
}
}
///////////////////////////////////////////////////////////////////////////////
// MacCarbonAppSocketServer
///////////////////////////////////////////////////////////////////////////////
MacCarbonAppSocketServer::MacCarbonAppSocketServer()
: event_queue_(GetCurrentEventQueue()) {
// Install event handler
VERIFY(noErr == InstallApplicationEventHandler(
NewEventHandlerUPP(WakeUpEventHandler), 1, kEventWakeUpSpec, this,
&event_handler_));
// Install a timer and set it idle to begin with.
VERIFY(noErr == InstallEventLoopTimer(GetMainEventLoop(),
kEventDurationForever,
kEventDurationForever,
NewEventLoopTimerUPP(TimerHandler),
this,
&timer_));
}
MacCarbonAppSocketServer::~MacCarbonAppSocketServer() {
RemoveEventLoopTimer(timer_);
RemoveEventHandler(event_handler_);
}
OSStatus MacCarbonAppSocketServer::WakeUpEventHandler(
EventHandlerCallRef next, EventRef event, void *data) {
QuitApplicationEventLoop();
return noErr;
}
void MacCarbonAppSocketServer::TimerHandler(
EventLoopTimerRef timer, void *data) {
QuitApplicationEventLoop();
}
bool MacCarbonAppSocketServer::Wait(int cms, bool process_io) {
if (!process_io && cms == 0) {
// No op.
return true;
}
if (kForever != cms) {
// Start a timer.
OSStatus error =
SetEventLoopTimerNextFireTime(timer_, cms / 1000.0);
if (error != noErr) {
LOG(LS_ERROR) << "Failed setting next fire time.";
}
}
if (!process_io) {
// No way to listen to common modes and not get socket events, unless
// we disable each one's callbacks.
EnableSocketCallbacks(false);
}
RunApplicationEventLoop();
if (!process_io) {
// Reenable them. Hopefully this won't cause spurious callbacks or
// missing ones while they were disabled.
EnableSocketCallbacks(true);
}
return true;
}
void MacCarbonAppSocketServer::WakeUp() {
// TODO: No-op if there's already a WakeUp in flight.
EventRef wake_up;
VERIFY(noErr == CreateEvent(NULL, kEventClassSocketServer, kEventWakeUp, 0,
kEventAttributeUserEvent, &wake_up));
OSStatus result = PostEventToQueue(event_queue_, wake_up,
kEventPriorityStandard);
if (noErr != result) {
LOG_E(LS_ERROR, OS, result) << "PostEventToQueue";
}
ReleaseEvent(wake_up);
}
#endif
} // namespace rtc