lomiri-location-service ..
An aggregating location service providing positioning and geocoding capabilities to applications.
service/skeleton.h
Go to the documentation of this file.
1/*
2 * Copyright © 2012-2013 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Thomas Voß <thomas.voss@canonical.com>
17 */
18#ifndef LOCATION_SERVICE_COM_LOMIRI_LOCATION_SERVICE_SKELETON_H_
19#define LOCATION_SERVICE_COM_LOMIRI_LOCATION_SERVICE_SKELETON_H_
20
24
25#include <core/dbus/dbus.h>
26#include <core/dbus/object.h>
27#include <core/dbus/property.h>
28#include <core/dbus/service_watcher.h>
29#include <core/dbus/skeleton.h>
30
31#include <core/dbus/interfaces/properties.h>
32
33#include <functional>
34
35namespace com
36{
37namespace lomiri
38{
39namespace location
40{
41namespace service
42{
44 : public core::dbus::Skeleton<com::lomiri::location::service::Interface>,
45 public std::enable_shared_from_this<Skeleton>
46{
47public:
48 typedef std::shared_ptr<Skeleton> Ptr;
49
50 // Models resolution of an incoming dbus message to the credentials of the message sender.
52 {
53 typedef std::shared_ptr<CredentialsResolver> Ptr;
54
56 virtual ~CredentialsResolver() = default;
57
58 // Resolves the sender of msg to the respective credentials.
59 virtual Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr& msg) = 0;
60 };
61
62 // Implements CredentialsResolver by reaching out to the dbus daemon and
63 // invoking:
64 // * GetConnectionUnixProcessID
65 // * GetConnectionUnixUser
67 {
68 // Functor for resolving a process id to an app-armor profile name.
69 typedef std::function<std::string(pid_t)> AppArmorProfileResolver;
70
71 // Returns an AppArmorProfileResolver leveraging libapparmor.
73
74 // Sets up a new instance for the given bus connection.
75 DBusDaemonCredentialsResolver(const core::dbus::Bus::Ptr& bus,
77
78 // Resolves the sender of msg to pid, uid by calling out to the dbus daemon.
79 Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr& msg);
80
81 // Stub for accessing the dbus daemon.
82 core::dbus::DBus daemon;
83
84 // Helper to resolve an application's pid to an app-armor profile name.
86 };
87
88 // Models the generation of stable and unique object paths for client-specific sessions.
89 // The requirements for the resulting object path are:
90 // * Unique for the entire system over its complete lifetime
91 // * Stable with respect to an app. That is, one app is always assigned the same object path.
93 {
94 typedef std::shared_ptr<ObjectPathGenerator> Ptr;
95
97 virtual ~ObjectPathGenerator() = default;
98
99 // Calculates an object path from pid and uid. The default implementation
100 // creates the path according to the following steps:
101 // [1.] Query the AppArmor profile name for pid in credentials.
102 // [1.1] If the process is running unconfined, rely on a counter to assemble the session name.
103 // [1.2] If the process is confined, use the AppArmor profile name to generate the path.
104 virtual core::dbus::types::ObjectPath object_path_for_caller_credentials(const Credentials& credentials);
105 };
106
108 {
109 // DBus connection set up for handling requests to the service.
110 core::dbus::Bus::Ptr incoming;
111 // DBus connection for reaching out to other services in a non-blocking way.
112 core::dbus::Bus::Ptr outgoing;
113 // An implementation of CredentialsResolver for resolving incoming message sender
114 // to Credentials = uid, pid.
116 // An implementation of ObjectPathGenerator for generating session names.
118 // Permission manager implementation for verifying incoming requests.
120 };
121
122 Skeleton(const Configuration& configuration);
123 ~Skeleton() noexcept;
124
125 // From com::lomiri::location::service::Interface
126 const core::Property<State>& state() const;
129 core::Property<bool>& is_online();
131 core::Property<std::vector<std::string>>& client_applications();
132
133protected:
134 // Enable subclasses to alter the state.
135 core::Property<State>& mutable_state();
136private:
137 // Handles incoming message calls for create_session_for_criteria.
138 // Dispatches to the actual implementation, and manages object lifetimes.
139 void handle_create_session_for_criteria(const core::dbus::Message::Ptr& msg);
140
141 // Tries to register the given session under the given path in the session store.
142 // Returns true iff the session has been added to the store.
143 bool add_to_session_store_for_path(
144 const core::dbus::types::ObjectPath& path,
145 std::unique_ptr<core::dbus::ServiceWatcher> watcher,
146 const session::Interface::Ptr& session);
147
148 // Removes the session with the given path from the session store.
149 void remove_from_session_store_for_path(const core::dbus::types::ObjectPath& path);
150
151 void add_client_application(const std::string& app_id);
152 void remove_client_application(const std::string& app_id);
153
154 // Called whenever the overall state of the service changes.
155 void on_state_changed(State state);
156 // Called whenever the value of the respective property changes.
157 void on_does_satellite_based_positioning_changed(bool value);
158 // Called whenever the value of the respective property changes.
159 void on_does_report_cell_and_wifi_ids_changed(bool value);
160 // Called whenever the value of the respective property changes.
161 void on_is_online_changed(bool value);
162 void on_client_applications_changed(const std::vector<std::string>& value);
163
164 // Stores the configuration passed in at creation time.
165 Configuration configuration;
166 // We observe sessions if they have died and resigned from the bus.
167 core::dbus::DBus daemon;
168 // The skeleton object representing com.lomiri.location.service.Interface on the bus.
169 core::dbus::Object::Ptr object;
170 // We emit property changes manually.
171 core::dbus::Signal
172 <
173 core::dbus::interfaces::Properties::Signals::PropertiesChanged,
174 core::dbus::interfaces::Properties::Signals::PropertiesChanged::ArgumentType
175 >::Ptr properties_changed;
176
177 // DBus properties as exposed on the bus for com.lomiri.location.service.Interface
178 struct
179 {
180 std::shared_ptr< core::dbus::Property<Interface::Properties::State> > state;
181 std::shared_ptr< core::dbus::Property<Interface::Properties::DoesSatelliteBasedPositioning> > does_satellite_based_positioning;
182 std::shared_ptr< core::dbus::Property<Interface::Properties::DoesReportCellAndWifiIds> > does_report_cell_and_wifi_ids;
183 std::shared_ptr< core::dbus::Property<Interface::Properties::IsOnline> > is_online;
184 std::shared_ptr< core::dbus::Property<Interface::Properties::VisibleSpaceVehicles> > visible_space_vehicles;
185 std::shared_ptr< core::dbus::Property<Interface::Properties::ClientApplications> > client_applications;
186 } properties;
187 // We sign up to property changes here, to be able to report them to the bus
188 struct
189 {
190 core::ScopedConnection state;
191 core::ScopedConnection does_satellite_based_positioning;
192 core::ScopedConnection does_report_cell_and_wifi_ids;
193 core::ScopedConnection is_online;
194 core::ScopedConnection client_applications;
195 } connections;
196 // Guards the session store.
197 std::mutex guard;
198 // We track sessions and their respective watchers.
199 struct Element
200 {
201 std::unique_ptr<core::dbus::ServiceWatcher> watcher;
202 std::shared_ptr<session::Interface> session;
203 };
204 // Keeps track of running sessions, keying them by their unique object path.
205 std::map<dbus::types::ObjectPath, Element> session_store;
206};
207}
208}
209}
210}
211#endif // LOCATION_SERVICE_COM_LOMIRI_LOCATION_SERVICE_SKELETON_H_
The Interface class models the primary interface to the location service.
std::shared_ptr< core::dbus::Property< Interface::Properties::State > > state
std::shared_ptr< core::dbus::Property< Interface::Properties::IsOnline > > is_online
std::shared_ptr< core::dbus::Property< Interface::Properties::DoesReportCellAndWifiIds > > does_report_cell_and_wifi_ids
core::Property< State > & mutable_state()
core::ScopedConnection does_satellite_based_positioning
std::shared_ptr< core::dbus::Property< Interface::Properties::VisibleSpaceVehicles > > visible_space_vehicles
core::ScopedConnection does_report_cell_and_wifi_ids
std::shared_ptr< core::dbus::Property< Interface::Properties::ClientApplications > > client_applications
std::shared_ptr< core::dbus::Property< Interface::Properties::DoesSatelliteBasedPositioning > > does_satellite_based_positioning
Skeleton(const Configuration &configuration)
State
State enumerates the known states of the service.
Definition state.h:27
Definition accuracy.h:24
Definition codec.h:39
A space-vehicle as visible to providers.
virtual Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr &msg)=0
Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr &msg)
DBusDaemonCredentialsResolver(const core::dbus::Bus::Ptr &bus, AppArmorProfileResolver app_armor_profile_resolver)
virtual core::dbus::types::ObjectPath object_path_for_caller_credentials(const Credentials &credentials)