diff options
author | abeera@google.com <abeera@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-13 17:38:52 +0000 |
---|---|---|
committer | abeera@google.com <abeera@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-07-13 17:38:52 +0000 |
commit | 509d9b37a4c35fbc6210b3232d79e777cec0ff3a (patch) | |
tree | 9ff690ab8e871d3d67e66f6e061ac6384c4347e9 /cloud_print | |
parent | 807f0e14337e705ce0ede8e1107b8704321b861c (diff) | |
download | chromium_src-509d9b37a4c35fbc6210b3232d79e777cec0ff3a.zip chromium_src-509d9b37a4c35fbc6210b3232d79e777cec0ff3a.tar.gz chromium_src-509d9b37a4c35fbc6210b3232d79e777cec0ff3a.tar.bz2 |
Additional Commits for Linux printer driver
Initial commit for Linux virtual printer
BUG=
TEST=
Review URL: http://codereview.chromium.org/7222011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@92378 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'cloud_print')
9 files changed, 316 insertions, 23 deletions
diff --git a/cloud_print/virtual_driver/posix/backend.gyp b/cloud_print/virtual_driver/posix/backend.gyp new file mode 100644 index 0000000..8492a80 --- /dev/null +++ b/cloud_print/virtual_driver/posix/backend.gyp @@ -0,0 +1,32 @@ +# 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. + +{ + 'target_defaults': { + 'include_dirs': [ + '../..' + ], + }, + 'targets' : [ + { + 'target_name': 'GCP-driver', + 'type': 'executable', + 'dependencies': [ + '../../../base/base.gyp:base', + ], + 'msvs_guid': '8D06D53B-289E-4f99-99FC-77C77DB478A8', + 'sources' : [ + 'virtual_driver_posix.cc', + 'printer_driver_util_linux.cc', + '../virtual_driver_switches.cc', + ] + }, + ], +} + +# Local Variables: +# tab-width:2 +# indent-tabs-mode:nil +# End: +# vim: set expandtab tabstop=2 shiftwidth=2: diff --git a/cloud_print/virtual_driver/posix/printer_driver_util_linux.cc b/cloud_print/virtual_driver/posix/printer_driver_util_linux.cc new file mode 100644 index 0000000..119a4b5 --- /dev/null +++ b/cloud_print/virtual_driver/posix/printer_driver_util_linux.cc @@ -0,0 +1,48 @@ +// 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 <cups/backend.h> + +#include "base/base_paths.h" +#include "base/command_line.h" +#include "base/file_path.h" +#include "base/logging.h" +#include "base/path_service.h" + +#include "cloud_print/virtual_driver/posix/printer_driver_util_posix.h" +#include "cloud_print/virtual_driver/virtual_driver_switches.h" + +void LaunchPrintDialog(std::string output_path, std::string job_title, + std::string current_user) { + std::string set_var; + + // Set Environment variable to control display. + set_var="/home/" + current_user + "/.Xauthority"; + if(setenv("DISPLAY",":0.0",0) == -1) { + LOG(ERROR) << "Unable to set DISPLAY environment variable"; + } + if(setenv("XAUTHORITY",set_var.c_str(),0) == -1) { + LOG(ERROR) << "Unable to set XAUTHORITY environment variable"; + } + + // Construct the call to Chrome + + FilePath chrome_path("google-chrome"); + FilePath job_path(output_path); + CommandLine command_line(chrome_path); + command_line.AppendSwitchPath(switches::kCloudPrintFile,job_path); + command_line.AppendSwitchNative(switches::kCloudPrintJobTitle, job_title); + command_line.AppendSwitch(switches::kCloudPrintDeleteFile); + LOG(INFO) << "Call to chrome is " << command_line.command_line_string(); + + if(system(command_line.command_line_string().c_str())== -1 ) { + LOG(ERROR) << "Unable to call Chrome"; + exit(CUPS_BACKEND_CANCEL); + } + + else { + LOG(INFO) << "Call to Chrome succeeded"; + } +} + diff --git a/cloud_print/virtual_driver/posix/printer_driver_util_posix.h b/cloud_print/virtual_driver/posix/printer_driver_util_posix.h new file mode 100644 index 0000000..4d8d7a5 --- /dev/null +++ b/cloud_print/virtual_driver/posix/printer_driver_util_posix.h @@ -0,0 +1,12 @@ +// 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_DRIVER_LINUX_PRINTER_DRIVER_UTIL_POSIX_H_ +#define CLOUD_PRINT_VIRTUAL_DRIVER_LINUX_PRINTER_DRIVER_UTIL_POSIX_H_ +#pragma once +#include <string> + +void LaunchPrintDialog(std::string output_path, std::string job_title, + std::string current_user); + +#endif // CLOUD_PRINT_VIRTUAL_DRIVER_LINUX_PRINTER_DRIVER_UTIL_POSIX_H_ diff --git a/cloud_print/virtual_driver/posix/virtual_driver_posix.cc b/cloud_print/virtual_driver/posix/virtual_driver_posix.cc new file mode 100644 index 0000000..868228a --- /dev/null +++ b/cloud_print/virtual_driver/posix/virtual_driver_posix.cc @@ -0,0 +1,156 @@ +// 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 <pwd.h> +#include <stdio.h> + +#include <cups/backend.h> + +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> + +#include "base/at_exit.h" +#include "base/base_paths.h" +#include "base/command_line.h" +#include "base/file_path.h" +#include "base/logging.h" +#include "base/path_service.h" + +#include "cloud_print/virtual_driver/posix/printer_driver_util_posix.h" + +using namespace std; + +int WriteToTemp(FILE* input_pdf, FilePath output_path) { + FILE* output_pdf; + char buffer[128]; + output_pdf = fopen(output_path.value().c_str(),"w"); + if(output_pdf == NULL) { + LOG(ERROR) <<"Unable to open file handle for writing output file"; + exit(CUPS_BACKEND_CANCEL); + } + // Read from input file or stdin and write to output file. + while(fgets(buffer,128,input_pdf) != NULL){ + fputs(buffer, output_pdf); + } + + LOG(INFO) << "Successfully wrote output file"; + // ensure everything is written, then close the files. + fflush(output_pdf); + fclose(input_pdf); + fclose(output_pdf); +} + +void SetUser(const char* user) { + struct passwd* calling_user=NULL; + calling_user=getpwnam(user); + if(calling_user == NULL) { + LOG(ERROR) << "Unable to get calling user"; + exit(CUPS_BACKEND_CANCEL); + } + if(setgid(calling_user->pw_gid)==-1) { + LOG(ERROR) << "Unable to set group ID"; + exit(CUPS_BACKEND_CANCEL); + } + if(!setuid(calling_user->pw_uid)==-1) { + LOG(ERROR) << "Unable to set UID"; + exit(CUPS_BACKEND_CANCEL); + } + + LOG(INFO) << "Successfully set user and group ID"; +} + + + +// Main function for backend. +int main(int argc, const char* argv[]) { + // With no arguments, send identification string as required by CUPS. + if (argc==1) { + printf("file GCP-driver:/ \"GCP Virtual Driver\" \"GCP Virtual Driver\" " + "\"MFG:Google Inc.;MDL:GCP Virtual Driver;DES:GCP Virtual Driver;" + "CLS:PRINTER;CMD:POSTSCRIPT;\"\n"); + return 0; + } + + if (argc<6 || argc>7) { + fprintf(stderr,"Usage: GCP-Driver job-id user" + "title copies options [file]\n"); + return 0; + } + + //AtExitManager is necessary to use the path service. + base::AtExitManager aemanager; + // File handler for the temporary PDF we pass onto Chrome. + FILE* output_pdf; + //CommandLine is only setup to enable logging, never used subseqeuently. + CommandLine::Init(argc,argv); + // Location for the log file. + std::string log_location="/var/log/GCP-Driver.log"; + + // Set up logging. + logging::InitLogging(log_location.c_str(), + logging::LOG_ONLY_TO_FILE, + logging::LOCK_LOG_FILE, + logging::APPEND_TO_OLD_LOG_FILE, + logging::DISABLE_DCHECK_FOR_NON_OFFICIAL_RELEASE_BUILDS); + + // Temporary directory to hold the output file. + FilePath temp_dir; + // Strings to hold details of current job. + std::string current_user; + std::string set_var; + std::string job_title; + std::string job_id; + std::string file_name; + + // Get temp directory to hold spool file. + if(!PathService::Get(base::DIR_TEMP, &temp_dir)) { + LOG(ERROR) << "Unable to get DIR_TEMP"; + return CUPS_BACKEND_CANCEL; + } + // Get details from command line and set spool path. + current_user = argv[2]; + job_title = argv[3]; + job_id = argv[1]; + file_name = current_user + "-" + job_title + "-" + job_id; + FilePath output_path = temp_dir.Append(file_name); + + // Read from file if specified. + if(argc==7){ + FILE* input_pdf=fopen(argv[6],"r"); + if(input_pdf == NULL) { + LOG(ERROR) << "Unable to read input PDF"; + return CUPS_BACKEND_CANCEL; + } + // File is opened. + WriteToTemp(input_pdf,output_path); + } + // Otherwise, read from stdin. + else{ + WriteToTemp(stdin, output_path); + } + + // Set File permissions to allow non-sudo user to read spool file. + if(chmod(output_path.value().c_str(),S_IRUSR|S_IWUSR|S_IROTH|S_IWOTH)!=0) { + LOG(ERROR)<<"Unable to change file permissions"; + return CUPS_BACKEND_CANCEL; + } + + pid_t pid=fork(); + + if(!pid) { + // In child process. + + // Set the user to the one that initiated print job. + SetUser(argv[2]); + + // Launch Chrome and pass the print job onto Cloud Print. + LaunchPrintDialog(output_path.value(), job_title, current_user); + + return 0; + }// back in parent process. + // wait for child, then terminate. + waitpid(pid,NULL,0); + return 0; +}// main diff --git a/cloud_print/virtual_driver/virtual_driver_linux.gyp b/cloud_print/virtual_driver/virtual_driver_linux.gyp new file mode 100644 index 0000000..53fa411 --- /dev/null +++ b/cloud_print/virtual_driver/virtual_driver_linux.gyp @@ -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. +{ + '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_switches.cc b/cloud_print/virtual_driver/virtual_driver_switches.cc new file mode 100644 index 0000000..59022f1 --- /dev/null +++ b/cloud_print/virtual_driver/virtual_driver_switches.cc @@ -0,0 +1,13 @@ +// 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/virtual_driver_switches.h" + +namespace switches { +const char kCloudPrintDeleteFile[] = "cloud-print-delete-file"; +const char kCloudPrintFile[] = "cloud-print-file"; +const char kCloudPrintJobTitle[] = "cloud-print-job-title"; +const char kCloudPrintFileType[] = "cloud-print-file-type"; +} //namespace switches + diff --git a/cloud_print/virtual_driver/virtual_driver_switches.h b/cloud_print/virtual_driver/virtual_driver_switches.h new file mode 100644 index 0000000..aa8f127 --- /dev/null +++ b/cloud_print/virtual_driver/virtual_driver_switches.h @@ -0,0 +1,33 @@ +// 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_DRIVER_VIRTUAL_DRIVER_SWITCHES_H +#define CLOUD_PRINT_VIRTUAL_DRIVER_VIRTUAL_DRIVER_SWITCHES_H +#pragma once + +namespace switches { +// These constants are duplicated from chrome/common/chrome_switches.cc +// in order to avoid dependency problems. +// TODO(abodenha@chromium.org) Reunify them in some sensible manner. +// Bug: www.crbug.com/88991 + +// Used with kCloudPrintFile. Tells Chrome to delete the file when +// finished displaying the print dialog. +extern const char kCloudPrintDeleteFile[]; + +// Tells chrome to display the cloud print dialog and upload the +// specified file for printing. +extern const char kCloudPrintFile[]; + +// Used with kCloudPrintFile to specify a title for the resulting print +// job. +extern const char kCloudPrintJobTitle[]; + +// Specifies the mime type to be used when uploading data from the +// file referenced by cloud-print-file. +// Defaults to "application/pdf" if unspecified. +extern const char kCloudPrintFileType[]; +} //namespace switches + +#endif // CLOUD_PRINT_VIRTUAL_DRIVER_VIRTUAL_DRIVER_SWITCHES_H diff --git a/cloud_print/virtual_driver/win/port_monitor/port_monitor.cc b/cloud_print/virtual_driver/win/port_monitor/port_monitor.cc index 562d0b8..0c196ba1 100644 --- a/cloud_print/virtual_driver/win/port_monitor/port_monitor.cc +++ b/cloud_print/virtual_driver/win/port_monitor/port_monitor.cc @@ -25,29 +25,7 @@ #include "cloud_print/virtual_driver/win/port_monitor/spooler_win.h" #include "cloud_print/virtual_driver/win/virtual_driver_consts.h" #include "cloud_print/virtual_driver/win/virtual_driver_helpers.h" - -namespace switches { -// These constants are duplicated from chrome/common/chrome_switches.cc -// in order to avoid dependency problems. -// TODO(abodenha@chromium.org) Reunify them in some sensible manner. - -// Used with kCloudPrintFile. Tells Chrome to delete the file when -// finished displaying the print dialog. -const char kCloudPrintDeleteFile[] = "cloud-print-delete-file"; - -// Tells chrome to display the cloud print dialog and upload the -// specified file for printing. -const char kCloudPrintFile[] = "cloud-print-file"; - -// Used with kCloudPrintFile to specify a title for the resulting print -// job. -const char kCloudPrintJobTitle[] = "cloud-print-job-title"; - -// Specifies the mime type to be used when uploading data from the -// file referenced by cloud-print-file. -// Defaults to "application/pdf" if unspecified. -const char kCloudPrintFileType[] = "cloud-print-file-type"; -} +#include "cloud_print/virtual_driver/virtual_driver_switches.h" namespace cloud_print { diff --git a/cloud_print/virtual_driver/win/port_monitor/virtual_driver_port_monitor.gyp b/cloud_print/virtual_driver/win/port_monitor/virtual_driver_port_monitor.gyp index e04c25f..0a15c14 100644 --- a/cloud_print/virtual_driver/win/port_monitor/virtual_driver_port_monitor.gyp +++ b/cloud_print/virtual_driver/win/port_monitor/virtual_driver_port_monitor.gyp @@ -17,6 +17,7 @@ 'userenv.lib', ], 'sources': [ + '../../virtual_driver_switches.cc', '../virtual_driver_consts.h', '../virtual_driver_consts.cc', '../virtual_driver_helpers.h', |