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