| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/compiler_specific.h" |
| 13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 14 #include "base/process_util.h" | |
| 15 #include "base/memory/weak_ptr.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" | 16 #include "content/public/browser/browser_child_process_host_delegate.h" |
| 18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/utility_process_host.h" |
| 19 #include "ipc/ipc_message.h" | |
| 20 | 18 |
| 21 class BrowserChildProcessHostImpl; | 19 class BrowserChildProcessHostImpl; |
| 22 | 20 |
| 23 // This class acts as the browser-side host to a utility child process. A | 21 class CONTENT_EXPORT UtilityProcessHostImpl |
| 24 // utility process is a short-lived sandboxed process that is created to run | 22 : public NON_EXPORTED_BASE(content::UtilityProcessHost), |
| 25 // a specific task. This class lives solely on the IO thread. | 23 public content::BrowserChildProcessHostDelegate { |
| 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: | 24 public: |
| 38 // An interface to be implemented by consumers of the utility process to | 25 UtilityProcessHostImpl(content::UtilityProcessHostClient* client, |
| 39 // get results back. All functions are called on the thread passed along | 26 content::BrowserThread::ID client_thread_id); |
| 40 // to UtilityProcessHost. | 27 virtual ~UtilityProcessHostImpl(); |
| 41 class CONTENT_EXPORT Client : public base::RefCountedThreadSafe<Client> { | |
| 42 public: | |
| 43 Client(); | |
| 44 | 28 |
| 45 // Called when the process has crashed. | 29 // UtilityProcessHost implementation: |
| 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; | 30 virtual bool Send(IPC::Message* message) OVERRIDE; |
| 68 | 31 virtual bool StartBatchMode() OVERRIDE; |
| 69 // Starts utility process in batch mode. Caller must call EndBatchMode() | 32 virtual void EndBatchMode() OVERRIDE; |
| 70 // to finish the utility process. | 33 virtual void SetExposedDir(const FilePath& dir) OVERRIDE; |
| 71 bool StartBatchMode(); | 34 virtual void DisableSandbox() OVERRIDE; |
| 72 | 35 virtual void EnableZygote() OVERRIDE; |
| 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) | 36 #if defined(OS_POSIX) |
| 81 void set_env(const base::environment_vector& env) { env_ = env; } | 37 virtual void SetEnv(const base::environment_vector& env) OVERRIDE; |
| 82 #endif | 38 #endif |
| 83 | 39 |
| 84 protected: | 40 void set_child_flags(int flags) { child_flags_ = flags; } |
| 85 // Allow these methods to be overridden for tests. | |
| 86 virtual FilePath GetUtilityProcessCmd(); | |
| 87 | 41 |
| 88 private: | 42 private: |
| 89 // Starts a process if necessary. Returns true if it succeeded or a process | 43 // Starts a process if necessary. Returns true if it succeeded or a process |
| 90 // has already been started via StartBatchMode(). | 44 // has already been started via StartBatchMode(). |
| 91 bool StartProcess(); | 45 bool StartProcess(); |
| 92 | 46 |
| 93 // BrowserChildProcessHost: | 47 // BrowserChildProcessHost: |
| 94 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 48 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 95 virtual void OnProcessCrashed(int exit_code) OVERRIDE; | 49 virtual void OnProcessCrashed(int exit_code) OVERRIDE; |
| 96 | 50 |
| 97 // A pointer to our client interface, who will be informed of progress. | 51 // A pointer to our client interface, who will be informed of progress. |
| 98 scoped_refptr<Client> client_; | 52 scoped_refptr<content::UtilityProcessHostClient> client_; |
| 99 content::BrowserThread::ID client_thread_id_; | 53 content::BrowserThread::ID client_thread_id_; |
| 100 // True when running in batch mode, i.e., StartBatchMode() has been called | 54 // True when running in batch mode, i.e., StartBatchMode() has been called |
| 101 // and the utility process will run until EndBatchMode(). | 55 // and the utility process will run until EndBatchMode(). |
| 102 bool is_batch_mode_; | 56 bool is_batch_mode_; |
| 103 | 57 |
| 104 // Allows a directory to be opened through the sandbox, in case it's needed by | |
| 105 // the operation. | |
| 106 FilePath exposed_dir_; | 58 FilePath exposed_dir_; |
| 107 | 59 |
| 108 // Whether to pass switches::kNoSandbox to the child. | 60 // Whether to pass switches::kNoSandbox to the child. |
| 109 bool no_sandbox_; | 61 bool no_sandbox_; |
| 110 | 62 |
| 111 // Flags defined in ChildProcessHost with which to start the process. | 63 // Flags defined in ChildProcessHost with which to start the process. |
| 112 int child_flags_; | 64 int child_flags_; |
| 113 | 65 |
| 114 // If the |no_sandbox_| flag is off, and we are on Linux, launch the | 66 // Launch the utility process from the zygote. Defaults to false. |
| 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_; | 67 bool use_linux_zygote_; |
| 118 | 68 |
| 119 base::environment_vector env_; | 69 base::environment_vector env_; |
| 120 | 70 |
| 121 bool started_; | 71 bool started_; |
| 122 | 72 |
| 123 scoped_ptr<BrowserChildProcessHostImpl> process_; | 73 scoped_ptr<BrowserChildProcessHostImpl> process_; |
| 124 | 74 |
| 125 DISALLOW_COPY_AND_ASSIGN(UtilityProcessHost); | 75 DISALLOW_COPY_AND_ASSIGN(UtilityProcessHostImpl); |
| 126 }; | 76 }; |
| 127 | 77 |
| 128 #endif // CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_ | 78 #endif // CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_ |
| OLD | NEW |