| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 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 #import "chrome/browser/ui/cocoa/notifications/xpc_mach_port.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 |
| 10 @class OS_xpc_mach_send; |
| 11 |
| 12 extern "C" { |
| 13 |
| 14 OS_xpc_mach_send* xpc_mach_send_create(mach_port_t); |
| 15 mach_port_t xpc_mach_send_copy_right(OS_xpc_mach_send*); |
| 16 |
| 17 } // extern "C" |
| 18 |
| 19 // Give the compiler declarations for the NSXPCCoder methods. |
| 20 @protocol ForwardDeclareXPCCoder |
| 21 - (void)encodeXPCObject:(id)object forKey:(id)key; |
| 22 - (id)decodeXPCObjectForKey:(id)key; |
| 23 @end |
| 24 |
| 25 namespace { |
| 26 |
| 27 NSString* const kCrSendRight = @"org.chromium.xpc.MachSendRight"; |
| 28 |
| 29 } // namespace |
| 30 |
| 31 @implementation CrXPCMachPort { |
| 32 base::mac::ScopedMachSendRight port_; |
| 33 } |
| 34 |
| 35 - (instancetype)initWithMachSendRight: |
| 36 (base::mac::ScopedMachSendRight)sendRight { |
| 37 if ((self = [super init])) { |
| 38 DCHECK(sendRight.is_valid()); |
| 39 port_ = std::move(sendRight); |
| 40 } |
| 41 return self; |
| 42 } |
| 43 |
| 44 - (base::mac::ScopedMachSendRight)takeRight { |
| 45 return std::move(port_); |
| 46 } |
| 47 |
| 48 // NSCoding: |
| 49 - (instancetype)initWithCoder:(NSCoder*)coder { |
| 50 DCHECK([coder isKindOfClass:NSClassFromString(@"NSXPCDecoder")]); |
| 51 if ((self = [super init])) { |
| 52 id coderAsId = coder; |
| 53 |
| 54 OS_xpc_mach_send* xpcObject = |
| 55 [coderAsId decodeXPCObjectForKey:kCrSendRight]; |
| 56 if (!xpcObject) |
| 57 return nil; |
| 58 |
| 59 port_.reset(xpc_mach_send_copy_right(xpcObject)); |
| 60 } |
| 61 return self; |
| 62 } |
| 63 |
| 64 - (void)encodeWithCoder:(NSCoder*)coder { |
| 65 DCHECK([coder isKindOfClass:NSClassFromString(@"NSXPCEncoder")]); |
| 66 DCHECK(port_.is_valid()); |
| 67 |
| 68 id coderAsId = coder; |
| 69 |
| 70 base::scoped_nsobject<OS_xpc_mach_send> xpcObject( |
| 71 xpc_mach_send_create(port_.get())); |
| 72 [coderAsId encodeXPCObject:xpcObject forKey:kCrSendRight]; |
| 73 } |
| 74 |
| 75 // NSSecureCoding: |
| 76 + (BOOL)supportsSecureCoding { |
| 77 return YES; |
| 78 } |
| 79 |
| 80 @end |
| OLD | NEW |