OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "cloud_print/virtual_driver/posix/printer_driver_util_posix.h" | |
6 | |
7 #import <ApplicationServices/ApplicationServices.h> | |
8 #import <CoreServices/CoreServices.h> | |
9 #import <Foundation/NSAutoreleasePool.h> | |
10 #import <Foundation/NSAppleEventDescriptor.h> | |
11 #import <ScriptingBridge/SBApplication.h> | |
12 | |
13 #include "base/logging.h" | |
14 #include "base/mac/foundation_util.h" | |
15 #include "base/mac/mac_logging.h" | |
16 | |
17 #include <cups/backend.h> | |
18 | |
19 #include <stdlib.h> | |
20 #include <string> | |
21 | |
22 // Duplicated is chrome/common/cloud_print/cloud_print_class_mac.h | |
23 const AEEventClass kAECloudPrintClass = 'GCPp'; | |
24 | |
25 namespace cloud_print { | |
26 // Checks to see whether the browser process, whose bundle ID | |
27 // is specified by bundle ID, is running. | |
28 bool IsBrowserRunning(std::string bundleID) { | |
29 SBApplication* app = [SBApplication applicationWithBundleIdentifier: | |
30 [NSString stringWithUTF8String:bundleID.c_str()]]; | |
31 if ([app isRunning]) { | |
32 return true; | |
33 } | |
34 return false; | |
35 } | |
36 } // namespace cloud_print | |
37 | |
38 namespace printer_driver_util { | |
39 void LaunchPrintDialog(const std::string& outputPath, | |
40 const std::string& jobTitle, | |
41 const std::string& user, | |
42 const std::string& print_ticket) { | |
43 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; | |
44 OSStatus status = noErr; | |
45 FSRef ref; | |
46 // Get the bundleID of the browser. | |
47 std::string bundleID = base::mac::BaseBundleID(); | |
48 // If the browser is running, send the event to it. | |
49 // Otherwise, send the event to the service process. | |
50 if (!cloud_print::IsBrowserRunning(bundleID)) { | |
51 // Generate the bundle ID for the Service process. | |
52 bundleID = bundleID + ".helper"; | |
53 } | |
54 CFStringRef bundleIDCF = CFStringCreateWithCString( | |
55 NULL, | |
56 bundleID.c_str(), | |
57 kCFStringEncodingUTF8); | |
58 CFURLRef* kDontWantURL = NULL; | |
59 // Locate the service process with the help of the bundle ID. | |
60 status = LSFindApplicationForInfo(kLSUnknownCreator, bundleIDCF, | |
61 NULL, &ref, kDontWantURL); | |
62 | |
63 if (status != noErr) { | |
64 OSSTATUS_LOG(ERROR, status) | |
65 << "Couldn't locate the process to send Apple Event"; | |
66 exit(CUPS_BACKEND_CANCEL); | |
67 } | |
68 | |
69 // Create the actual Apple Event. | |
70 NSAppleEventDescriptor* event = | |
71 [NSAppleEventDescriptor appleEventWithEventClass:kAECloudPrintClass | |
72 eventID:kAECloudPrintClass | |
73 targetDescriptor:nil | |
74 returnID:kAutoGenerateReturnID | |
75 transactionID:kAnyTransactionID]; | |
76 | |
77 if(event == nil) { | |
78 LOG(ERROR) << "Unable to Create Event"; | |
79 exit(CUPS_BACKEND_CANCEL); | |
80 } | |
81 | |
82 // Create the AppleEvent parameters. | |
83 NSAppleEventDescriptor* printPath = | |
84 [NSAppleEventDescriptor descriptorWithString: | |
85 [NSString stringWithUTF8String:outputPath.c_str()]]; | |
86 NSAppleEventDescriptor* title = | |
87 [NSAppleEventDescriptor descriptorWithString: | |
88 [NSString stringWithUTF8String:jobTitle.c_str()]]; | |
89 NSAppleEventDescriptor* mime = [NSAppleEventDescriptor | |
90 descriptorWithString:@"application/pdf"]; | |
91 NSAppleEventDescriptor* ticket = | |
92 [NSAppleEventDescriptor descriptorWithString: | |
93 [NSString stringWithUTF8String:print_ticket.c_str()]]; | |
94 | |
95 // Create and populate the list of parameters. | |
96 // Note that the array starts at index 1. | |
97 NSAppleEventDescriptor* parameters = [NSAppleEventDescriptor listDescriptor]; | |
98 | |
99 if(parameters == nil) { | |
100 LOG(ERROR) << "Unable to Create Paramters"; | |
101 exit(CUPS_BACKEND_CANCEL); | |
102 } | |
103 | |
104 [parameters insertDescriptor:mime atIndex:1]; | |
105 [parameters insertDescriptor:printPath atIndex:2]; | |
106 [parameters insertDescriptor:title atIndex:3]; | |
107 [parameters insertDescriptor:ticket atIndex:4]; | |
108 [event setParamDescriptor:parameters forKeyword:kAECloudPrintClass]; | |
109 | |
110 // Set the application launch parameters. | |
111 // We are just using launch services to deliver our Apple Event. | |
112 LSApplicationParameters params = { | |
113 0, kLSLaunchDefaults , &ref, NULL, NULL, NULL, NULL }; | |
114 | |
115 AEDesc* initialEvent = const_cast<AEDesc*> ([event aeDesc]); | |
116 params.initialEvent = static_cast<AppleEvent*> (initialEvent); | |
117 // Deliver the Apple Event using launch services. | |
118 status = LSOpenApplication(¶ms, NULL); | |
119 if (status != noErr) { | |
120 OSSTATUS_LOG(ERROR, status) << "Unable to launch"; | |
121 exit(CUPS_BACKEND_CANCEL); | |
122 } | |
123 | |
124 [pool release]; | |
125 return; | |
126 } | |
127 | |
128 } // namespace printer_driver_util | |
OLD | NEW |