fd1fad91cf6d9b6ca05462a201a86c149a0ee38c
[platform/framework/web/crosswalk.git] / src / chrome / service / service_utility_process_host.h
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.
4
5 #ifndef CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_
6 #define CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_
7
8 #include "build/build_config.h"
9
10 #include <string>
11 #include <vector>
12
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"
21
22 class CommandLine;
23
24 namespace base {
25 class MessageLoopProxy;
26 class ScopedTempDir;
27 }  // namespace base
28
29 namespace content {
30 class ChildProcessHost;
31 }
32
33 namespace printing {
34 class Emf;
35 struct PageRange;
36 struct PrinterCapsAndDefaults;
37 struct PrinterSemanticCapsAndDefaults;
38 }  // namespace printing
39
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
42 // a specific task.
43 class ServiceUtilityProcessHost : public content::ChildProcessHostDelegate {
44  public:
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> {
49    public:
50     Client() {}
51
52     // Called when the child process died before a reply was receieved.
53     virtual void OnChildDied() {}
54
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() {}
63
64     // Called when the printer capabilities and defaults have been
65     // retrieved successfully or if retrieval failed.
66     virtual void OnGetPrinterCapsAndDefaults(
67         bool succedded,
68         const std::string& printer_name,
69         const printing::PrinterCapsAndDefaults& caps_and_defaults) {}
70
71     // Called when the printer capabilities and defaults have been
72     // retrieved successfully or if retrieval failed.
73     virtual void OnGetPrinterSemanticCapsAndDefaults(
74         bool succedded,
75         const std::string& printer_name,
76         const printing::PrinterSemanticCapsAndDefaults& caps_and_defaults) {}
77
78    protected:
79     virtual ~Client() {}
80
81    private:
82     friend class base::RefCountedThreadSafe<Client>;
83     friend class ServiceUtilityProcessHost;
84
85     // Invoked when a metafile file is ready.
86     void MetafileAvailable(const base::FilePath& metafile_path,
87                            int highest_rendered_page_number,
88                            double scale_factor);
89
90     DISALLOW_COPY_AND_ASSIGN(Client);
91   };
92
93   ServiceUtilityProcessHost(Client* client,
94                             base::MessageLoopProxy* client_message_loop_proxy);
95   virtual ~ServiceUtilityProcessHost();
96
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);
104
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
108   // in a sandbox.
109   bool StartGetPrinterCapsAndDefaults(const std::string& printer_name);
110
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);
116
117  protected:
118   // Allows this method to be overridden for tests.
119   virtual base::FilePath GetUtilityProcessCmd();
120
121   // ChildProcessHostDelegate implementation:
122   virtual void OnChildDisconnected() OVERRIDE;
123   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
124   virtual base::ProcessHandle GetHandle() const OVERRIDE;
125
126  private:
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
129   // true.
130   bool StartProcess(bool no_sandbox, const base::FilePath& exposed_dir);
131
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,
137               bool no_sandbox,
138               const base::FilePath& exposed_dir);
139
140   base::ProcessHandle handle() const { return handle_; }
141
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);
155
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_;
168
169   DISALLOW_COPY_AND_ASSIGN(ServiceUtilityProcessHost);
170 };
171
172 #endif  // CHROME_SERVICE_SERVICE_UTILITY_PROCESS_HOST_H_