ubuntu-location-service  ..
An aggregating location service providing positioning and geocoding capabilities to applications.
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_UBUNTU_LOCATION_SERVICE_SKELETON_H_
19 #define LOCATION_SERVICE_COM_UBUNTU_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 namespace com
34 {
35 namespace ubuntu
36 {
37 namespace location
38 {
39 namespace service
40 {
41 class Skeleton
42  : public core::dbus::Skeleton<com::ubuntu::location::service::Interface>,
43  public std::enable_shared_from_this<Skeleton>
44 {
45 public:
46  typedef std::shared_ptr<Skeleton> Ptr;
47 
48  // Models resolution of an incoming dbus message to the credentials of the message sender.
50  {
51  typedef std::shared_ptr<CredentialsResolver> Ptr;
52 
53  CredentialsResolver() = default;
54  virtual ~CredentialsResolver() = default;
55 
56  // Resolves the sender of msg to the respective credentials.
57  virtual Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr& msg) = 0;
58  };
59 
60  // Implements CredentialsResolver by reaching out to the dbus daemon and
61  // invoking:
62  // * GetConnectionUnixProcessID
63  // * GetConnectionUnixUser
65  {
66  // Sets up a new instance for the given bus connection.
67  DBusDaemonCredentialsResolver(const core::dbus::Bus::Ptr& bus);
68 
69  // Resolves the sender of msg to pid, uid by calling out to the dbus daemon.
70  Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr& msg);
71 
72  // Stub for accessing the dbus daemon.
73  core::dbus::DBus daemon;
74  };
75 
76  // Models the generation of stable and unique object paths for client-specific sessions.
77  // The requirements for the resulting object path are:
78  // * Unique for the entire system over its complete lifetime
79  // * Stable with respect to an app. That is, one app is always assigned the same object path.
81  {
82  typedef std::shared_ptr<ObjectPathGenerator> Ptr;
83 
84  ObjectPathGenerator() = default;
85  virtual ~ObjectPathGenerator() = default;
86 
87  // Calculates an object path from pid and uid. The default implementation
88  // creates the path according to the following steps:
89  // [1.] Query the AppArmor profile name for pid in credentials.
90  // [1.1] If the process is running unconfined, rely on a counter to assemble the session name.
91  // [1.2] If the process is confined, use the AppArmor profile name to generate the path.
92  virtual core::dbus::types::ObjectPath object_path_for_caller_credentials(const Credentials& credentials);
93  };
94 
96  {
97  // DBus connection set up for handling requests to the service.
98  core::dbus::Bus::Ptr incoming;
99  // DBus connection for reaching out to other services in a non-blocking way.
100  core::dbus::Bus::Ptr outgoing;
101  // An implementation of CredentialsResolver for resolving incoming message sender
102  // to Credentials = uid, pid.
104  // An implementation of ObjectPathGenerator for generating session names.
106  // Permission manager implementation for verifying incoming requests.
108  };
109 
110  Skeleton(const Configuration& configuration);
111  ~Skeleton() noexcept;
112 
113  // From com::ubuntu::location::service::Interface
114  core::Property<bool>& does_satellite_based_positioning();
115  core::Property<bool>& does_report_cell_and_wifi_ids();
116  core::Property<bool>& is_online();
117  core::Property<std::map<SpaceVehicle::Key, SpaceVehicle>>& visible_space_vehicles();
118 
119 private:
120  // Handles incoming message calls for create_session_for_criteria.
121  // Dispatches to the actual implementation, and manages object lifetimes.
122  void handle_create_session_for_criteria(const core::dbus::Message::Ptr& msg);
123 
124  // Tries to register the given session under the given path in the session store.
125  // Returns true iff the session has been added to the store.
126  bool add_to_session_store_for_path(
127  const core::dbus::types::ObjectPath& path,
128  std::unique_ptr<core::dbus::ServiceWatcher> watcher,
129  const session::Interface::Ptr& session);
130 
131  // Removes the session with the given path from the session store.
132  void remove_from_session_store_for_path(const core::dbus::types::ObjectPath& path);
133 
134  // Called whenever the value of the respective property changes.
135  void on_does_satellite_based_positioning_changed(bool value);
136  // Called whenever the value of the respective property changes.
137  void on_does_report_cell_and_wifi_ids_changed(bool value);
138  // Called whenever the value of the respective property changes.
139  void on_is_online_changed(bool value);
140 
141  // Stores the configuration passed in at creation time.
142  Configuration configuration;
143  // We observe sessions if they have died and resigned from the bus.
144  core::dbus::DBus daemon;
145  // The skeleton object representing com.ubuntu.location.service.Interface on the bus.
146  core::dbus::Object::Ptr object;
147  // We emit property changes manually.
148  core::dbus::Signal
149  <
150  core::dbus::interfaces::Properties::Signals::PropertiesChanged,
151  core::dbus::interfaces::Properties::Signals::PropertiesChanged::ArgumentType
152  >::Ptr properties_changed;
153 
154  // DBus properties as exposed on the bus for com.ubuntu.location.service.Interface
155  struct
156  {
157  std::shared_ptr< core::dbus::Property<Interface::Properties::DoesSatelliteBasedPositioning> > does_satellite_based_positioning;
158  std::shared_ptr< core::dbus::Property<Interface::Properties::DoesReportCellAndWifiIds> > does_report_cell_and_wifi_ids;
159  std::shared_ptr< core::dbus::Property<Interface::Properties::IsOnline> > is_online;
160  std::shared_ptr< core::dbus::Property<Interface::Properties::VisibleSpaceVehicles> > visible_space_vehicles;
161  } properties;
162  // We sign up to property changes here, to be able to report them to the bus
163  struct
164  {
165  core::ScopedConnection does_satellite_based_positioning;
166  core::ScopedConnection does_report_cell_and_wifi_ids;
167  core::ScopedConnection is_online;
168  } connections;
169  // Guards the session store.
170  std::mutex guard;
171  // We track sessions and their respective watchers.
172  struct Element
173  {
174  std::unique_ptr<core::dbus::ServiceWatcher> watcher;
175  std::shared_ptr<session::Interface> session;
176  };
177  // Keeps track of running sessions, keying them by their unique object path.
178  std::map<dbus::types::ObjectPath, Element> session_store;
179 };
180 }
181 }
182 }
183 }
184 #endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_SERVICE_SKELETON_H_
core::ScopedConnection does_satellite_based_positioning
Definition: skeleton.h:165
std::shared_ptr< core::dbus::Property< Interface::Properties::DoesReportCellAndWifiIds > > does_report_cell_and_wifi_ids
Definition: skeleton.h:158
core::Property< std::map< SpaceVehicle::Key, SpaceVehicle > > & visible_space_vehicles()
std::shared_ptr< PermissionManager > Ptr
virtual core::dbus::types::ObjectPath object_path_for_caller_credentials(const Credentials &credentials)
Definition: codec.h:35
STL namespace.
Definition: accuracy.h:23
core::Property< bool > & does_report_cell_and_wifi_ids()
std::shared_ptr< core::dbus::Property< Interface::Properties::IsOnline > > is_online
Definition: skeleton.h:159
std::shared_ptr< core::dbus::Property< Interface::Properties::DoesSatelliteBasedPositioning > > does_satellite_based_positioning
Definition: skeleton.h:157
Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr &msg)
Credentials of a remote session.
Skeleton(const Configuration &configuration)
core::ScopedConnection is_online
Definition: skeleton.h:167
core::Property< bool > & does_satellite_based_positioning()
The Interface class models the primary interface to the location service.
Definition: interface.h:46
virtual Credentials resolve_credentials_for_incoming_message(const core::dbus::Message::Ptr &msg)=0
std::shared_ptr< CredentialsResolver > Ptr
Definition: skeleton.h:51
std::shared_ptr< ObjectPathGenerator > Ptr
Definition: skeleton.h:82
core::ScopedConnection does_report_cell_and_wifi_ids
Definition: skeleton.h:166
core::Property< bool > & is_online()
std::shared_ptr< Skeleton > Ptr
Definition: skeleton.h:46
A space-vehicle as visible to providers.
Definition: space_vehicle.h:33
std::shared_ptr< core::dbus::Property< Interface::Properties::VisibleSpaceVehicles > > visible_space_vehicles
Definition: skeleton.h:160