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"
25 class MessageLoopProxy
;
30 class ChildProcessHost
;
36 struct PrinterCapsAndDefaults
;
37 struct PrinterSemanticCapsAndDefaults
;
38 } // namespace printing
40 // Acts as the service-side host to a utility child process. A
41 // utility process is a short-lived sandboxed process that is created to run
43 class ServiceUtilityProcessHost
: public content::ChildProcessHostDelegate
{
45 // Consumers of ServiceUtilityProcessHost must implement this interface to
46 // get results back. All functions are called on the thread passed along
47 // to ServiceUtilityProcessHost.
48 class Client
: public base::RefCountedThreadSafe
<Client
> {
52 // Called when the child process died before a reply was receieved.
53 virtual void OnChildDied() {}
55 // Called when at least one page in the specified PDF has been rendered
56 // successfully into |metafile|.
57 virtual void OnRenderPDFPagesToMetafileSucceeded(
58 const printing::Emf
& metafile
,
59 int highest_rendered_page_number
,
60 double scale_factor
) {}
61 // Called when no page in the passed in PDF could be rendered.
62 virtual void OnRenderPDFPagesToMetafileFailed() {}
64 // Called when the printer capabilities and defaults have been
65 // retrieved successfully or if retrieval failed.
66 virtual void OnGetPrinterCapsAndDefaults(
68 const std::string
& printer_name
,
69 const printing::PrinterCapsAndDefaults
& caps_and_defaults
) {}
71 // Called when the printer capabilities and defaults have been
72 // retrieved successfully or if retrieval failed.
73 virtual void OnGetPrinterSemanticCapsAndDefaults(
75 const std::string
& printer_name
,
76 const printing::PrinterSemanticCapsAndDefaults
& caps_and_defaults
) {}
82 friend class base::RefCountedThreadSafe
<Client
>;
83 friend class ServiceUtilityProcessHost
;
85 // Invoked when a metafile file is ready.
86 void MetafileAvailable(const base::FilePath
& metafile_path
,
87 int highest_rendered_page_number
,
90 DISALLOW_COPY_AND_ASSIGN(Client
);
93 ServiceUtilityProcessHost(Client
* client
,
94 base::MessageLoopProxy
* client_message_loop_proxy
);
95 virtual ~ServiceUtilityProcessHost();
97 // Starts a process to render the specified pages in the given PDF file into
98 // a metafile. Currently only implemented for Windows. If the PDF has fewer
99 // pages than the specified page ranges, it will render as many as available.
100 bool StartRenderPDFPagesToMetafile(
101 const base::FilePath
& pdf_path
,
102 const printing::PdfRenderSettings
& render_settings
,
103 const std::vector
<printing::PageRange
>& page_ranges
);
105 // Starts a process to get capabilities and defaults for the specified
106 // printer. Used on Windows to isolate the service process from printer driver
107 // crashes by executing this in a separate process. The process does not run
109 bool StartGetPrinterCapsAndDefaults(const std::string
& printer_name
);
111 // Starts a process to get capabilities and defaults for the specified
112 // printer. Used on Windows to isolate the service process from printer driver
113 // crashes by executing this in a separate process. The process does not run
114 // in a sandbox. Returns result as printing::PrinterSemanticCapsAndDefaults.
115 bool StartGetPrinterSemanticCapsAndDefaults(const std::string
& printer_name
);
118 // Allows this method to be overridden for tests.
119 virtual base::FilePath
GetUtilityProcessCmd();
121 // ChildProcessHostDelegate implementation:
122 virtual void OnChildDisconnected() OVERRIDE
;
123 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
124 virtual base::ProcessHandle
GetHandle() const OVERRIDE
;
127 // Starts a process. Returns true iff it succeeded. |exposed_dir| is the
128 // path to the exposed to the sandbox. This is ignored if |no_sandbox| is
130 bool StartProcess(bool no_sandbox
, const base::FilePath
& exposed_dir
);
132 // Launch the child process synchronously.
133 // TODO(sanjeevr): Determine whether we need to make the launch asynchronous.
134 // |exposed_dir| is the path to tbe exposed to the sandbox. This is ignored
135 // if |no_sandbox| is true.
136 bool Launch(CommandLine
* cmd_line
,
138 const base::FilePath
& exposed_dir
);
140 base::ProcessHandle
handle() const { return handle_
; }
142 // Messages handlers:
143 void OnRenderPDFPagesToMetafileSucceeded(int highest_rendered_page_number
,
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 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_