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 "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/public/common/child_process_host_delegate.h"
14 #include "ipc/ipc_platform_file.h"
24 class ChildProcessHost
;
29 class PdfRenderSettings
;
31 struct PrinterCapsAndDefaults
;
32 struct PrinterSemanticCapsAndDefaults
;
33 } // namespace printing
35 // Acts as the service-side host to a utility child process. A
36 // utility process is a short-lived sandboxed process that is created to run
38 class ServiceUtilityProcessHost
: public content::ChildProcessHostDelegate
{
40 // Consumers of ServiceUtilityProcessHost must implement this interface to
41 // get results back. All functions are called on the thread passed along
42 // to ServiceUtilityProcessHost.
43 class Client
: public base::RefCountedThreadSafe
<Client
> {
47 // Called when the child process died before a reply was receieved.
48 virtual void OnChildDied() {}
50 virtual void OnRenderPDFPagesToMetafilePageDone(
52 const printing::MetafilePlayer
& emf
) {}
54 // Called when at all pages in the PDF has been rendered.
55 virtual void OnRenderPDFPagesToMetafileDone(bool success
) {}
57 // Called when the printer capabilities and defaults have been
58 // retrieved successfully or if retrieval failed.
59 virtual void OnGetPrinterCapsAndDefaults(
61 const std::string
& printer_name
,
62 const printing::PrinterCapsAndDefaults
& caps_and_defaults
) {}
64 // Called when the printer capabilities and defaults have been
65 // retrieved successfully or if retrieval failed.
66 virtual void OnGetPrinterSemanticCapsAndDefaults(
68 const std::string
& printer_name
,
69 const printing::PrinterSemanticCapsAndDefaults
& caps_and_defaults
) {}
75 friend class base::RefCountedThreadSafe
<Client
>;
76 friend class ServiceUtilityProcessHost
;
78 // Invoked when a metafile file is ready.
79 // Returns true if metafile successfully loaded from |file|.
80 bool MetafileAvailable(float scale_factor
, base::File file
);
82 DISALLOW_COPY_AND_ASSIGN(Client
);
85 ServiceUtilityProcessHost(Client
* client
,
86 base::SingleThreadTaskRunner
* client_task_runner
);
87 ~ServiceUtilityProcessHost() override
;
89 // Starts a process to render the specified pages in the given PDF file into
90 // a metafile. Currently only implemented for Windows. If the PDF has fewer
91 // pages than the specified page ranges, it will render as many as available.
92 bool StartRenderPDFPagesToMetafile(
93 const base::FilePath
& pdf_path
,
94 const printing::PdfRenderSettings
& render_settings
);
96 // Starts a process to get capabilities and defaults for the specified
97 // printer. Used on Windows to isolate the service process from printer driver
98 // crashes by executing this in a separate process. The process does not run
100 bool StartGetPrinterCapsAndDefaults(const std::string
& printer_name
);
102 // Starts a process to get capabilities and defaults for the specified
103 // printer. Used on Windows to isolate the service process from printer driver
104 // crashes by executing this in a separate process. The process does not run
105 // in a sandbox. Returns result as printing::PrinterSemanticCapsAndDefaults.
106 bool StartGetPrinterSemanticCapsAndDefaults(const std::string
& printer_name
);
109 bool Send(IPC::Message
* msg
);
111 // Allows this method to be overridden for tests.
112 virtual base::FilePath
GetUtilityProcessCmd();
114 // ChildProcessHostDelegate implementation:
115 void OnChildDisconnected() override
;
116 bool OnMessageReceived(const IPC::Message
& message
) override
;
117 const base::Process
& GetProcess() const override
;
120 // Starts a process. Returns true iff it succeeded.
121 bool StartProcess(bool no_sandbox
);
123 // Launch the child process synchronously.
124 bool Launch(base::CommandLine
* cmd_line
, bool no_sandbox
);
126 base::ProcessHandle
handle() const { return process_
.Handle(); }
128 void OnMetafileSpooled(bool success
);
129 void OnPDFToEmfFinished(bool success
);
131 // Messages handlers:
132 void OnRenderPDFPagesToMetafilesPageCount(int page_count
);
133 void OnRenderPDFPagesToMetafilesPageDone(bool success
, float scale_factor
);
134 void OnGetPrinterCapsAndDefaultsSucceeded(
135 const std::string
& printer_name
,
136 const printing::PrinterCapsAndDefaults
& caps_and_defaults
);
137 void OnGetPrinterCapsAndDefaultsFailed(const std::string
& printer_name
);
138 void OnGetPrinterSemanticCapsAndDefaultsSucceeded(
139 const std::string
& printer_name
,
140 const printing::PrinterSemanticCapsAndDefaults
& caps_and_defaults
);
141 void OnGetPrinterSemanticCapsAndDefaultsFailed(
142 const std::string
& printer_name
);
144 scoped_ptr
<content::ChildProcessHost
> child_process_host_
;
145 base::Process process_
;
146 // A pointer to our client interface, who will be informed of progress.
147 scoped_refptr
<Client
> client_
;
148 scoped_refptr
<base::SingleThreadTaskRunner
> client_task_runner_
;
149 bool waiting_for_reply_
;
151 // Start time of operation.
152 base::Time start_time_
;
155 scoped_ptr
<PdfToEmfState
> pdf_to_emf_state_
;
157 base::WeakPtrFactory
<ServiceUtilityProcessHost
> weak_ptr_factory_
;
159 DISALLOW_COPY_AND_ASSIGN(ServiceUtilityProcessHost
);
162 #endif // CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_