| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_ | 5 #ifndef CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_ |
| 6 #define CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_ | 6 #define CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_ |
| 7 | 7 |
| 8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
| 9 | 9 |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include "base/basictypes.h" | |
| 14 #include "base/files/file_path.h" | |
| 15 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/process/process.h" | 12 #include "base/memory/weak_ptr.h" |
| 18 #include "content/public/common/child_process_host_delegate.h" | 13 #include "content/public/common/child_process_host_delegate.h" |
| 19 #include "ipc/ipc_channel.h" | 14 #include "ipc/ipc_platform_file.h" |
| 20 #include "printing/pdf_render_settings.h" | |
| 21 | 15 |
| 22 namespace base { | 16 namespace base { |
| 23 class CommandLine; | 17 class CommandLine; |
| 18 class File; |
| 19 class FilePath; |
| 24 class MessageLoopProxy; | 20 class MessageLoopProxy; |
| 25 class ScopedTempDir; | 21 class ScopedTempDir; |
| 26 } // namespace base | 22 } // namespace base |
| 27 | 23 |
| 28 namespace content { | 24 namespace content { |
| 29 class ChildProcessHost; | 25 class ChildProcessHost; |
| 30 } | 26 } |
| 31 | 27 |
| 32 namespace printing { | 28 namespace printing { |
| 33 class MetafilePlayer; | 29 class MetafilePlayer; |
| 30 class PdfRenderSettings; |
| 34 struct PageRange; | 31 struct PageRange; |
| 35 struct PrinterCapsAndDefaults; | 32 struct PrinterCapsAndDefaults; |
| 36 struct PrinterSemanticCapsAndDefaults; | 33 struct PrinterSemanticCapsAndDefaults; |
| 37 } // namespace printing | 34 } // namespace printing |
| 38 | 35 |
| 39 // Acts as the service-side host to a utility child process. A | 36 // Acts as the service-side host to a utility child process. A |
| 40 // utility process is a short-lived sandboxed process that is created to run | 37 // utility process is a short-lived sandboxed process that is created to run |
| 41 // a specific task. | 38 // a specific task. |
| 42 class ServiceUtilityProcessHost : public content::ChildProcessHostDelegate { | 39 class ServiceUtilityProcessHost : public content::ChildProcessHostDelegate { |
| 43 public: | 40 public: |
| 44 // Consumers of ServiceUtilityProcessHost must implement this interface to | 41 // Consumers of ServiceUtilityProcessHost must implement this interface to |
| 45 // get results back. All functions are called on the thread passed along | 42 // get results back. All functions are called on the thread passed along |
| 46 // to ServiceUtilityProcessHost. | 43 // to ServiceUtilityProcessHost. |
| 47 class Client : public base::RefCountedThreadSafe<Client> { | 44 class Client : public base::RefCountedThreadSafe<Client> { |
| 48 public: | 45 public: |
| 49 Client() {} | 46 Client() {} |
| 50 | 47 |
| 51 // Called when the child process died before a reply was receieved. | 48 // Called when the child process died before a reply was receieved. |
| 52 virtual void OnChildDied() {} | 49 virtual void OnChildDied() {} |
| 53 | 50 |
| 54 // Called when at least one page in the specified PDF has been rendered | 51 virtual void OnRenderPDFPagesToMetafilePageDone( |
| 55 // successfully into |metafile|. | 52 double scale_factor, |
| 56 virtual void OnRenderPDFPagesToMetafileSucceeded( | 53 const printing::MetafilePlayer& emf) {} |
| 57 const printing::MetafilePlayer& metafile, | 54 |
| 58 int highest_rendered_page_number, | 55 // Called when at all pages in the PDF has been rendered. |
| 59 double scale_factor) {} | 56 virtual void OnRenderPDFPagesToMetafileDone(bool success) {} |
| 60 // Called when no page in the passed in PDF could be rendered. | |
| 61 virtual void OnRenderPDFPagesToMetafileFailed() {} | |
| 62 | 57 |
| 63 // Called when the printer capabilities and defaults have been | 58 // Called when the printer capabilities and defaults have been |
| 64 // retrieved successfully or if retrieval failed. | 59 // retrieved successfully or if retrieval failed. |
| 65 virtual void OnGetPrinterCapsAndDefaults( | 60 virtual void OnGetPrinterCapsAndDefaults( |
| 66 bool succedded, | 61 bool succedded, |
| 67 const std::string& printer_name, | 62 const std::string& printer_name, |
| 68 const printing::PrinterCapsAndDefaults& caps_and_defaults) {} | 63 const printing::PrinterCapsAndDefaults& caps_and_defaults) {} |
| 69 | 64 |
| 70 // Called when the printer capabilities and defaults have been | 65 // Called when the printer capabilities and defaults have been |
| 71 // retrieved successfully or if retrieval failed. | 66 // retrieved successfully or if retrieval failed. |
| 72 virtual void OnGetPrinterSemanticCapsAndDefaults( | 67 virtual void OnGetPrinterSemanticCapsAndDefaults( |
| 73 bool succedded, | 68 bool succedded, |
| 74 const std::string& printer_name, | 69 const std::string& printer_name, |
| 75 const printing::PrinterSemanticCapsAndDefaults& caps_and_defaults) {} | 70 const printing::PrinterSemanticCapsAndDefaults& caps_and_defaults) {} |
| 76 | 71 |
| 77 protected: | 72 protected: |
| 78 virtual ~Client() {} | 73 virtual ~Client() {} |
| 79 | 74 |
| 80 private: | 75 private: |
| 81 friend class base::RefCountedThreadSafe<Client>; | 76 friend class base::RefCountedThreadSafe<Client>; |
| 82 friend class ServiceUtilityProcessHost; | 77 friend class ServiceUtilityProcessHost; |
| 83 | 78 |
| 84 // Invoked when a metafile file is ready. | 79 // Invoked when a metafile file is ready. |
| 85 void MetafileAvailable(const base::FilePath& metafile_path, | 80 // Returns true if metafile successfully loaded from |file|. |
| 86 int highest_rendered_page_number, | 81 bool MetafileAvailable(double scale_factor, base::File file); |
| 87 double scale_factor); | |
| 88 | 82 |
| 89 DISALLOW_COPY_AND_ASSIGN(Client); | 83 DISALLOW_COPY_AND_ASSIGN(Client); |
| 90 }; | 84 }; |
| 91 | 85 |
| 92 ServiceUtilityProcessHost(Client* client, | 86 ServiceUtilityProcessHost(Client* client, |
| 93 base::MessageLoopProxy* client_message_loop_proxy); | 87 base::MessageLoopProxy* client_message_loop_proxy); |
| 94 virtual ~ServiceUtilityProcessHost(); | 88 virtual ~ServiceUtilityProcessHost(); |
| 95 | 89 |
| 96 // Starts a process to render the specified pages in the given PDF file into | 90 // Starts a process to render the specified pages in the given PDF file into |
| 97 // a metafile. Currently only implemented for Windows. If the PDF has fewer | 91 // a metafile. Currently only implemented for Windows. If the PDF has fewer |
| 98 // pages than the specified page ranges, it will render as many as available. | 92 // pages than the specified page ranges, it will render as many as available. |
| 99 bool StartRenderPDFPagesToMetafile( | 93 bool StartRenderPDFPagesToMetafile( |
| 100 const base::FilePath& pdf_path, | 94 const base::FilePath& pdf_path, |
| 101 const printing::PdfRenderSettings& render_settings, | 95 const printing::PdfRenderSettings& render_settings); |
| 102 const std::vector<printing::PageRange>& page_ranges); | |
| 103 | 96 |
| 104 // Starts a process to get capabilities and defaults for the specified | 97 // Starts a process to get capabilities and defaults for the specified |
| 105 // printer. Used on Windows to isolate the service process from printer driver | 98 // printer. Used on Windows to isolate the service process from printer driver |
| 106 // crashes by executing this in a separate process. The process does not run | 99 // crashes by executing this in a separate process. The process does not run |
| 107 // in a sandbox. | 100 // in a sandbox. |
| 108 bool StartGetPrinterCapsAndDefaults(const std::string& printer_name); | 101 bool StartGetPrinterCapsAndDefaults(const std::string& printer_name); |
| 109 | 102 |
| 110 // Starts a process to get capabilities and defaults for the specified | 103 // Starts a process to get capabilities and defaults for the specified |
| 111 // printer. Used on Windows to isolate the service process from printer driver | 104 // printer. Used on Windows to isolate the service process from printer driver |
| 112 // crashes by executing this in a separate process. The process does not run | 105 // crashes by executing this in a separate process. The process does not run |
| 113 // in a sandbox. Returns result as printing::PrinterSemanticCapsAndDefaults. | 106 // in a sandbox. Returns result as printing::PrinterSemanticCapsAndDefaults. |
| 114 bool StartGetPrinterSemanticCapsAndDefaults(const std::string& printer_name); | 107 bool StartGetPrinterSemanticCapsAndDefaults(const std::string& printer_name); |
| 115 | 108 |
| 109 bool Send(IPC::Message* msg); |
| 110 |
| 116 protected: | 111 protected: |
| 117 // Allows this method to be overridden for tests. | 112 // Allows this method to be overridden for tests. |
| 118 virtual base::FilePath GetUtilityProcessCmd(); | 113 virtual base::FilePath GetUtilityProcessCmd(); |
| 119 | 114 |
| 120 // ChildProcessHostDelegate implementation: | 115 // ChildProcessHostDelegate implementation: |
| 121 virtual void OnChildDisconnected() OVERRIDE; | 116 virtual void OnChildDisconnected() OVERRIDE; |
| 122 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 117 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 123 virtual base::ProcessHandle GetHandle() const OVERRIDE; | 118 virtual base::ProcessHandle GetHandle() const OVERRIDE; |
| 124 | 119 |
| 125 private: | 120 private: |
| 126 // Starts a process. Returns true iff it succeeded. |exposed_dir| is the | 121 // Starts a process. Returns true iff it succeeded. |
| 127 // path to the exposed to the sandbox. This is ignored if |no_sandbox| is | 122 bool StartProcess(bool no_sandbox); |
| 128 // true. | |
| 129 bool StartProcess(bool no_sandbox, const base::FilePath& exposed_dir); | |
| 130 | 123 |
| 131 // Launch the child process synchronously. | 124 // Launch the child process synchronously. |
| 132 // TODO(sanjeevr): Determine whether we need to make the launch asynchronous. | 125 bool Launch(base::CommandLine* cmd_line, bool no_sandbox); |
| 133 // |exposed_dir| is the path to tbe exposed to the sandbox. This is ignored | |
| 134 // if |no_sandbox| is true. | |
| 135 bool Launch(base::CommandLine* cmd_line, | |
| 136 bool no_sandbox, | |
| 137 const base::FilePath& exposed_dir); | |
| 138 | 126 |
| 139 base::ProcessHandle handle() const { return handle_; } | 127 base::ProcessHandle handle() const { return handle_; } |
| 140 | 128 |
| 129 void OnMetafileSpooled(bool success); |
| 130 void OnPDFToEmfFinished(bool success); |
| 131 |
| 141 // Messages handlers: | 132 // Messages handlers: |
| 142 void OnRenderPDFPagesToMetafilesSucceeded( | 133 void OnRenderPDFPagesToMetafilesPageCount(int page_count); |
| 143 const std::vector<printing::PageRange>& page_ranges, | 134 void OnRenderPDFPagesToMetafilesPageDone(bool success, double scale_factor); |
| 144 double scale_factor); | |
| 145 void OnRenderPDFPagesToMetafileFailed(); | |
| 146 void OnGetPrinterCapsAndDefaultsSucceeded( | 135 void OnGetPrinterCapsAndDefaultsSucceeded( |
| 147 const std::string& printer_name, | 136 const std::string& printer_name, |
| 148 const printing::PrinterCapsAndDefaults& caps_and_defaults); | 137 const printing::PrinterCapsAndDefaults& caps_and_defaults); |
| 149 void OnGetPrinterCapsAndDefaultsFailed(const std::string& printer_name); | 138 void OnGetPrinterCapsAndDefaultsFailed(const std::string& printer_name); |
| 150 void OnGetPrinterSemanticCapsAndDefaultsSucceeded( | 139 void OnGetPrinterSemanticCapsAndDefaultsSucceeded( |
| 151 const std::string& printer_name, | 140 const std::string& printer_name, |
| 152 const printing::PrinterSemanticCapsAndDefaults& caps_and_defaults); | 141 const printing::PrinterSemanticCapsAndDefaults& caps_and_defaults); |
| 153 void OnGetPrinterSemanticCapsAndDefaultsFailed( | 142 void OnGetPrinterSemanticCapsAndDefaultsFailed( |
| 154 const std::string& printer_name); | 143 const std::string& printer_name); |
| 155 | 144 |
| 156 scoped_ptr<content::ChildProcessHost> child_process_host_; | 145 scoped_ptr<content::ChildProcessHost> child_process_host_; |
| 157 base::ProcessHandle handle_; | 146 base::ProcessHandle handle_; |
| 158 // A pointer to our client interface, who will be informed of progress. | 147 // A pointer to our client interface, who will be informed of progress. |
| 159 scoped_refptr<Client> client_; | 148 scoped_refptr<Client> client_; |
| 160 scoped_refptr<base::MessageLoopProxy> client_message_loop_proxy_; | 149 scoped_refptr<base::MessageLoopProxy> client_message_loop_proxy_; |
| 161 bool waiting_for_reply_; | 150 bool waiting_for_reply_; |
| 162 // The base path to the temp file where the metafile will be written to. | 151 |
| 163 base::FilePath metafile_path_; | |
| 164 // The temporary folder created for the metafile. | |
| 165 scoped_ptr<base::ScopedTempDir> scratch_metafile_dir_; | |
| 166 // Start time of operation. | 152 // Start time of operation. |
| 167 base::Time start_time_; | 153 base::Time start_time_; |
| 168 | 154 |
| 155 class PdfToEmfState; |
| 156 scoped_ptr<PdfToEmfState> pdf_to_emf_state_; |
| 157 |
| 158 base::WeakPtrFactory<ServiceUtilityProcessHost> weak_ptr_factory_; |
| 159 |
| 169 DISALLOW_COPY_AND_ASSIGN(ServiceUtilityProcessHost); | 160 DISALLOW_COPY_AND_ASSIGN(ServiceUtilityProcessHost); |
| 170 }; | 161 }; |
| 171 | 162 |
| 172 #endif // CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_ | 163 #endif // CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_ |
| OLD | NEW |