Upstream version 5.34.104.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/installed_applications_manager.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     ApplicationStorage* app_storage,
22     scoped_refptr<dbus::Bus> session_bus)
23     : session_bus_(session_bus) {
24   installed_apps_.reset(new InstalledApplicationsManager(session_bus_,
25                                                          app_service,
26                                                          app_storage));
27   running_apps_.reset(new RunningApplicationsManager(session_bus_,
28                                                      app_service));
29
30   // TODO(cmarcelo): This is just a placeholder to test D-Bus is working, remove
31   // once we exported proper objects.
32   ExportTestObject();
33
34   // Auto activation waits for the service name to be registered, so we do this
35   // as the last step so all the object paths and interfaces are set.
36   session_bus_->RequestOwnership(
37       kXWalkDBusServiceName,
38       dbus::Bus::REQUIRE_PRIMARY,
39       base::Bind(&ApplicationServiceProviderLinux::OnServiceNameExported,
40                  base::Unretained(this)));
41 }
42
43 ApplicationServiceProviderLinux::~ApplicationServiceProviderLinux() {}
44
45 void ApplicationServiceProviderLinux::OnServiceNameExported(
46     const std::string& service_name, bool success) {
47   if (!success) {
48     LOG(ERROR) << "Couldn't own D-Bus service name: " << service_name;
49     return;
50   }
51   VLOG(1) << "D-Bus service name exported.";
52 }
53
54 namespace {
55
56 void OnExported(const std::string& interface_name,
57                 const std::string& method_name,
58                 bool success) {
59   if (!success) {
60     LOG(WARNING) << "Couldn't export method '" + method_name
61                  << "' from '" + interface_name + "'.";
62   }
63 }
64
65 void OnPing(dbus::MethodCall* method_call,
66             dbus::ExportedObject::ResponseSender response_sender) {
67   scoped_ptr<dbus::Response> response =
68       dbus::Response::FromMethodCall(method_call);
69   dbus::MessageWriter writer(response.get());
70   writer.AppendString("Pong");
71   response_sender.Run(response.Pass());
72 }
73
74 }  // namespace
75
76 void ApplicationServiceProviderLinux::ExportTestObject() {
77   dbus::ExportedObject* object =
78       session_bus_->GetExportedObject(dbus::ObjectPath("/test"));
79   object->ExportMethod("org.crosswalkproject.TestInterface1", "Ping",
80                        base::Bind(&OnPing), base::Bind(&OnExported));
81 }
82
83 }  // namespace application
84 }  // namespace xwalk