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.
4
5#ifndef CHROME_COMMON_SERVICE_PROCESS_UTIL_H_
6#define CHROME_COMMON_SERVICE_PROCESS_UTIL_H_
7
8#include <string>
9
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"
16
17class CommandLine;
18class MultiProcessLock;
19
20#if defined(OS_MACOSX)
21#ifdef __OBJC__
22@class NSString;
23#else
24class NSString;
25#endif
26#endif
27
28namespace base {
29class MessageLoopProxy;
30}
31
32// Return the IPC channel to connect to the service process.
33IPC::ChannelHandle GetServiceProcessChannel();
34
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.
38std::string GetServiceProcessScopedName(const std::string& append_str);
39
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.
42std::string GetServiceProcessScopedVersionedName(const std::string& append_str);
43#endif  // OS_MACOSX
44
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.
48NSString* GetServiceProcessLaunchDSocketEnvVar();
49#endif
50
51#if defined(OS_POSIX)
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.
55MultiProcessLock* TakeNamedLock(const std::string& name, bool waiting);
56#endif
57
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
62// IPC commands.
63bool CheckServiceProcessReady();
64
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.
70bool GetServiceProcessData(std::string* version, base::ProcessId* pid);
71// --------------------------------------------------------------------------
72
73// Forces a service process matching the specified version to shut down.
74bool ForceServiceProcessShutdown(const std::string& version,
75                                 base::ProcessId process_id);
76
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.
81class ServiceProcessState {
82 public:
83  ServiceProcessState();
84  ~ServiceProcessState();
85
86  // Tries to become the sole service process for the current user data dir.
87  // Returns false if another service process is already running.
88  bool Initialize();
89
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.
96  bool SignalReady(
97      base::MessageLoopProxy* message_loop_proxy,
98      const base::Closure& terminate_task);
99
100  // Signal that the service process is stopped.
101  void SignalStopped();
102
103  // Register the service process to run on startup.
104  bool AddToAutoRun();
105
106  // Unregister the service process to run on startup.
107  bool RemoveFromAutoRun();
108
109  // Return the channel handle used for communicating with the service.
110  IPC::ChannelHandle GetServiceProcessChannel();
111
112 private:
113#if !defined(OS_MACOSX)
114  // Create the shared memory data for the service process.
115  bool CreateSharedData();
116
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();
120
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();
124#endif  // !OS_MACOSX
125
126  // Creates the platform specific state.
127  void CreateState();
128
129  // Tear down the platform specific state.
130  void TearDownState();
131
132  // Initializes the command-line that can be used to autorun the service
133  // process.
134  void CreateAutoRunCommandLine();
135
136  // An opaque object that maintains state. The actual definition of this is
137  // platform dependent.
138  struct StateData;
139  StateData* state_;
140  scoped_ptr<base::SharedMemory> shared_mem_service_data_;
141  scoped_ptr<CommandLine> autorun_command_line_;
142};
143
144#endif  // CHROME_COMMON_SERVICE_PROCESS_UTIL_H_
145