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.
5 #ifndef CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_
6 #define CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_
8 #include "build/build_config.h"
13 #include "base/basictypes.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/process/process.h"
18 #include "content/public/common/child_process_host_delegate.h"
19 #include "ipc/ipc_channel.h"
20 #include "printing/pdf_render_settings.h"
24 class MessageLoopProxy
;
29 class ChildProcessHost
;
35 struct PrinterCapsAndDefaults
;
36 struct PrinterSemanticCapsAndDefaults
;
37 } // namespace printing
39 // 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
42 class ServiceUtilityProcessHost
: public content::ChildProcessHostDelegate
{
44 // Consumers of ServiceUtilityProcessHost must implement this interface to
45 // get results back. All functions are called on the thread passed along
46 // to ServiceUtilityProcessHost.
47 class Client
: public base::RefCountedThreadSafe
<Client
> {
51 // Called when the child process died before a reply was receieved.
52 virtual void OnChildDied() {}
54 // Called when at least one page in the specified PDF has been rendered
55 // successfully into |metafile|.
56 virtual void OnRenderPDFPagesToMetafileSucceeded(
57 const printing::Emf
& metafile
,
58 int highest_rendered_page_number
,
59 double scale_factor
) {}
60 // Called when no page in the passed in PDF could be rendered.
61 virtual void OnRenderPDFPagesToMetafileFailed() {}
63 // Called when the printer capabilities and defaults have been
64 // retrieved successfully or if retrieval failed.
65 virtual void OnGetPrinterCapsAndDefaults(
67 const std::string
& printer_name
,
68 const printing::PrinterCapsAndDefaults
& caps_and_defaults
) {}
70 // Called when the printer capabilities and defaults have been
71 // retrieved successfully or if retrieval failed.
72 virtual void OnGetPrinterSemanticCapsAndDefaults(
74 const std::string
& printer_name
,
75 const printing::PrinterSemanticCapsAndDefaults
& caps_and_defaults
) {}
81 friend class base::RefCountedThreadSafe
<Client
>;
82 friend class ServiceUtilityProcessHost
;
84 // Invoked when a metafile file is ready.
85 void MetafileAvailable(const base::FilePath
& metafile_path
,
86 int highest_rendered_page_number
,
89 DISALLOW_COPY_AND_ASSIGN(Client
);
92 ServiceUtilityProcessHost(Client
* client
,
93 base::MessageLoopProxy
* client_message_loop_proxy
);
94 virtual ~ServiceUtilityProcessHost();
96 // 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
98 // pages than the specified page ranges, it will render as many as available.
99 bool StartRenderPDFPagesToMetafile(
100 const base::FilePath
& pdf_path
,
101 const printing::PdfRenderSettings
& render_settings
,
102 const std::vector
<printing::PageRange
>& page_ranges
);
104 // Starts a process to get capabilities and defaults for the specified
105 // 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
108 bool StartGetPrinterCapsAndDefaults(const std::string
& printer_name
);
110 // Starts a process to get capabilities and defaults for the specified
111 // 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
113 // in a sandbox. Returns result as printing::PrinterSemanticCapsAndDefaults.
114 bool StartGetPrinterSemanticCapsAndDefaults(const std::string
& printer_name
);
117 // Allows this method to be overridden for tests.
118 virtual base::FilePath
GetUtilityProcessCmd();
120 // ChildProcessHostDelegate implementation:
121 virtual void OnChildDisconnected() OVERRIDE
;
122 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
123 virtual base::ProcessHandle
GetHandle() const OVERRIDE
;
126 // Starts a process. Returns true iff it succeeded. |exposed_dir| is the
127 // path to the exposed to the sandbox. This is ignored if |no_sandbox| is
129 bool StartProcess(bool no_sandbox
, const base::FilePath
& exposed_dir
);
131 // Launch the child process synchronously.
132 // TODO(sanjeevr): Determine whether we need to make the launch asynchronous.
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
,
137 const base::FilePath
& exposed_dir
);
139 base::ProcessHandle
handle() const { return handle_
; }
141 // Messages handlers:
142 void OnRenderPDFPagesToMetafilesSucceeded(
143 const std::vector
<printing::PageRange
>& page_ranges
,
144 double scale_factor
);
145 void OnRenderPDFPagesToMetafileFailed();
146 void OnGetPrinterCapsAndDefaultsSucceeded(
147 const std::string
& printer_name
,
148 const printing::PrinterCapsAndDefaults
& caps_and_defaults
);
149 void OnGetPrinterCapsAndDefaultsFailed(const std::string
& printer_name
);
150 void OnGetPrinterSemanticCapsAndDefaultsSucceeded(
151 const std::string
& printer_name
,
152 const printing::PrinterSemanticCapsAndDefaults
& caps_and_defaults
);
153 void OnGetPrinterSemanticCapsAndDefaultsFailed(
154 const std::string
& printer_name
);
156 scoped_ptr
<content::ChildProcessHost
> child_process_host_
;
157 base::ProcessHandle handle_
;
158 // A pointer to our client interface, who will be informed of progress.
159 scoped_refptr
<Client
> client_
;
160 scoped_refptr
<base::MessageLoopProxy
> client_message_loop_proxy_
;
161 bool waiting_for_reply_
;
162 // The base path to the temp file where the metafile will be written to.
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.
167 base::Time start_time_
;
169 DISALLOW_COPY_AND_ASSIGN(ServiceUtilityProcessHost
);
172 #endif // CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_