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 // This is a class that is used by the service process to signal events and
78 // share data with external clients. This class lives in this file because the
79 // internal data structures and mechanisms used by the utility methods above
80 // and this class are shared.
81 class ServiceProcessState
{
83 ServiceProcessState();
84 ~ServiceProcessState();
86 // Tries to become the sole service process for the current user data dir.
87 // Returns false if another service process is already running.
90 // Signal that the service process is ready.
91 // This method is called when the service process is running and initialized.
92 // |terminate_task| is invoked when we get a terminate request from another
93 // process (in the same thread that called SignalReady). It can be NULL.
94 // |message_loop_proxy| must be of type IO and is the loop that POSIX uses
95 // to monitor the service process.
97 base::MessageLoopProxy
* message_loop_proxy
,
98 const base::Closure
& terminate_task
);
100 // Signal that the service process is stopped.
101 void SignalStopped();
103 // Register the service process to run on startup.
106 // Unregister the service process to run on startup.
107 bool RemoveFromAutoRun();
109 // Return the channel handle used for communicating with the service.
110 IPC::ChannelHandle
GetServiceProcessChannel();
113 #if !defined(OS_MACOSX)
114 // Create the shared memory data for the service process.
115 bool CreateSharedData();
117 // If an older version of the service process running, it should be shutdown.
118 // Returns false if this process needs to exit.
119 bool HandleOtherVersion();
121 // Acquires a singleton lock for the service process. A return value of false
122 // means that a service process instance is already running.
123 bool TakeSingletonLock();
126 // Creates the platform specific state.
129 // Tear down the platform specific state.
130 void TearDownState();
132 // Initializes the command-line that can be used to autorun the service
134 void CreateAutoRunCommandLine();
136 // An opaque object that maintains state. The actual definition of this is
137 // platform dependent.
140 scoped_ptr
<base::SharedMemory
> shared_mem_service_data_
;
141 scoped_ptr
<base::CommandLine
> autorun_command_line_
;
144 #endif // CHROME_COMMON_SERVICE_PROCESS_UTIL_H_