| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/multi_process_notification.h" | |
| 6 | |
| 7 #import <Foundation/Foundation.h> | |
| 8 #include <notify.h> | |
| 9 #include <unistd.h> | |
| 10 | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/eintr_wrapper.h" | |
| 13 #include "base/file_path.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/mac/mac_util.h" | |
| 16 #include "base/mac/scoped_nsautorelease_pool.h" | |
| 17 #include "base/message_loop.h" | |
| 18 #include "base/message_pump_libevent.h" | |
| 19 #include "base/path_service.h" | |
| 20 #include "base/stringprintf.h" | |
| 21 #include "base/sys_string_conversions.h" | |
| 22 #include "base/task.h" | |
| 23 #include "chrome/common/chrome_paths.h" | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 std::string AddPrefixToNotification(const std::string& name, | |
| 28 multi_process_notification::Domain domain) { | |
| 29 // The ordering of the components in the string returned by this function | |
| 30 // is important. Read "NAMESPACE CONVENTIONS" in 'man 3 notify' for details. | |
| 31 base::mac::ScopedNSAutoreleasePool pool; | |
| 32 NSBundle *bundle = base::mac::MainAppBundle(); | |
| 33 NSString *ns_bundle_id = [bundle bundleIdentifier]; | |
| 34 std::string bundle_id = base::SysNSStringToUTF8(ns_bundle_id); | |
| 35 std::string domain_string; | |
| 36 switch (domain) { | |
| 37 case multi_process_notification::ProfileDomain: { | |
| 38 FilePath user_data_dir; | |
| 39 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) { | |
| 40 NOTREACHED(); | |
| 41 } | |
| 42 domain_string = StringPrintf("user.uid.%u.%s.", | |
| 43 getuid(), user_data_dir.value().c_str()); | |
| 44 break; | |
| 45 } | |
| 46 | |
| 47 case multi_process_notification::UserDomain: | |
| 48 domain_string = StringPrintf("user.uid.%u.", getuid()); | |
| 49 break; | |
| 50 | |
| 51 case multi_process_notification::SystemDomain: | |
| 52 break; | |
| 53 } | |
| 54 return domain_string + bundle_id + "." + name; | |
| 55 } | |
| 56 | |
| 57 | |
| 58 } // namespace | |
| 59 | |
| 60 namespace multi_process_notification { | |
| 61 | |
| 62 bool Post(const std::string& name, Domain domain) { | |
| 63 std::string notification = AddPrefixToNotification(name, domain); | |
| 64 uint32_t status = notify_post(notification.c_str()); | |
| 65 DCHECK_EQ(status, static_cast<uint32_t>(NOTIFY_STATUS_OK)); | |
| 66 return status == NOTIFY_STATUS_OK; | |
| 67 } | |
| 68 | |
| 69 | |
| 70 class ListenerImpl : public base::MessagePumpLibevent::Watcher { | |
| 71 public: | |
| 72 ListenerImpl(const std::string& name, | |
| 73 Domain domain, | |
| 74 Listener::Delegate* delegate); | |
| 75 virtual ~ListenerImpl(); | |
| 76 | |
| 77 bool Start(); | |
| 78 | |
| 79 virtual void OnFileCanReadWithoutBlocking(int fd); | |
| 80 virtual void OnFileCanWriteWithoutBlocking(int fd); | |
| 81 | |
| 82 private: | |
| 83 std::string name_; | |
| 84 Domain domain_; | |
| 85 Listener::Delegate* delegate_; | |
| 86 int fd_; | |
| 87 int token_; | |
| 88 base::MessagePumpLibevent::FileDescriptorWatcher watcher_; | |
| 89 | |
| 90 DISALLOW_COPY_AND_ASSIGN(ListenerImpl); | |
| 91 }; | |
| 92 | |
| 93 ListenerImpl::ListenerImpl(const std::string& name, | |
| 94 Domain domain, | |
| 95 Listener::Delegate* delegate) | |
| 96 : name_(name), domain_(domain), delegate_(delegate), fd_(-1), token_(-1) { | |
| 97 } | |
| 98 | |
| 99 ListenerImpl::~ListenerImpl() { | |
| 100 if (token_ != -1) { | |
| 101 uint32_t status = notify_cancel(token_); | |
| 102 DCHECK_EQ(status, static_cast<uint32_t>(NOTIFY_STATUS_OK)); | |
| 103 token_ = -1; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 bool ListenerImpl::Start() { | |
| 108 DCHECK_EQ(fd_, -1); | |
| 109 DCHECK_EQ(token_, -1); | |
| 110 std::string notification = AddPrefixToNotification(name_, domain_); | |
| 111 | |
| 112 uint32_t status = notify_register_file_descriptor(notification.c_str(), &fd_, | |
| 113 0, &token_); | |
| 114 if (status != NOTIFY_STATUS_OK) { | |
| 115 LOG(ERROR) << "Unable to notify_register_file_descriptor for '" | |
| 116 << notification << "' Status: " << status; | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 MessageLoopForIO *io_loop = MessageLoopForIO::current(); | |
| 121 return io_loop->WatchFileDescriptor(fd_, true, MessageLoopForIO::WATCH_READ, | |
| 122 &watcher_, this); | |
| 123 } | |
| 124 | |
| 125 void ListenerImpl::OnFileCanReadWithoutBlocking(int fd) { | |
| 126 DCHECK_EQ(fd, fd_); | |
| 127 int token = 0; | |
| 128 if (HANDLE_EINTR(read(fd_, &token, sizeof(token))) >= 0) { | |
| 129 // Have to swap to native endianness <http://openradar.appspot.com/8821081>. | |
| 130 token = static_cast<int>(ntohl(token)); | |
| 131 if (token == token_) { | |
| 132 delegate_->OnNotificationReceived(name_, domain_); | |
| 133 } else { | |
| 134 LOG(WARNING) << "Unexpected value " << token; | |
| 135 } | |
| 136 } | |
| 137 } | |
| 138 | |
| 139 void ListenerImpl::OnFileCanWriteWithoutBlocking(int fd) { | |
| 140 NOTREACHED(); | |
| 141 } | |
| 142 | |
| 143 Listener::Listener(const std::string& name, | |
| 144 Domain domain, | |
| 145 Listener::Delegate* delegate) | |
| 146 : impl_(new ListenerImpl(name, domain, delegate)) { | |
| 147 } | |
| 148 | |
| 149 Listener::~Listener() { | |
| 150 } | |
| 151 | |
| 152 bool Listener::Start() { | |
| 153 return impl_->Start(); | |
| 154 } | |
| 155 | |
| 156 } // namespace multi_process_notification | |
| OLD | NEW |