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"
20 class MessageLoopProxy
;
25 class ChildProcessHost
;
30 class PdfRenderSettings
;
32 struct PrinterCapsAndDefaults
;
33 struct PrinterSemanticCapsAndDefaults
;
34 } // namespace printing
36 // Acts as the service-side host to a utility child process. A
37 // utility process is a short-lived sandboxed process that is created to run
39 class ServiceUtilityProcessHost
: public content::ChildProcessHostDelegate
{
41 // Consumers of ServiceUtilityProcessHost must implement this interface to
42 // get results back. All functions are called on the thread passed along
43 // to ServiceUtilityProcessHost.
44 class Client
: public base::RefCountedThreadSafe
<Client
> {
48 // Called when the child process died before a reply was receieved.
49 virtual void OnChildDied() {}
51 virtual void OnRenderPDFPagesToMetafilePageDone(
53 const printing::MetafilePlayer
& emf
) {}
55 // Called when at all pages in the PDF has been rendered.
56 virtual void OnRenderPDFPagesToMetafileDone(bool success
) {}
58 // Called when the printer capabilities and defaults have been
59 // retrieved successfully or if retrieval failed.
60 virtual void OnGetPrinterCapsAndDefaults(
62 const std::string
& printer_name
,
63 const printing::PrinterCapsAndDefaults
& caps_and_defaults
) {}
65 // Called when the printer capabilities and defaults have been
66 // retrieved successfully or if retrieval failed.
67 virtual void OnGetPrinterSemanticCapsAndDefaults(
69 const std::string
& printer_name
,
70 const printing::PrinterSemanticCapsAndDefaults
& caps_and_defaults
) {}
76 friend class base::RefCountedThreadSafe
<Client
>;
77 friend class ServiceUtilityProcessHost
;
79 // Invoked when a metafile file is ready.
80 // Returns true if metafile successfully loaded from |file|.
81 bool MetafileAvailable(float scale_factor
, base::File file
);
83 DISALLOW_COPY_AND_ASSIGN(Client
);
86 ServiceUtilityProcessHost(Client
* client
,
87 base::MessageLoopProxy
* client_message_loop_proxy
);
88 virtual ~ServiceUtilityProcessHost();
90 // Starts a process to render the specified pages in the given PDF file into
91 // a metafile. Currently only implemented for Windows. If the PDF has fewer
92 // pages than the specified page ranges, it will render as many as available.
93 bool StartRenderPDFPagesToMetafile(
94 const base::FilePath
& pdf_path
,
95 const printing::PdfRenderSettings
& render_settings
);
97 // Starts a process to get capabilities and defaults for the specified
98 // printer. Used on Windows to isolate the service process from printer driver
99 // crashes by executing this in a separate process. The process does not run
101 bool StartGetPrinterCapsAndDefaults(const std::string
& printer_name
);
103 // Starts a process to get capabilities and defaults for the specified
104 // printer. Used on Windows to isolate the service process from printer driver
105 // crashes by executing this in a separate process. The process does not run
106 // in a sandbox. Returns result as printing::PrinterSemanticCapsAndDefaults.
107 bool StartGetPrinterSemanticCapsAndDefaults(const std::string
& printer_name
);
110 bool Send(IPC::Message
* msg
);
112 // Allows this method to be overridden for tests.
113 virtual base::FilePath
GetUtilityProcessCmd();
115 // ChildProcessHostDelegate implementation:
116 virtual void OnChildDisconnected() override
;
117 virtual bool OnMessageReceived(const IPC::Message
& message
) override
;
118 virtual const base::Process
& GetProcess() const override
;
121 // Starts a process. Returns true iff it succeeded.
122 bool StartProcess(bool no_sandbox
);
124 // Launch the child process synchronously.
125 bool Launch(base::CommandLine
* cmd_line
, bool no_sandbox
);
127 base::ProcessHandle
handle() const { return process_
.Handle(); }
129 void OnMetafileSpooled(bool success
);
130 void OnPDFToEmfFinished(bool success
);
132 // Messages handlers:
133 void OnRenderPDFPagesToMetafilesPageCount(int page_count
);
134 void OnRenderPDFPagesToMetafilesPageDone(bool success
, float scale_factor
);
135 void OnGetPrinterCapsAndDefaultsSucceeded(
136 const std::string
& printer_name
,
137 const printing::PrinterCapsAndDefaults
& caps_and_defaults
);
138 void OnGetPrinterCapsAndDefaultsFailed(const std::string
& printer_name
);
139 void OnGetPrinterSemanticCapsAndDefaultsSucceeded(
140 const std::string
& printer_name
,
141 const printing::PrinterSemanticCapsAndDefaults
& caps_and_defaults
);
142 void OnGetPrinterSemanticCapsAndDefaultsFailed(
143 const std::string
& printer_name
);
145 scoped_ptr
<content::ChildProcessHost
> child_process_host_
;
146 base::Process process_
;
147 // A pointer to our client interface, who will be informed of progress.
148 scoped_refptr
<Client
> client_
;
149 scoped_refptr
<base::MessageLoopProxy
> client_message_loop_proxy_
;
150 bool waiting_for_reply_
;
152 // Start time of operation.
153 base::Time start_time_
;
156 scoped_ptr
<PdfToEmfState
> pdf_to_emf_state_
;
158 base::WeakPtrFactory
<ServiceUtilityProcessHost
> weak_ptr_factory_
;
160 DISALLOW_COPY_AND_ASSIGN(ServiceUtilityProcessHost
);
163 #endif // CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_