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.
5 #include "xwalk/application/browser/linux/running_applications_manager.h"
10 #include "dbus/message.h"
12 #include "xwalk/application/browser/linux/running_application_object.h"
16 // D-Bus Interface implemented by the manager object of running applications.
20 // Launch(string app_id) -> ObjectPath
21 // Launches the application with 'app_id'.
22 const char kRunningManagerDBusInterface[] =
23 "org.crosswalkproject.Running.Manager1";
25 const char kRunningManagerDBusError[] =
26 "org.crosswalkproject.Running.Manager.Error";
28 const dbus::ObjectPath kRunningManagerDBusPath("/running1");
34 namespace application {
36 dbus::ObjectPath GetRunningPathForAppID(const std::string& app_id) {
37 return dbus::ObjectPath(kRunningManagerDBusPath.value() + "/" + app_id);
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);
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()));
55 RunningApplicationsManager::~RunningApplicationsManager() {}
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>();
69 void RunningApplicationsManager::OnLaunch(
70 dbus::MethodCall* method_call,
71 dbus::ExportedObject::ResponseSender response_sender) {
73 dbus::MessageReader reader(method_call);
75 // We might want to pass key-value pairs if have more parameters in future.
76 unsigned int launcher_pid;
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());
89 Application::LaunchParams params;
90 params.launcher_pid = launcher_pid;
92 params.window_state = ui::SHOW_STATE_FULLSCREEN;
94 Application* application = application_service_->Launch(app_id, params);
96 scoped_ptr<dbus::Response> response =
97 CreateError(method_call,
98 "Error launching application with id " + app_id);
99 response_sender.Run(response.Pass());
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(),
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());
116 void RunningApplicationsManager::OnExported(
117 const std::string& interface_name,
118 const std::string& method_name,
121 LOG(WARNING) << "Error exporting method '" << interface_name
122 << "." << method_name << "' in '"
123 << kRunningManagerDBusPath.value() << "'.";
127 void RunningApplicationsManager::WillDestroyApplication(Application* app) {
128 dbus::ObjectPath path = GetRunningPathForAppID(app->id());
130 adaptor_.RemoveManagedObject(path);
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));
140 dbus::ObjectPath path = running_application->path();
142 adaptor_.AddManagedObject(running_application.PassAs<dbus::ManagedObject>());
147 } // namespace application