Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / xwalk / application / browser / linux / running_applications_manager.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/linux/running_applications_manager.h"
6
7 #include <string>
8 #include "base/bind.h"
9 #include "dbus/bus.h"
10 #include "dbus/message.h"
11
12 #include "xwalk/application/browser/linux/running_application_object.h"
13
14 namespace {
15
16 // D-Bus Interface implemented by the manager object of running applications.
17 //
18 // Methods:
19 //
20 //   Launch(string app_id) -> ObjectPath
21 //     Launches the application with 'app_id'.
22 const char kRunningManagerDBusInterface[] =
23     "org.crosswalkproject.Running.Manager1";
24
25 const char kRunningManagerDBusError[] =
26     "org.crosswalkproject.Running.Manager.Error";
27
28 const dbus::ObjectPath kRunningManagerDBusPath("/running1");
29
30
31 }  // namespace
32
33 namespace xwalk {
34 namespace application {
35
36 dbus::ObjectPath GetRunningPathForAppID(const std::string& app_id) {
37   return dbus::ObjectPath(kRunningManagerDBusPath.value() + "/" + app_id);
38 }
39
40 RunningApplicationsManager::RunningApplicationsManager(
41     scoped_refptr<dbus::Bus> bus, ApplicationService* service)
42     : weak_factory_(this),
43       application_service_(service),
44       adaptor_(bus, kRunningManagerDBusPath) {
45   application_service_->AddObserver(this);
46
47   adaptor_.manager_object()->ExportMethod(
48       kRunningManagerDBusInterface, "Launch",
49       base::Bind(&RunningApplicationsManager::OnLaunch,
50                  weak_factory_.GetWeakPtr()),
51       base::Bind(&RunningApplicationsManager::OnExported,
52                  weak_factory_.GetWeakPtr()));
53 }
54
55 RunningApplicationsManager::~RunningApplicationsManager() {}
56
57 namespace {
58
59 scoped_ptr<dbus::Response> CreateError(dbus::MethodCall* method_call,
60                                        const std::string& message) {
61     scoped_ptr<dbus::ErrorResponse> error_response =
62         dbus::ErrorResponse::FromMethodCall(
63             method_call, kRunningManagerDBusError, message);
64     return error_response.PassAs<dbus::Response>();
65 }
66
67 }  // namespace
68
69 void RunningApplicationsManager::OnLaunch(
70     dbus::MethodCall* method_call,
71     dbus::ExportedObject::ResponseSender response_sender) {
72
73   dbus::MessageReader reader(method_call);
74   std::string app_id;
75   // We might want to pass key-value pairs if have more parameters in future.
76   unsigned int launcher_pid;
77   bool fullscreen;
78
79   if (!reader.PopString(&app_id) ||
80       !reader.PopUint32(&launcher_pid) ||
81       !reader.PopBool(&fullscreen)) {
82     scoped_ptr<dbus::Response> response =
83         CreateError(method_call,
84                     "Error parsing message. Missing arguments.");
85     response_sender.Run(response.Pass());
86     return;
87   }
88
89   Application::LaunchParams params;
90   params.launcher_pid = launcher_pid;
91   if (fullscreen)
92     params.window_state = ui::SHOW_STATE_FULLSCREEN;
93
94   Application* application = application_service_->Launch(app_id, params);
95   if (!application) {
96     scoped_ptr<dbus::Response> response =
97         CreateError(method_call,
98                     "Error launching application with id " + app_id);
99     response_sender.Run(response.Pass());
100     return;
101   }
102   CHECK(app_id == application->id());
103   // FIXME(cmarcelo): ApplicationService will tell us when new applications
104   // appear (with DidLaunchApplication()) and we create new managed objects
105   // in D-Bus based on that.
106   dbus::ObjectPath path = AddObject(app_id, method_call->GetSender(),
107                                     application);
108
109   scoped_ptr<dbus::Response> response =
110       dbus::Response::FromMethodCall(method_call);
111   dbus::MessageWriter writer(response.get());
112   writer.AppendObjectPath(path);
113   response_sender.Run(response.Pass());
114 }
115
116 void RunningApplicationsManager::OnExported(
117     const std::string& interface_name,
118     const std::string& method_name,
119     bool success) {
120   if (!success) {
121     LOG(WARNING) << "Error exporting method '" << interface_name
122                  << "." << method_name << "' in '"
123                  << kRunningManagerDBusPath.value() << "'.";
124   }
125 }
126
127 void RunningApplicationsManager::WillDestroyApplication(Application* app) {
128   dbus::ObjectPath path = GetRunningPathForAppID(app->id());
129
130   adaptor_.RemoveManagedObject(path);
131 }
132
133 dbus::ObjectPath RunningApplicationsManager::AddObject(
134     const std::string& app_id, const std::string& launcher_name,
135     Application* application) {
136   scoped_ptr<RunningApplicationObject> running_application(
137       new RunningApplicationObject(adaptor_.bus(), app_id,
138                                    launcher_name, application));
139
140   dbus::ObjectPath path = running_application->path();
141
142   adaptor_.AddManagedObject(running_application.PassAs<dbus::ManagedObject>());
143
144   return path;
145 }
146
147 }  // namespace application
148 }  // namespace xwalk