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_SERVICE_SERVICE_IPC_SERVER_H_
6#define CHROME_SERVICE_SERVICE_IPC_SERVER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/memory/scoped_ptr.h"
12#include "ipc/ipc_channel_handle.h"
13#include "ipc/ipc_listener.h"
14#include "ipc/ipc_sync_channel.h"
15#include "ipc/ipc_sync_message_filter.h"
16#include "ipc/ipc_sender.h"
17
18namespace base {
19
20class DictionaryValue;
21
22}  // namespace base
23
24// This class handles IPC commands for the service process.
25class ServiceIPCServer : public IPC::Listener, public IPC::Sender {
26 public:
27  explicit ServiceIPCServer(const IPC::ChannelHandle& handle);
28  virtual ~ServiceIPCServer();
29
30  bool Init();
31
32  // IPC::Sender implementation.
33  virtual bool Send(IPC::Message* msg) OVERRIDE;
34
35  IPC::SyncChannel* channel() { return channel_.get(); }
36
37  // Safe to call on any thread, as long as it's guaranteed that the thread's
38  // lifetime is less than the main thread.
39  IPC::SyncMessageFilter* sync_message_filter() {
40    return sync_message_filter_.get();
41  }
42
43  bool is_client_connected() const { return client_connected_; }
44
45
46 private:
47  friend class MockServiceIPCServer;
48
49  // IPC::Listener implementation.
50  virtual bool OnMessageReceived(const IPC::Message& msg) OVERRIDE;
51  virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
52  virtual void OnChannelError() OVERRIDE;
53
54  // IPC message handlers.
55  void OnEnableCloudPrintProxyWithRobot(
56      const std::string& robot_auth_code,
57      const std::string& robot_email,
58      const std::string& user_email,
59      const base::DictionaryValue& user_settings);
60  void OnGetCloudPrintProxyInfo();
61  void OnDisableCloudPrintProxy();
62
63  void OnShutdown();
64  void OnUpdateAvailable();
65
66  // Helper method to create the sync channel.
67  void CreateChannel();
68
69  IPC::ChannelHandle channel_handle_;
70  scoped_ptr<IPC::SyncChannel> channel_;
71  // Indicates whether a client is currently connected to the channel.
72  bool client_connected_;
73
74  // Allows threads other than the main thread to send sync messages.
75  scoped_refptr<IPC::SyncMessageFilter> sync_message_filter_;
76
77
78  DISALLOW_COPY_AND_ASSIGN(ServiceIPCServer);
79};
80
81#endif  // CHROME_SERVICE_SERVICE_IPC_SERVER_H_
82