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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_
6 #define CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_
12 #include "base/basictypes.h"
13 #include "base/memory/ref_counted.h"
14 #include "content/browser/browser_child_process_host.h"
15 #include "content/browser/browser_thread.h"
17 // This class acts as the browser-side host to a utility child process. A
18 // utility process is a short-lived sandboxed process that is created to run
19 // a specific task. This class lives solely on the IO thread.
20 // If you need a single method call in the sandbox, use StartFooBar(p).
21 // If you need multiple batches of work to be done in the sandboxed process,
22 // use StartBatchMode(), then multiple calls to StartFooBar(p),
23 // then finish with EndBatchMode().
24 class UtilityProcessHost
: public BrowserChildProcessHost
{
26 // An interface to be implemented by consumers of the utility process to
27 // get results back. All functions are called on the thread passed along
28 // to UtilityProcessHost.
29 class Client
: public base::RefCountedThreadSafe
<Client
> {
33 // Called when the process has crashed.
34 virtual void OnProcessCrashed(int exit_code
);
36 // Allow the client to filter IPC messages.
37 virtual bool OnMessageReceived(const IPC::Message
& message
);
40 friend class base::RefCountedThreadSafe
<Client
>;
45 friend class UtilityProcessHost
;
47 DISALLOW_COPY_AND_ASSIGN(Client
);
50 UtilityProcessHost(Client
* client
, BrowserThread::ID client_thread_id
);
51 virtual ~UtilityProcessHost();
53 // BrowserChildProcessHost override
54 virtual bool Send(IPC::Message
* message
);
56 // Starts utility process in batch mode. Caller must call EndBatchMode()
57 // to finish the utility process.
58 bool StartBatchMode();
60 // Ends the utility process. Must be called after StartBatchMode().
63 void set_exposed_dir(const FilePath
& dir
) { exposed_dir_
= dir
; }
66 // Allow these methods to be overridden for tests.
67 virtual FilePath
GetUtilityProcessCmd();
70 // Starts a process if necessary. Returns true if it succeeded or a process
71 // has already been started via StartBatchMode().
75 virtual bool OnMessageReceived(const IPC::Message
& message
);
77 // BrowserChildProcessHost:
78 virtual void OnProcessCrashed(int exit_code
);
79 virtual bool CanShutdown();
81 // A pointer to our client interface, who will be informed of progress.
82 scoped_refptr
<Client
> client_
;
83 BrowserThread::ID client_thread_id_
;
84 // True when running in batch mode, i.e., StartBatchMode() has been called
85 // and the utility process will run until EndBatchMode().
88 // Allows a directory to be opened through the sandbox, in case it's needed by
90 FilePath exposed_dir_
;
94 DISALLOW_COPY_AND_ASSIGN(UtilityProcessHost
);
97 #endif // CONTENT_BROWSER_UTILITY_PROCESS_HOST_H_