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 MultiProcessLock; 18 19#if defined(OS_MACOSX) 20#ifdef __OBJC__ 21@class NSString; 22#else 23class NSString; 24#endif 25#endif 26 27namespace base { 28class CommandLine; 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// Creates command-line to run the service process. 78scoped_ptr<base::CommandLine> CreateServiceProcessCommandLine(); 79 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. 84class ServiceProcessState { 85 public: 86 ServiceProcessState(); 87 ~ServiceProcessState(); 88 89 // Tries to become the sole service process for the current user data dir. 90 // Returns false if another service process is already running. 91 bool Initialize(); 92 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. 99 bool SignalReady( 100 base::MessageLoopProxy* message_loop_proxy, 101 const base::Closure& terminate_task); 102 103 // Signal that the service process is stopped. 104 void SignalStopped(); 105 106 // Register the service process to run on startup. 107 bool AddToAutoRun(); 108 109 // Unregister the service process to run on startup. 110 bool RemoveFromAutoRun(); 111 112 // Return the channel handle used for communicating with the service. 113 IPC::ChannelHandle GetServiceProcessChannel(); 114 115 private: 116#if !defined(OS_MACOSX) 117 // Create the shared memory data for the service process. 118 bool CreateSharedData(); 119 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(); 123 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(); 127#endif // !OS_MACOSX 128 129 // Creates the platform specific state. 130 void CreateState(); 131 132 // Tear down the platform specific state. 133 void TearDownState(); 134 135 // An opaque object that maintains state. The actual definition of this is 136 // platform dependent. 137 struct StateData; 138 StateData* state_; 139 scoped_ptr<base::SharedMemory> shared_mem_service_data_; 140 scoped_ptr<base::CommandLine> autorun_command_line_; 141}; 142 143#endif // CHROME_COMMON_SERVICE_PROCESS_UTIL_H_ 144