From a529af57f7b475e18cb878712c42c95c7c82e56b Mon Sep 17 00:00:00 2001 From: "abeera@google.com" Date: Mon, 15 Aug 2011 20:56:17 +0000 Subject: Virtual Cloud Print Driver for Mac. Includes code for the driver itself. Also modifies the browser process as well as service process to register Apple Event handlers. Also changes the service process to allow registration of driver. BUG= TEST= Review URL: http://codereview.chromium.org/7485011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96825 0039d316-1c4b-4281-b951-d872f2087c98 --- cloud_print/virtual_driver/posix/backend.gyp | 56 +++++++--- .../posix/install_cloud_print_driver_mac.mm | 20 ++++ .../virtual_driver/posix/installer_util_mac.h | 16 +++ .../virtual_driver/posix/installer_util_mac.mm | 65 +++++++++++ .../posix/printer_driver_util_mac.mm | 120 +++++++++++++++++++++ .../posix/uninstall_cloud_print_driver_mac.mm | 20 ++++ .../virtual_driver/virtual_driver_linux.gyp | 20 ---- .../virtual_driver/virtual_driver_posix.gyp | 15 +++ 8 files changed, 299 insertions(+), 33 deletions(-) create mode 100644 cloud_print/virtual_driver/posix/install_cloud_print_driver_mac.mm create mode 100644 cloud_print/virtual_driver/posix/installer_util_mac.h create mode 100644 cloud_print/virtual_driver/posix/installer_util_mac.mm create mode 100644 cloud_print/virtual_driver/posix/printer_driver_util_mac.mm create mode 100644 cloud_print/virtual_driver/posix/uninstall_cloud_print_driver_mac.mm delete mode 100644 cloud_print/virtual_driver/virtual_driver_linux.gyp create mode 100644 cloud_print/virtual_driver/virtual_driver_posix.gyp (limited to 'cloud_print') diff --git a/cloud_print/virtual_driver/posix/backend.gyp b/cloud_print/virtual_driver/posix/backend.gyp index 6f25dba..df6e49b 100644 --- a/cloud_print/virtual_driver/posix/backend.gyp +++ b/cloud_print/virtual_driver/posix/backend.gyp @@ -4,27 +4,57 @@ { 'target_defaults': { - 'include_dirs': [ - '../..' - ], - 'variables': { - 'chromium_code': 1 - } + 'variables': { 'chromium_code':1 }, }, - 'targets' : [ + 'targets': [ { 'target_name': 'GCP-driver', 'type': 'executable', 'dependencies': [ '../../../base/base.gyp:base', ], - 'sources' : [ - 'virtual_driver_posix.cc', + 'sources': [ 'printer_driver_util_linux.cc', 'printer_driver_util_posix.h', + 'printer_driver_util_mac.mm', + 'virtual_driver_posix.cc', '../virtual_driver_switches.cc', - '../virtual_driver_switches.h', - ] - }, - ], + ], + 'conditions': [ + ['OS=="mac"', { + 'sources!': ['../virtual_driver_switches.cc'], + 'libraries': ['ScriptingBridge.framework'], + }], + ], + }], + 'conditions': [ + ['OS=="mac"', { + 'targets' : [ + { + 'target_name': 'GCP-install', + 'type': 'executable', + 'dependencies': [ + '../../../base/base.gyp:base', + ], + 'sources' : [ + 'install_cloud_print_driver_mac.mm', + 'installer_util_mac.h', + 'installer_util_mac.mm' + ], + }, + { + 'target_name': 'GCP-uninstall', + 'type': 'executable', + 'dependencies': [ + '../../../base/base.gyp:base', + ], + 'sources' : [ + 'installer_util_mac.h', + 'installer_util_mac.mm', + 'uninstall_cloud_print_driver_mac.mm', + ], + }, + ], + }], + ], } diff --git a/cloud_print/virtual_driver/posix/install_cloud_print_driver_mac.mm b/cloud_print/virtual_driver/posix/install_cloud_print_driver_mac.mm new file mode 100644 index 0000000..0d3f44c2 --- /dev/null +++ b/cloud_print/virtual_driver/posix/install_cloud_print_driver_mac.mm @@ -0,0 +1,20 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import +#import + +#include "cloud_print/virtual_driver/posix/installer_util_mac.h" + +const AEEventClass kAECloudPrintInstallClass= 'GCPi'; + +// Installer for Virtual Driver on Mac. Sends an Apple Event to +// Chrome, launching it if necessary. The Apple Event registers +// the virtual driver with the service process. +int main() { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + cloud_print::sendServiceProcessEvent(kAECloudPrintInstallClass); + [pool release]; + return 0; +} diff --git a/cloud_print/virtual_driver/posix/installer_util_mac.h b/cloud_print/virtual_driver/posix/installer_util_mac.h new file mode 100644 index 0000000..9983313 --- /dev/null +++ b/cloud_print/virtual_driver/posix/installer_util_mac.h @@ -0,0 +1,16 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CLOUD_PRINT_VIRTUAL_POSIX_INSTALLER_UTIL_MAC_H_ +#define CLOUD_PRINT_VIRTUAL_POSIX_INSTALLER_UTIL_MAC_H_ +#pragma once + +#import + +namespace cloud_print { +void sendServiceProcessEvent(const AEEventClass sendClass); +} // namespace cloud_print + +#endif // CLOUD_PRINT_VIRTUAL_POSIX_INSTALLER_UTIL_MAC_H_ + diff --git a/cloud_print/virtual_driver/posix/installer_util_mac.mm b/cloud_print/virtual_driver/posix/installer_util_mac.mm new file mode 100644 index 0000000..c6179c1 --- /dev/null +++ b/cloud_print/virtual_driver/posix/installer_util_mac.mm @@ -0,0 +1,65 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cloud_print/virtual_driver/posix/installer_util_mac.h" + +#import +#import +#import +#import + +#include "base/mac/foundation_util.h" + +#include +#include +#include + +namespace cloud_print { +// Sends an event of a class Type sendClass to the Chromium +// service process. Used to install and uninstall the Cloud +// Print driver on Mac. +void sendServiceProcessEvent(const AEEventClass sendClass) { + FSRef ref; + OSStatus status = noErr; + CFURLRef* kDontWantURL = NULL; + + std::string bundleID = base::mac::BaseBundleID(); + CFStringRef bundleIDCF = CFStringCreateWithCString(NULL, bundleID.c_str(), + kCFStringEncodingUTF8); + + status = LSFindApplicationForInfo(kLSUnknownCreator, bundleIDCF, NULL, + &ref, kDontWantURL); + + if (status != noErr) { + std::cerr << "Failed to make path ref"; + std::cerr << GetMacOSStatusErrorString(status); + std::cerr << GetMacOSStatusCommentString(status); + exit(-1); + } + + NSAppleEventDescriptor* sendEvent = + [NSAppleEventDescriptor appleEventWithEventClass:sendClass + eventID:sendClass + targetDescriptor:nil + returnID:kAutoGenerateReturnID + transactionID:kAnyTransactionID]; + if (sendEvent == nil) { + // Write to system error log using cerr. + std::cerr << "Unable to create Apple Event"; + } + LSApplicationParameters params = { 0, kLSLaunchDefaults, &ref, NULL, NULL, + NULL, NULL }; + AEDesc* initialEvent = const_cast ([sendEvent aeDesc]); + params.initialEvent = static_cast (initialEvent); + status = LSOpenApplication(¶ms, NULL); + + if (status != noErr) { + std::cerr << "Unable to launch Chrome to install"; + std::cerr << GetMacOSStatusErrorString(status); + std::cerr << GetMacOSStatusCommentString(status); + exit(-1); + } +} + +} // namespace cloud_print diff --git a/cloud_print/virtual_driver/posix/printer_driver_util_mac.mm b/cloud_print/virtual_driver/posix/printer_driver_util_mac.mm new file mode 100644 index 0000000..d85e3ff --- /dev/null +++ b/cloud_print/virtual_driver/posix/printer_driver_util_mac.mm @@ -0,0 +1,120 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "cloud_print/virtual_driver/posix/printer_driver_util_posix.h" + +#import +#import +#import +#import +#import + +#include "base/logging.h" +#include "base/mac/foundation_util.h" + +#include + +#include +#include + +// Duplicated is content/common/cloud_print_class_mac.h +const AEEventClass kAECloudPrintClass = 'GCPp'; + +namespace cloud_print { +// Checks to see whether the browser process, whose bundle ID +// is specified by bundle ID, is running. +bool IsBrowserRunning(std::string bundleID) { + SBApplication* app = [SBApplication applicationWithBundleIdentifier: + [NSString stringWithUTF8String:bundleID.c_str()]]; + if ([app isRunning]) { + return true; + } + return false; +} +} // namespace cloud_print + +void LaunchPrintDialog(const std::string& outputPath, + const std::string& jobTitle, + const std::string& user) { + NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; + OSStatus status = noErr; + FSRef ref; + // Get the bundleID of the browser. + std::string bundleID = base::mac::BaseBundleID(); + // If the browser is running, send the event to it. + // Otherwise, send the event to the service process. + if (!cloud_print::IsBrowserRunning(bundleID)) { + // Generate the bundle ID for the Service process. + bundleID = bundleID + ".helper"; + } + CFStringRef bundleIDCF = CFStringCreateWithCString( + NULL, + bundleID.c_str(), + kCFStringEncodingUTF8); + CFURLRef* kDontWantURL = NULL; + // Locate the service process with the help of the bundle ID. + status = LSFindApplicationForInfo(kLSUnknownCreator, bundleIDCF, + NULL, &ref, kDontWantURL); + + if (status != noErr) { + LOG(ERROR) << "Couldn't locate the process to send Apple Event"; + exit(CUPS_BACKEND_CANCEL); + } + + // Create the actual Apple Event. + NSAppleEventDescriptor* event = + [NSAppleEventDescriptor appleEventWithEventClass:kAECloudPrintClass + eventID:kAECloudPrintClass + targetDescriptor:nil + returnID:kAutoGenerateReturnID + transactionID:kAnyTransactionID]; + + if(event == nil) { + LOG(ERROR) << "Unable to Create Event"; + exit(CUPS_BACKEND_CANCEL); + } + + // Create the AppleEvent parameters. + NSAppleEventDescriptor* printPath = + [NSAppleEventDescriptor descriptorWithString: + [NSString stringWithUTF8String:outputPath.c_str()]]; + NSAppleEventDescriptor* title = + [NSAppleEventDescriptor descriptorWithString: + [NSString stringWithUTF8String:jobTitle.c_str()]]; + NSAppleEventDescriptor* mime = [NSAppleEventDescriptor + descriptorWithString:@"application/pdf"]; + + // Create and populate the list of parameters. + // Note that the array starts at index 1. + NSAppleEventDescriptor* parameters = [NSAppleEventDescriptor listDescriptor]; + + if(parameters == nil) { + LOG(ERROR) << "Unable to Create Paramters"; + exit(CUPS_BACKEND_CANCEL); + } + + [parameters insertDescriptor:mime atIndex:1]; + [parameters insertDescriptor:printPath atIndex:2]; + [parameters insertDescriptor:title atIndex:3]; + [event setParamDescriptor:parameters forKeyword:kAECloudPrintClass]; + + // Set the application launch parameters. + // We are just using launch services to deliver our Apple Event. + LSApplicationParameters params = { + 0, kLSLaunchDefaults , &ref, NULL, NULL, NULL, NULL }; + + AEDesc* initialEvent = const_cast ([event aeDesc]); + params.initialEvent = static_cast (initialEvent); + // Deliver the Apple Event using launch services. + status = LSOpenApplication(¶ms, NULL); + if (status != noErr) { + LOG(ERROR) << "Unable to launch"; + LOG(ERROR) << GetMacOSStatusErrorString(status); + LOG(ERROR) << GetMacOSStatusCommentString(status); + exit(CUPS_BACKEND_CANCEL); + } + + [pool release]; + return; +} diff --git a/cloud_print/virtual_driver/posix/uninstall_cloud_print_driver_mac.mm b/cloud_print/virtual_driver/posix/uninstall_cloud_print_driver_mac.mm new file mode 100644 index 0000000..adb7edc --- /dev/null +++ b/cloud_print/virtual_driver/posix/uninstall_cloud_print_driver_mac.mm @@ -0,0 +1,20 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#import +#import + +#include "cloud_print/virtual_driver/posix/installer_util_mac.h" + +const AEEventClass kAECloudPrintUninstallClass = 'GCPu'; + +// Uninstaller for Virtual Driver on Mac. Sends an Apple Event to +// Chrome, launching it if necessary. The Apple Event unregisters +// the virtual driver with the service process. +int main() { + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + cloud_print::sendServiceProcessEvent(kAECloudPrintUninstallClass); + [pool release]; + return 0; +} diff --git a/cloud_print/virtual_driver/virtual_driver_linux.gyp b/cloud_print/virtual_driver/virtual_driver_linux.gyp deleted file mode 100644 index 53fa411..0000000 --- a/cloud_print/virtual_driver/virtual_driver_linux.gyp +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright (c) 2011 The Chromium Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. -{ - 'targets': [ - { - 'target_name': 'virtual_driver', - 'type': 'none', - 'dependencies': [ - 'posix/backend.gyp:*', - ], - }, - ], -} - -# Local Variables: -# tab-width:2 -# indent-tabs-mode:nil -# End: -# vim: set expandtab tabstop=2 shiftwidth=2: diff --git a/cloud_print/virtual_driver/virtual_driver_posix.gyp b/cloud_print/virtual_driver/virtual_driver_posix.gyp new file mode 100644 index 0000000..cb458ec --- /dev/null +++ b/cloud_print/virtual_driver/virtual_driver_posix.gyp @@ -0,0 +1,15 @@ +# Copyright (c) 2011 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +{ + 'targets': [ + { + 'target_name': 'virtual_driver_posix', + 'type': 'none', + 'dependencies': [ + 'posix/backend.gyp:*', + ], + }, + ], +} + -- cgit v1.1