OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "content/common/chrome_application_mac.h" | 5 #import "content/common/chrome_application_mac.h" |
| 6 #import "content/common/cloud_print_class_mac.h" |
6 | 7 |
7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/mac/foundation_util.h" |
8 | 10 |
9 @interface CrApplication () | 11 @interface CrApplication () |
10 - (void)setHandlingSendEvent:(BOOL)handlingSendEvent; | 12 - (void)setHandlingSendEvent:(BOOL)handlingSendEvent; |
| 13 - (void)setCloudPrintHandler; |
| 14 - (void)submitPrint:(NSAppleEventDescriptor*)event; |
11 @end | 15 @end |
12 | 16 |
13 @implementation CrApplication | 17 @implementation CrApplication |
14 // Initialize NSApplication using the custom subclass. Check whether NSApp | 18 // Initialize NSApplication using the custom subclass. Check whether NSApp |
15 // was already initialized using another class, because that would break | 19 // was already initialized using another class, because that would break |
16 // some things. | 20 // some things. |
17 + (NSApplication*)sharedApplication { | 21 + (NSApplication*)sharedApplication { |
18 NSApplication* app = [super sharedApplication]; | 22 NSApplication* app = [super sharedApplication]; |
19 if (![NSApp isKindOfClass:self]) { | 23 if (![NSApp isKindOfClass:self]) { |
20 LOG(ERROR) << "NSApp should be of type " << [[self className] UTF8String] | 24 LOG(ERROR) << "NSApp should be of type " << [[self className] UTF8String] |
21 << ", not " << [[NSApp className] UTF8String]; | 25 << ", not " << [[NSApp className] UTF8String]; |
22 DCHECK(false) << "NSApp is of wrong type"; | 26 DCHECK(false) << "NSApp is of wrong type"; |
23 } | 27 } |
24 return app; | 28 return app; |
25 } | 29 } |
26 | 30 |
27 - (id)init { | 31 - (id)init { |
28 if ((self = [super init])) { | 32 if ((self = [super init])) { |
29 eventHooks_.reset([[NSMutableArray alloc] init]); | 33 eventHooks_.reset([[NSMutableArray alloc] init]); |
30 } | 34 } |
31 return self; | 35 return self; |
32 } | 36 } |
33 | 37 |
| 38 // Sets the Cloud Print Handler. Used only by service process. |
| 39 - (void)setCloudPrintHandler { |
| 40 NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; |
| 41 LOG(ERROR) << "Setting handler"; |
| 42 [em setEventHandler:self |
| 43 andSelector:@selector(submitPrint:) |
| 44 forEventClass:cloud_print_class |
| 45 andEventID:cloud_print_class]; |
| 46 LOG(ERROR) << "Successfully Set handler"; |
| 47 } |
| 48 |
| 49 // Event handler for Cloud Print Event. Forwards print job recieved to Chrome, |
| 50 // launching Chrome if necessary. Used to beat CUPS sandboxing. |
| 51 - (void)submitPrint:(NSAppleEventDescriptor*)event { |
| 52 LOG(ERROR) << "In submit print"; |
| 53 //TODO: resolve dependency on flag |
| 54 const char silent[]="--no-startup-window"; |
| 55 //Set up flag so that it can be passed along with Apple Event |
| 56 CFStringRef silent_launch_flag = CFStringCreateWithCString( |
| 57 NULL,silent,kCFStringEncodingUTF8); |
| 58 CFStringRef flags[] = {silent_launch_flag}; |
| 59 //Argv array that will be passed |
| 60 CFArrayRef pass_argv = CFArrayCreate(NULL, (const void**) flags, 1, |
| 61 &kCFTypeArrayCallBacks); |
| 62 FSRef ref; |
| 63 OSStatus status = noErr; |
| 64 CFURLRef* kDontWantURL = NULL; |
| 65 //Get the bundle ID of Chrome |
| 66 std::string bundleID= base::mac::BaseBundleID(); |
| 67 CFStringRef bundleIDCF = CFStringCreateWithCString( |
| 68 NULL,bundleID.c_str(),kCFStringEncodingUTF8); |
| 69 //Use Launch Services to locate Chrome using its bundleID |
| 70 status = LSFindApplicationForInfo(kLSUnknownCreator, bundleIDCF, |
| 71 NULL, &ref, kDontWantURL); |
| 72 |
| 73 if (status != noErr) { |
| 74 LOG(ERROR) <<"Failed to make path ref"; |
| 75 LOG(ERROR) << GetMacOSStatusErrorString(status); |
| 76 LOG(ERROR) << GetMacOSStatusCommentString(status); |
| 77 } |
| 78 //Actually create the Apple Event. |
| 79 NSAppleEventDescriptor *send_event = [NSAppleEventDescriptor |
| 80 appleEventWithEventClass: |
| 81 cloud_print_class |
| 82 eventID:cloud_print_class |
| 83 targetDescriptor:nil |
| 84 returnID:kAutoGenerateReturnID |
| 85 transactionID:kAnyTransactionID]; |
| 86 //Pull the parameters out of AppleEvent sent to us and attach them |
| 87 // to our Apple Event. |
| 88 NSAppleEventDescriptor *parameters = [event |
| 89 paramDescriptorForKeyword: |
| 90 cloud_print_class]; |
| 91 [send_event setParamDescriptor:parameters forKeyword:cloud_print_class]; |
| 92 LSApplicationParameters params = {0, kLSLaunchDefaults , &ref, NULL, |
| 93 NULL, pass_argv, NULL}; |
| 94 params.initialEvent =(AppleEvent *) [send_event aeDesc]; |
| 95 //Send the Apple Event Using launch services, launching Chrome if necessary. |
| 96 status = LSOpenApplication(¶ms, NULL); |
| 97 |
| 98 if (status != noErr) { |
| 99 LOG(ERROR) <<"Unable to launch"; |
| 100 LOG(ERROR) << GetMacOSStatusErrorString(status); |
| 101 LOG(ERROR) << GetMacOSStatusCommentString(status); |
| 102 } |
| 103 } |
| 104 |
| 105 |
34 - (BOOL)isHandlingSendEvent { | 106 - (BOOL)isHandlingSendEvent { |
35 return handlingSendEvent_; | 107 return handlingSendEvent_; |
36 } | 108 } |
37 | 109 |
38 - (void)setHandlingSendEvent:(BOOL)handlingSendEvent { | 110 - (void)setHandlingSendEvent:(BOOL)handlingSendEvent { |
39 handlingSendEvent_ = handlingSendEvent; | 111 handlingSendEvent_ = handlingSendEvent; |
40 } | 112 } |
41 | 113 |
42 - (void)clearIsHandlingSendEvent { | 114 - (void)clearIsHandlingSendEvent { |
43 [self setHandlingSendEvent:NO]; | 115 [self setHandlingSendEvent:NO]; |
(...skipping 11 matching lines...) Expand all Loading... |
55 [eventHooks_ addObject:handler]; | 127 [eventHooks_ addObject:handler]; |
56 } | 128 } |
57 | 129 |
58 - (void)removeEventHook:(id<CrApplicationEventHookProtocol>)handler { | 130 - (void)removeEventHook:(id<CrApplicationEventHookProtocol>)handler { |
59 [eventHooks_ removeObject:handler]; | 131 [eventHooks_ removeObject:handler]; |
60 } | 132 } |
61 | 133 |
62 @end | 134 @end |
63 | 135 |
64 namespace chrome_application_mac { | 136 namespace chrome_application_mac { |
| 137 CrApplication* var; |
65 | 138 |
66 ScopedSendingEvent::ScopedSendingEvent() | 139 ScopedSendingEvent::ScopedSendingEvent() |
67 : app_(static_cast<CrApplication*>([CrApplication sharedApplication])), | 140 : app_(static_cast<CrApplication*>([CrApplication sharedApplication])), |
68 handling_([app_ isHandlingSendEvent]) { | 141 handling_([app_ isHandlingSendEvent]) { |
69 [app_ setHandlingSendEvent:YES]; | 142 [app_ setHandlingSendEvent:YES]; |
70 } | 143 } |
71 | 144 |
72 ScopedSendingEvent::~ScopedSendingEvent() { | 145 ScopedSendingEvent::~ScopedSendingEvent() { |
73 [app_ setHandlingSendEvent:handling_]; | 146 [app_ setHandlingSendEvent:handling_]; |
74 } | 147 } |
75 | 148 |
76 void RegisterCrApp() { | 149 void RegisterCrApp() { |
77 [CrApplication sharedApplication]; | 150 var = (CrApplication*)[CrApplication sharedApplication]; |
78 } | 151 } |
79 | 152 |
| 153 void setHandler() { |
| 154 [var setCloudPrintHandler]; |
| 155 } |
| 156 |
| 157 |
80 } // namespace chrome_application_mac | 158 } // namespace chrome_application_mac |
OLD | NEW |