summaryrefslogtreecommitdiffstats
path: root/chrome/browser/service/service_process_control.h
blob: 53f40dee288986ff811c4c6eba7d82ff2e0f407f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_SERVICE_SERVICE_PROCESS_CONTROL_H_
#define CHROME_BROWSER_SERVICE_SERVICE_PROCESS_CONTROL_H_

#include <queue>

#include "base/id_map.h"
#include "base/process.h"
#include "base/scoped_ptr.h"
#include "base/task.h"
#include "chrome/common/service_process_type.h"
#include "ipc/ipc_sync_channel.h"

class Profile;

// A ServiceProcessControl works as a portal between the service process and
// the browser process.
//
// It is used to start and terminate the service process. It is also used
// to send and receive IPC messages from the service process.
//
// THREADING
//
// This class is accessed on the UI thread through some UI actions. It then
// talks to the IPC channel on the IO thread.
class ServiceProcessControl : public IPC::Channel::Sender,
                              public IPC::Channel::Listener {
 public:
  typedef IDMap<ServiceProcessControl>::iterator iterator;
  typedef std::queue<IPC::Message> MessageQueue;

  // An interface for handling messages received from the service process.
  class MessageHandler {
   public:
    virtual ~MessageHandler() {}
    // This is a test signal sent from the service process. This can be used
    // the healthiness of the service.
    virtual void OnGoodDay() = 0;
  };

  // Construct a ServiceProcessControl with |profile| and a specific |type|.
  ServiceProcessControl(Profile* profile, ServiceProcessType type);
  virtual ~ServiceProcessControl();

  // Return the user profile associated with this service process.
  Profile* profile() const { return profile_; }

  // Return the type of this object.
  ServiceProcessType type() const { return type_; }

  // Return true if this object is connected to the service.
  bool is_connected() const { return channel_.get() != NULL; }

  // Create a new service process and connects to it.
  // |launch_done_task| is called if launching the service process has failed
  // or we have successfully launched the process and connected to it.
  // If the service process is already running this method will try to connect
  // to the service process.
  void Launch(Task* launch_done_task);

  // IPC::Channel::Listener implementation.
  virtual void OnMessageReceived(const IPC::Message& message);
  virtual void OnChannelConnected(int32 peer_pid);
  virtual void OnChannelError();

  // IPC::Channel::Sender implementation
  virtual bool Send(IPC::Message* message);

  // Send a hello message to the service process for testing purpose.
  // Return true if the message was sent.
  bool SendHello();

  // Send a shutdown message to the service process. IPC channel will be
  // destroyed after calling this method.
  // Return true if the message was sent.
  bool Shutdown();

  // Send a message to enable the remoting service in the service process.
  // Return true if the message was sent.
  bool EnableRemotingWithTokens(const std::string& user,
                                const std::string& remoting_token,
                                const std::string& talk_token);

  // Set the message handler for receiving messages from the service process.
  // TODO(hclam): Allow more than 1 handler.
  void SetMessageHandler(MessageHandler* message_handler) {
    message_handler_ = message_handler;
  }

 private:
  class Launcher;

  // Method called by Launcher when the service process is launched.
  void OnProcessLaunched(Task* launch_done_task);

  // Used internally to connect to the service process. |task| is executed
  // when the connection is made or an error occurred.
  void ConnectInternal(Task* task);

  Profile* profile_;
  ServiceProcessType type_;

  // IPC channel to the service process.
  scoped_ptr<IPC::SyncChannel> channel_;

  // Service process launcher.
  scoped_refptr<Launcher> launcher_;

  // Callback that gets invoked when the channel is connected or failed to
  // connect.
  scoped_ptr<Task> connect_done_task_;

  // Handler for messages from service process.
  MessageHandler* message_handler_;
};

#endif  // CHROME_BROWSER_SERVICE_SERVICE_PROCESS_H_