OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
Mark Mentovai
2011/01/04 18:19:33
What will you use this for?
How is it supposed to
dmac
2011/01/06 06:06:03
Sending notifications to the service process.
| |
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 #include <notify.h> | |
Mark Mentovai
2011/01/04 18:19:33
<notify.h> and <Foundation/Foundation.h> can be in
dmac
2011/01/06 06:06:03
Done.
| |
8 | |
9 #import <Foundation/Foundation.h> | |
10 | |
11 #include "base/mac/mac_util.h" | |
12 #include "base/mac/scoped_nsautorelease_pool.h" | |
13 #include "base/message_loop.h" | |
14 #include "base/message_pump_libevent.h" | |
15 #include "base/sys_string_conversions.h" | |
16 #include "base/task.h" | |
17 | |
18 namespace { | |
19 | |
20 std::string PrependBundleID(const std::string& name) { | |
21 base::mac::ScopedNSAutoreleasePool pool; | |
22 NSBundle *bundle = base::mac::MainAppBundle(); | |
23 NSString *ns_bundle_id = [bundle bundleIdentifier]; | |
24 std::string bundle_id = base::SysNSStringToUTF8(ns_bundle_id); | |
25 return bundle_id + "." + name; | |
26 } | |
27 | |
28 } // namespace | |
29 | |
30 namespace multi_process_notification { | |
31 | |
32 bool Post(const std::string& name) { | |
33 std::string notification = PrependBundleID(name); | |
34 uint32_t status = notify_post(notification.c_str()); | |
35 DCHECK_EQ(status, static_cast<uint32_t>(NOTIFY_STATUS_OK)); | |
36 return status == NOTIFY_STATUS_OK; | |
37 } | |
38 | |
39 class ListenerImpl : public base::MessagePumpLibevent::Watcher { | |
40 public: | |
41 ListenerImpl(const std::string& name, | |
42 Listener::Delegate* delegate); | |
43 virtual ~ListenerImpl(); | |
44 | |
45 virtual bool Start(); | |
46 | |
47 virtual void OnFileCanReadWithoutBlocking(int fd); | |
48 virtual void OnFileCanWriteWithoutBlocking(int fd); | |
49 | |
50 private: | |
51 std::string name_; | |
52 base::MessagePumpLibevent::FileDescriptorWatcher watcher_; | |
53 Listener::Delegate* delegate_; | |
54 int fd_; | |
55 int token_; | |
56 }; | |
57 | |
58 ListenerImpl::ListenerImpl(const std::string& name, | |
59 Listener::Delegate* delegate) | |
60 : name_(name), delegate_(delegate), fd_(-1), token_(-1) { | |
61 } | |
62 | |
63 ListenerImpl::~ListenerImpl() { | |
64 if (token_ != -1) { | |
65 uint32_t status = notify_cancel(token_); | |
66 DCHECK_EQ(status, static_cast<uint32_t>(NOTIFY_STATUS_OK)); | |
67 token_ = -1; | |
68 } | |
69 } | |
70 | |
71 bool ListenerImpl::Start() { | |
72 DCHECK_EQ(fd_, -1); | |
73 DCHECK_EQ(token_, -1); | |
74 std::string notification = PrependBundleID(name_); | |
75 | |
76 uint32_t status = notify_register_file_descriptor(notification.c_str(), &fd_, | |
77 0, &token_); | |
78 if (status != NOTIFY_STATUS_OK) { | |
79 return false; | |
80 } | |
81 | |
82 MessageLoopForIO *io_loop = MessageLoopForIO::current(); | |
83 return io_loop->WatchFileDescriptor(fd_, true, MessageLoopForIO::WATCH_READ, | |
Mark Mentovai
2011/01/04 18:19:33
Do you ever need to undo this? watcher_ will disap
dmac
2011/01/06 06:06:03
when watcher_ is destroyed it handles taking care
| |
84 &watcher_, this); | |
85 } | |
86 | |
87 void ListenerImpl::OnFileCanReadWithoutBlocking(int fd) { | |
88 DCHECK_EQ(fd, fd_); | |
89 int token = 0; | |
90 if (read(fd_, &token, sizeof(token)) >= 0) { | |
Mark Mentovai
2011/01/04 18:19:33
HANDLE_EINTR from base/eintr_wrapper.h.
dmac
2011/01/06 06:06:03
Done.
| |
91 // Have to swap to native endianness <rdar://problem/5352778>. | |
Mark Mentovai
2011/01/04 18:19:33
Link to the problem: good.
Link to the problem th
dmac
2011/01/06 06:06:03
I copied the rdar link from some Apple source code
| |
92 token = static_cast<int>(ntohl(token)); | |
93 if (token == token_) { | |
94 delegate_->OnNotificationReceived(name_); | |
95 } else { | |
96 LOG(WARNING) << "Unexpected value " << token; | |
97 } | |
98 } | |
99 } | |
100 | |
101 void ListenerImpl::OnFileCanWriteWithoutBlocking(int fd) { | |
102 NOTREACHED(); | |
103 } | |
104 | |
105 Listener::Listener(const std::string& name, Listener::Delegate* delegate) : | |
106 impl_(new ListenerImpl(name, delegate)) { | |
107 } | |
108 | |
109 Listener::~Listener() { } | |
Mark Mentovai
2011/01/04 18:19:33
{} on separate lines, along with whatever other qu
dmac
2011/01/06 06:06:03
Done.
| |
110 | |
111 bool Listener::Start() { | |
112 return impl_->Start(); | |
113 } | |
114 | |
115 } // namespace multi_process_notification | |
OLD | NEW |