summaryrefslogtreecommitdiffstats
path: root/mojo/dbus/dbus_external_service.cc
blob: 3a13cf7b521c336187a151ae4e1d1f6206158967 (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
// Copyright 2014 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.

#include "mojo/dbus/dbus_external_service.h"

#include "base/bind.h"
#include "base/callback.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop_proxy.h"
#include "dbus/bus.h"
#include "dbus/exported_object.h"
#include "dbus/file_descriptor.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "mojo/common/channel_init.h"
#include "mojo/public/cpp/bindings/error_handler.h"
#include "mojo/public/cpp/bindings/remote_ptr.h"
#include "mojo/public/cpp/shell/application.h"
#include "mojo/public/interfaces/shell/shell.mojom.h"
#include "mojo/shell/external_service.mojom.h"

namespace mojo {

DBusExternalServiceBase::DBusExternalServiceBase(
    const std::string& service_name)
    : service_name_(service_name),
      exported_object_(NULL) {
}
DBusExternalServiceBase::~DBusExternalServiceBase() {}

void DBusExternalServiceBase::Start() {
  InitializeDBus();
  ExportMethods();
  TakeDBusServiceOwnership();
  DVLOG(1) << "External service started";
}

void DBusExternalServiceBase::OnError() {
  Disconnect();
}

void DBusExternalServiceBase::ConnectChannel(
    dbus::MethodCall* method_call,
    dbus::ExportedObject::ResponseSender sender) {
  dbus::MessageReader reader(method_call);
  dbus::FileDescriptor wrapped_fd;
  if (!reader.PopFileDescriptor(&wrapped_fd)) {
    sender.Run(
        dbus::ErrorResponse::FromMethodCall(
            method_call,
            "org.chromium.Mojo.BadHandle",
            "Invalid FD.").PassAs<dbus::Response>());
    return;
  }
  wrapped_fd.CheckValidity();
  channel_init_.reset(new mojo::common::ChannelInit);
  mojo::ScopedMessagePipeHandle message_pipe =
      channel_init_->Init(wrapped_fd.TakeValue(),
                          base::MessageLoopProxy::current());
  CHECK(message_pipe.is_valid());

  Connect(mojo::ScopedExternalServiceHostHandle::From(message_pipe.Pass()));
  sender.Run(dbus::Response::FromMethodCall(method_call));
}

void DBusExternalServiceBase::ExportMethods() {
  CHECK(exported_object_);
  CHECK(exported_object_->ExportMethodAndBlock(
      kMojoDBusInterface, kMojoDBusConnectMethod,
      base::Bind(&DBusExternalServiceBase::ConnectChannel,
                 base::Unretained(this))));
}

void DBusExternalServiceBase::InitializeDBus() {
  CHECK(!bus_);
  dbus::Bus::Options options;
  options.bus_type = dbus::Bus::SESSION;
  bus_ = new dbus::Bus(options);
  CHECK(bus_->Connect());
  CHECK(bus_->SetUpAsyncOperations());

  exported_object_ =
      bus_->GetExportedObject(dbus::ObjectPath(kMojoDBusImplPath));
}

void DBusExternalServiceBase::TakeDBusServiceOwnership() {
  CHECK(bus_->RequestOwnershipAndBlock(
      service_name_,
      dbus::Bus::REQUIRE_PRIMARY_ALLOW_REPLACEMENT))
      << "Unable to take ownership of " << service_name_;
}

}  // namespace mojo