OLD | NEW |
| (Empty) |
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 CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_ | |
6 #define CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/process_util.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "content/common/content_export.h" | |
17 #include "content/public/browser/browser_child_process_host_delegate.h" | |
18 #include "content/public/browser/browser_thread.h" | |
19 #include "ipc/ipc_message.h" | |
20 | |
21 class BrowserChildProcessHostImpl; | |
22 | |
23 // This class acts as the browser-side host to a utility child process. A | |
24 // utility process is a short-lived sandboxed process that is created to run | |
25 // a specific task. This class lives solely on the IO thread. | |
26 // If you need a single method call in the sandbox, use StartFooBar(p). | |
27 // If you need multiple batches of work to be done in the sandboxed process, | |
28 // use StartBatchMode(), then multiple calls to StartFooBar(p), | |
29 // then finish with EndBatchMode(). | |
30 // | |
31 // Note: If your class keeps a ptr to an object of this type, grab a weak ptr to | |
32 // avoid a use after free. See http://crbug.com/108871. | |
33 class CONTENT_EXPORT UtilityProcessHost | |
34 : public content::BrowserChildProcessHostDelegate, | |
35 public IPC::Message::Sender, | |
36 public base::SupportsWeakPtr<UtilityProcessHost> { | |
37 public: | |
38 // An interface to be implemented by consumers of the utility process to | |
39 // get results back. All functions are called on the thread passed along | |
40 // to UtilityProcessHost. | |
41 class CONTENT_EXPORT Client : public base::RefCountedThreadSafe<Client> { | |
42 public: | |
43 Client(); | |
44 | |
45 // Called when the process has crashed. | |
46 virtual void OnProcessCrashed(int exit_code); | |
47 | |
48 // Allow the client to filter IPC messages. | |
49 virtual bool OnMessageReceived(const IPC::Message& message); | |
50 | |
51 protected: | |
52 friend class base::RefCountedThreadSafe<Client>; | |
53 | |
54 virtual ~Client(); | |
55 | |
56 private: | |
57 friend class UtilityProcessHost; | |
58 | |
59 DISALLOW_COPY_AND_ASSIGN(Client); | |
60 }; | |
61 | |
62 UtilityProcessHost(Client* client, | |
63 content::BrowserThread::ID client_thread_id); | |
64 virtual ~UtilityProcessHost(); | |
65 | |
66 // IPC::Message::Sender implementation: | |
67 virtual bool Send(IPC::Message* message) OVERRIDE; | |
68 | |
69 // Starts utility process in batch mode. Caller must call EndBatchMode() | |
70 // to finish the utility process. | |
71 bool StartBatchMode(); | |
72 | |
73 // Ends the utility process. Must be called after StartBatchMode(). | |
74 void EndBatchMode(); | |
75 | |
76 void set_exposed_dir(const FilePath& dir) { exposed_dir_ = dir; } | |
77 void set_no_sandbox(bool flag) { no_sandbox_ = flag; } | |
78 void set_child_flags(int flags) { child_flags_ = flags; } | |
79 void set_use_linux_zygote(bool flag) { use_linux_zygote_ = flag; } | |
80 #if defined(OS_POSIX) | |
81 void set_env(const base::environment_vector& env) { env_ = env; } | |
82 #endif | |
83 | |
84 protected: | |
85 // Allow these methods to be overridden for tests. | |
86 virtual FilePath GetUtilityProcessCmd(); | |
87 | |
88 private: | |
89 // Starts a process if necessary. Returns true if it succeeded or a process | |
90 // has already been started via StartBatchMode(). | |
91 bool StartProcess(); | |
92 | |
93 // BrowserChildProcessHost: | |
94 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
95 virtual void OnProcessCrashed(int exit_code) OVERRIDE; | |
96 | |
97 // A pointer to our client interface, who will be informed of progress. | |
98 scoped_refptr<Client> client_; | |
99 content::BrowserThread::ID client_thread_id_; | |
100 // True when running in batch mode, i.e., StartBatchMode() has been called | |
101 // and the utility process will run until EndBatchMode(). | |
102 bool is_batch_mode_; | |
103 | |
104 // Allows a directory to be opened through the sandbox, in case it's needed by | |
105 // the operation. | |
106 FilePath exposed_dir_; | |
107 | |
108 // Whether to pass switches::kNoSandbox to the child. | |
109 bool no_sandbox_; | |
110 | |
111 // Flags defined in ChildProcessHost with which to start the process. | |
112 int child_flags_; | |
113 | |
114 // If the |no_sandbox_| flag is off, and we are on Linux, launch the | |
115 // utility process from the zygote. Defaults to false. | |
116 // Can only be used for tasks that do not require FS access. | |
117 bool use_linux_zygote_; | |
118 | |
119 base::environment_vector env_; | |
120 | |
121 bool started_; | |
122 | |
123 scoped_ptr<BrowserChildProcessHostImpl> process_; | |
124 | |
125 DISALLOW_COPY_AND_ASSIGN(UtilityProcessHost); | |
126 }; | |
127 | |
128 #endif // CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_ | |
OLD | NEW |