There are many ways to get it wrong.
[chromium-blink-merge.git] / chrome / service / service_utility_process_host.h
blob855e2a78bdbaf7a453566611cd6524215a5ab4d7
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"
10 #include <string>
11 #include <vector>
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"
22 namespace base {
23 class CommandLine;
24 class MessageLoopProxy;
25 class ScopedTempDir;
26 } // namespace base
28 namespace content {
29 class ChildProcessHost;
32 namespace printing {
33 class Emf;
34 struct PageRange;
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
41 // a specific task.
42 class ServiceUtilityProcessHost : public content::ChildProcessHostDelegate {
43 public:
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> {
48 public:
49 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(
66 bool succedded,
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(
73 bool succedded,
74 const std::string& printer_name,
75 const printing::PrinterSemanticCapsAndDefaults& caps_and_defaults) {}
77 protected:
78 virtual ~Client() {}
80 private:
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,
87 double scale_factor);
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
107 // in a sandbox.
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);
116 protected:
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;
125 private:
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
128 // true.
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,
136 bool no_sandbox,
137 const base::FilePath& exposed_dir);
139 base::ProcessHandle handle() const { return handle_; }
141 // Messages handlers:
142 void OnRenderPDFPagesToMetafileSucceeded(int highest_rendered_page_number,
143 double scale_factor);
144 void OnRenderPDFPagesToMetafileFailed();
145 void OnGetPrinterCapsAndDefaultsSucceeded(
146 const std::string& printer_name,
147 const printing::PrinterCapsAndDefaults& caps_and_defaults);
148 void OnGetPrinterCapsAndDefaultsFailed(const std::string& printer_name);
149 void OnGetPrinterSemanticCapsAndDefaultsSucceeded(
150 const std::string& printer_name,
151 const printing::PrinterSemanticCapsAndDefaults& caps_and_defaults);
152 void OnGetPrinterSemanticCapsAndDefaultsFailed(
153 const std::string& printer_name);
155 scoped_ptr<content::ChildProcessHost> child_process_host_;
156 base::ProcessHandle handle_;
157 // A pointer to our client interface, who will be informed of progress.
158 scoped_refptr<Client> client_;
159 scoped_refptr<base::MessageLoopProxy> client_message_loop_proxy_;
160 bool waiting_for_reply_;
161 // The path to the temp file where the metafile will be written to.
162 base::FilePath metafile_path_;
163 // The temporary folder created for the metafile.
164 scoped_ptr<base::ScopedTempDir> scratch_metafile_dir_;
165 // Start time of operation.
166 base::Time start_time_;
168 DISALLOW_COPY_AND_ASSIGN(ServiceUtilityProcessHost);
171 #endif // CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_