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