• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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/memory/scoped_ptr.h"
12 #include "base/process.h"
13 #include "base/shared_memory.h"
14 #include "ipc/ipc_channel_handle.h"
15 
16 class Task;
17 class CommandLine;
18 
19 namespace base {
20   class MessageLoopProxy;
21 }
22 
23 // Return the IPC channel to connect to the service process.
24 IPC::ChannelHandle GetServiceProcessChannel();
25 
26 #if !defined(OS_MACOSX)
27 // Return a name that is scoped to this instance of the service process. We
28 // use the user-data-dir as a scoping prefix.
29 std::string GetServiceProcessScopedName(const std::string& append_str);
30 
31 // Return a name that is scoped to this instance of the service process. We
32 // use the user-data-dir and the version as a scoping prefix.
33 std::string GetServiceProcessScopedVersionedName(const std::string& append_str);
34 #endif  // OS_MACOSX
35 
36 // The following methods are used in a process that acts as a client to the
37 // service process (typically the browser process).
38 // --------------------------------------------------------------------------
39 // This method checks that if the service process is ready to receive
40 // IPC commands.
41 bool CheckServiceProcessReady();
42 
43 // Returns the process id and version of the currently running service process.
44 // Note: DO NOT use this check whether the service process is ready because
45 // a true return value only means that some process shared data was available,
46 // and not that the process is ready to receive IPC commands, or even running.
47 // This method is only exposed for testing.
48 bool GetServiceProcessData(std::string* version, base::ProcessId* pid);
49 // --------------------------------------------------------------------------
50 
51 // Forces a service process matching the specified version to shut down.
52 bool ForceServiceProcessShutdown(const std::string& version,
53                                  base::ProcessId process_id);
54 
55 // This is a class that is used by the service process to signal events and
56 // share data with external clients. This class lives in this file because the
57 // internal data structures and mechanisms used by the utility methods above
58 // and this class are shared.
59 class ServiceProcessState {
60  public:
61   ServiceProcessState();
62   ~ServiceProcessState();
63 
64   // Tries to become the sole service process for the current user data dir.
65   // Returns false if another service process is already running.
66   bool Initialize();
67 
68   // Signal that the service process is ready.
69   // This method is called when the service process is running and initialized.
70   // |shutdown_task| is invoked when we get a shutdown request from another
71   // process (in the same thread that called SignalReady). It can be NULL.
72   // |message_loop_proxy| must be of type IO and is the loop that POSIX uses
73   // to monitor the service process.
74   bool SignalReady(
75       base::MessageLoopProxy* message_loop_proxy, Task* shutdown_task);
76 
77   // Signal that the service process is stopped.
78   void SignalStopped();
79 
80   // Register the service process to run on startup.
81   bool AddToAutoRun();
82 
83   // Unregister the service process to run on startup.
84   bool RemoveFromAutoRun();
85 
86   // Return the channel handle used for communicating with the service.
87   IPC::ChannelHandle GetServiceProcessChannel();
88 
89  private:
90 
91 #if !defined(OS_MACOSX)
92   // Create the shared memory data for the service process.
93   bool CreateSharedData();
94 
95   // If an older version of the service process running, it should be shutdown.
96   // Returns false if this process needs to exit.
97   bool HandleOtherVersion();
98 
99   // Acquires a singleton lock for the service process. A return value of false
100   // means that a service process instance is already running.
101   bool TakeSingletonLock();
102 #endif  // !OS_MACOSX
103 
104   // Creates the platform specific state.
105   void CreateState();
106 
107   // Tear down the platform specific state.
108   void TearDownState();
109 
110   // Initializes the command-line that can be used to autorun the service
111   // process.
112   void CreateAutoRunCommandLine();
113 
114   // An opaque object that maintains state. The actual definition of this is
115   // platform dependent.
116   struct StateData;
117   StateData* state_;
118   scoped_ptr<base::SharedMemory> shared_mem_service_data_;
119   scoped_ptr<CommandLine> autorun_command_line_;
120 };
121 
122 #endif  // CHROME_COMMON_SERVICE_PROCESS_UTIL_H_
123