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_BROWSER_CHROMEOS_DBUS_LIVENESS_SERVICE_PROVIDER_H_
6#define CHROME_BROWSER_CHROMEOS_DBUS_LIVENESS_SERVICE_PROVIDER_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/compiler_specific.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/weak_ptr.h"
14#include "chrome/browser/chromeos/dbus/cros_dbus_service.h"
15#include "dbus/exported_object.h"
16
17namespace dbus {
18class MethodCall;
19class Response;
20}
21
22namespace chromeos {
23
24// This class exports a "CheckLiveness" D-Bus method that the session manager
25// calls periodically to confirm that Chrome's UI thread is responsive to D-Bus
26// messages.  It can be tested with the following command:
27//
28// % dbus-send --system --type=method_call --print-reply
29//     --dest=org.chromium.LibCrosService
30//     /org/chromium/LibCrosService
31//     org.chromium.LibCrosServiceInterface.CheckLiveness
32//
33// -> method return sender=:1.9 -> dest=:1.27 reply_serial=2
34//
35// (An empty response should be returned.)
36class LivenessServiceProvider
37    : public CrosDBusService::ServiceProviderInterface {
38 public:
39  LivenessServiceProvider();
40  virtual ~LivenessServiceProvider();
41
42  // CrosDBusService::ServiceProviderInterface overrides:
43  virtual void Start(
44      scoped_refptr<dbus::ExportedObject> exported_object) OVERRIDE;
45
46 private:
47  // Called from ExportedObject when CheckLiveness() is exported as a D-Bus
48  // method or failed to be exported.
49  void OnExported(const std::string& interface_name,
50                  const std::string& method_name,
51                  bool success);
52
53  // Called on UI thread in response to a D-Bus request.
54  void CheckLiveness(dbus::MethodCall* method_call,
55                     dbus::ExportedObject::ResponseSender response_sender);
56
57  // Keep this last so that all weak pointers will be invalidated at the
58  // beginning of destruction.
59  base::WeakPtrFactory<LivenessServiceProvider> weak_ptr_factory_;
60
61  DISALLOW_COPY_AND_ASSIGN(LivenessServiceProvider);
62};
63
64}  // namespace chromeos
65
66#endif  // CHROME_BROWSER_CHROMEOS_DBUS_LIVENESS_SERVICE_PROVIDER_H_
67