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.
5 #ifndef CHROME_COMMON_SERVICE_PROCESS_UTIL_H_
6 #define CHROME_COMMON_SERVICE_PROCESS_UTIL_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/shared_memory.h"
14 #include "base/process/process.h"
15 #include "ipc/ipc_channel_handle.h"
17 class MultiProcessLock
;
19 #if defined(OS_MACOSX)
29 class SingleThreadTaskRunner
;
32 // Return the IPC channel to connect to the service process.
33 IPC::ChannelHandle
GetServiceProcessChannel();
35 // Return a name that is scoped to this instance of the service process. We
36 // use the user-data-dir as a scoping prefix.
37 std::string
GetServiceProcessScopedName(const std::string
& append_str
);
39 #if !defined(OS_MACOSX)
40 // Return a name that is scoped to this instance of the service process. We
41 // use the user-data-dir and the version as a scoping prefix.
42 std::string
GetServiceProcessScopedVersionedName(const std::string
& append_str
);
46 // Attempts to take a lock named |name|. If |waiting| is true then this will
47 // make multiple attempts to acquire the lock.
48 // Caller is responsible for ownership of the MultiProcessLock.
49 MultiProcessLock
* TakeNamedLock(const std::string
& name
, bool waiting
);
52 // The following methods are used in a process that acts as a client to the
53 // service process (typically the browser process).
54 // --------------------------------------------------------------------------
55 // This method checks that if the service process is ready to receive
57 bool CheckServiceProcessReady();
59 // Returns the process id and version of the currently running service process.
60 // Note: DO NOT use this check whether the service process is ready because
61 // a true return value only means that some process shared data was available,
62 // and not that the process is ready to receive IPC commands, or even running.
63 // This method is only exposed for testing.
64 bool GetServiceProcessData(std::string
* version
, base::ProcessId
* pid
);
65 // --------------------------------------------------------------------------
67 // Forces a service process matching the specified version to shut down.
68 bool ForceServiceProcessShutdown(const std::string
& version
,
69 base::ProcessId process_id
);
71 // Creates command-line to run the service process.
72 scoped_ptr
<base::CommandLine
> CreateServiceProcessCommandLine();
74 // This is a class that is used by the service process to signal events and
75 // share data with external clients. This class lives in this file because the
76 // internal data structures and mechanisms used by the utility methods above
77 // and this class are shared.
78 class ServiceProcessState
{
80 ServiceProcessState();
81 ~ServiceProcessState();
83 // Tries to become the sole service process for the current user data dir.
84 // Returns false if another service process is already running.
87 // Signal that the service process is ready.
88 // This method is called when the service process is running and initialized.
89 // |terminate_task| is invoked when we get a terminate request from another
90 // process (in the same thread that called SignalReady). It can be NULL.
91 // |task_runner| must be of type IO and is the loop that POSIX uses
92 // to monitor the service process.
93 bool SignalReady(base::SingleThreadTaskRunner
* task_runner
,
94 const base::Closure
& terminate_task
);
96 // Signal that the service process is stopped.
99 // Register the service process to run on startup.
102 // Unregister the service process to run on startup.
103 bool RemoveFromAutoRun();
105 // Return the channel handle used for communicating with the service.
106 IPC::ChannelHandle
GetServiceProcessChannel();
109 #if !defined(OS_MACOSX)
110 // Create the shared memory data for the service process.
111 bool CreateSharedData();
113 // If an older version of the service process running, it should be shutdown.
114 // Returns false if this process needs to exit.
115 bool HandleOtherVersion();
117 // Acquires a singleton lock for the service process. A return value of false
118 // means that a service process instance is already running.
119 bool TakeSingletonLock();
122 // Creates the platform specific state.
125 // Tear down the platform specific state.
126 void TearDownState();
128 // An opaque object that maintains state. The actual definition of this is
129 // platform dependent.
132 scoped_ptr
<base::SharedMemory
> shared_mem_service_data_
;
133 scoped_ptr
<base::CommandLine
> autorun_command_line_
;
136 #endif // CHROME_COMMON_SERVICE_PROCESS_UTIL_H_