Upstream version 8.36.169.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / application_service_provider_linux.cc
1 // Copyright (c) 2013 Intel Corporation. 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 #include "xwalk/application/browser/application_service_provider_linux.h"
6
7 #include <string>
8 #include "base/bind.h"
9 #include "dbus/bus.h"
10 #include "dbus/exported_object.h"
11 #include "dbus/message.h"
12 #include "xwalk/dbus/xwalk_service_name.h"
13 #include "xwalk/application/browser/linux/running_application_object.h"
14 #include "xwalk/application/browser/linux/running_applications_manager.h"
15
16 namespace xwalk {
17 namespace application {
18
19 ApplicationServiceProviderLinux::ApplicationServiceProviderLinux(
20     ApplicationService* app_service,
21     scoped_refptr<dbus::Bus> session_bus)
22     : session_bus_(session_bus) {
23   running_apps_.reset(new RunningApplicationsManager(session_bus_,
24                                                      app_service));
25
26   // TODO(cmarcelo): This is just a placeholder to test D-Bus is working, remove
27   // once we exported proper objects.
28   ExportTestObject();
29
30   // Auto activation waits for the service name to be registered, so we do this
31   // as the last step so all the object paths and interfaces are set.
32   session_bus_->RequestOwnership(
33       kXWalkDBusServiceName,
34       dbus::Bus::REQUIRE_PRIMARY,
35       base::Bind(&ApplicationServiceProviderLinux::OnServiceNameExported,
36                  base::Unretained(this)));
37 }
38
39 ApplicationServiceProviderLinux::~ApplicationServiceProviderLinux() {}
40
41 RunningApplicationObject*
42 ApplicationServiceProviderLinux::GetRunningApplicationObject(
43     const Application* app) {
44   return running_apps_->GetRunningApp(app->id());
45 }
46
47 void ApplicationServiceProviderLinux::OnServiceNameExported(
48     const std::string& service_name, bool success) {
49   if (!success) {
50     LOG(ERROR) << "Couldn't own D-Bus service name: " << service_name;
51     return;
52   }
53   VLOG(1) << "D-Bus service name exported.";
54 }
55
56 namespace {
57
58 void OnExported(const std::string& interface_name,
59                 const std::string& method_name,
60                 bool success) {
61   if (!success) {
62     LOG(WARNING) << "Couldn't export method '" + method_name
63                  << "' from '" + interface_name + "'.";
64   }
65 }
66
67 void OnPing(dbus::MethodCall* method_call,
68             dbus::ExportedObject::ResponseSender response_sender) {
69   scoped_ptr<dbus::Response> response =
70       dbus::Response::FromMethodCall(method_call);
71   dbus::MessageWriter writer(response.get());
72   writer.AppendString("Pong");
73   response_sender.Run(response.Pass());
74 }
75
76 }  // namespace
77
78 void ApplicationServiceProviderLinux::ExportTestObject() {
79   dbus::ExportedObject* object =
80       session_bus_->GetExportedObject(dbus::ObjectPath("/test"));
81   object->ExportMethod("org.crosswalkproject.TestInterface1", "Ping",
82                        base::Bind(&OnPing), base::Bind(&OnExported));
83 }
84
85 }  // namespace application
86 }  // namespace xwalk