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 MessageLoopProxy
;
32 // Return the IPC channel to connect to the service process.
33 IPC::ChannelHandle
GetServiceProcessChannel();
35 #if !defined(OS_MACOSX)
36 // Return a name that is scoped to this instance of the service process. We
37 // use the user-data-dir as a scoping prefix.
38 std::string
GetServiceProcessScopedName(const std::string
& append_str
);
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
);
45 #if defined(OS_MACOSX)
46 // Return the name that is used to extract the socket path out of the
47 // dictionary provided by launchd.
48 NSString
* GetServiceProcessLaunchDSocketEnvVar();
52 // Attempts to take a lock named |name|. If |waiting| is true then this will
53 // make multiple attempts to acquire the lock.
54 // Caller is responsible for ownership of the MultiProcessLock.
55 MultiProcessLock
* TakeNamedLock(const std::string
& name
, bool waiting
);
58 // The following methods are used in a process that acts as a client to the
59 // service process (typically the browser process).
60 // --------------------------------------------------------------------------
61 // This method checks that if the service process is ready to receive
63 bool CheckServiceProcessReady();
65 // Returns the process id and version of the currently running service process.
66 // Note: DO NOT use this check whether the service process is ready because
67 // a true return value only means that some process shared data was available,
68 // and not that the process is ready to receive IPC commands, or even running.
69 // This method is only exposed for testing.
70 bool GetServiceProcessData(std::string
* version
, base::ProcessId
* pid
);
71 // --------------------------------------------------------------------------
73 // Forces a service process matching the specified version to shut down.
74 bool ForceServiceProcessShutdown(const std::string
& version
,
75 base::ProcessId process_id
);
77 // Creates command-line to run the service process.
78 scoped_ptr
<base::CommandLine
> CreateServiceProcessCommandLine();
80 // This is a class that is used by the service process to signal events and
81 // share data with external clients. This class lives in this file because the
82 // internal data structures and mechanisms used by the utility methods above
83 // and this class are shared.
84 class ServiceProcessState
{
86 ServiceProcessState();
87 ~ServiceProcessState();
89 // Tries to become the sole service process for the current user data dir.
90 // Returns false if another service process is already running.
93 // Signal that the service process is ready.
94 // This method is called when the service process is running and initialized.
95 // |terminate_task| is invoked when we get a terminate request from another
96 // process (in the same thread that called SignalReady). It can be NULL.
97 // |message_loop_proxy| must be of type IO and is the loop that POSIX uses
98 // to monitor the service process.
100 base::MessageLoopProxy
* message_loop_proxy
,
101 const base::Closure
& terminate_task
);
103 // Signal that the service process is stopped.
104 void SignalStopped();
106 // Register the service process to run on startup.
109 // Unregister the service process to run on startup.
110 bool RemoveFromAutoRun();
112 // Return the channel handle used for communicating with the service.
113 IPC::ChannelHandle
GetServiceProcessChannel();
116 #if !defined(OS_MACOSX)
117 // Create the shared memory data for the service process.
118 bool CreateSharedData();
120 // If an older version of the service process running, it should be shutdown.
121 // Returns false if this process needs to exit.
122 bool HandleOtherVersion();
124 // Acquires a singleton lock for the service process. A return value of false
125 // means that a service process instance is already running.
126 bool TakeSingletonLock();
129 // Creates the platform specific state.
132 // Tear down the platform specific state.
133 void TearDownState();
135 // An opaque object that maintains state. The actual definition of this is
136 // platform dependent.
139 scoped_ptr
<base::SharedMemory
> shared_mem_service_data_
;
140 scoped_ptr
<base::CommandLine
> autorun_command_line_
;
143 #endif // CHROME_COMMON_SERVICE_PROCESS_UTIL_H_