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  const core::Property<State>& state() const;
115  core::Property<bool>& does_satellite_based_positioning();
116  core::Property<bool>& does_report_cell_and_wifi_ids();
117  core::Property<bool>& is_online();
118  core::Property<std::map<SpaceVehicle::Key, SpaceVehicle>>& visible_space_vehicles();
119 
120 protected:
121  // Enable subclasses to alter the state.
122  core::Property<State>& mutable_state();
123 private:
124  // Handles incoming message calls for create_session_for_criteria.
125  // Dispatches to the actual implementation, and manages object lifetimes.
126  void handle_create_session_for_criteria(const core::dbus::Message::Ptr& msg);
127 
128  // Tries to register the given session under the given path in the session store.
129  // Returns true iff the session has been added to the store.
130  bool add_to_session_store_for_path(
131  const core::dbus::types::ObjectPath& path,
132  std::unique_ptr<core::dbus::ServiceWatcher> watcher,
133  const session::Interface::Ptr& session);
134 
135  // Removes the session with the given path from the session store.
136  void remove_from_session_store_for_path(const core::dbus::types::ObjectPath& path);
137 
138  // Called whenever the overall state of the service changes.
139  void on_state_changed(State state);
140  // Called whenever the value of the respective property changes.
141  void on_does_satellite_based_positioning_changed(bool value);
142  // Called whenever the value of the respective property changes.
143  void on_does_report_cell_and_wifi_ids_changed(bool value);
144  // Called whenever the value of the respective property changes.
145  void on_is_online_changed(bool value);
146 
147  // Stores the configuration passed in at creation time.
148  Configuration configuration;
149  // We observe sessions if they have died and resigned from the bus.
150  core::dbus::DBus daemon;
151  // The skeleton object representing com.ubuntu.location.service.Interface on the bus.
152  core::dbus::Object::Ptr object;
153  // We emit property changes manually.
154  core::dbus::Signal
155  <
156  core::dbus::interfaces::Properties::Signals::PropertiesChanged,
157  core::dbus::interfaces::Properties::Signals::PropertiesChanged::ArgumentType
158  >::Ptr properties_changed;
159 
160  // DBus properties as exposed on the bus for com.ubuntu.location.service.Interface
161  struct
162  {
163  std::shared_ptr< core::dbus::Property<Interface::Properties::State> > state;
164  std::shared_ptr< core::dbus::Property<Interface::Properties::DoesSatelliteBasedPositioning> > does_satellite_based_positioning;
165  std::shared_ptr< core::dbus::Property<Interface::Properties::DoesReportCellAndWifiIds> > does_report_cell_and_wifi_ids;
166  std::shared_ptr< core::dbus::Property<Interface::Properties::IsOnline> > is_online;
167  std::shared_ptr< core::dbus::Property<Interface::Properties::VisibleSpaceVehicles> > visible_space_vehicles;
168  } properties;
169  // We sign up to property changes here, to be able to report them to the bus
170  struct
171  {
172  core::ScopedConnection state;
173  core::ScopedConnection does_satellite_based_positioning;
174  core::ScopedConnection does_report_cell_and_wifi_ids;
175  core::ScopedConnection is_online;
176  } connections;
177  // Guards the session store.
178  std::mutex guard;
179  // We track sessions and their respective watchers.
180  struct Element
181  {
182  std::unique_ptr<core::dbus::ServiceWatcher> watcher;
183  std::shared_ptr<session::Interface> session;
184  };
185  // Keeps track of running sessions, keying them by their unique object path.
186  std::map<dbus::types::ObjectPath, Element> session_store;
187 };
188 }
189 }
190 }
191 }
192 #endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_SERVICE_SKELETON_H_
State
State enumerates the known states of the service.
Definition: state.h:26
core::ScopedConnection does_satellite_based_positioning
Definition: skeleton.h:173
std::shared_ptr< core::dbus::Property< Interface::Properties::DoesReportCellAndWifiIds > > does_report_cell_and_wifi_ids
Definition: skeleton.h:165
core::Property< std::map< SpaceVehicle::Key, SpaceVehicle > > & visible_space_vehicles()
std::shared_ptr< PermissionManager > Ptr
std::shared_ptr< core::dbus::Property< Interface::Properties::State > > state
Definition: skeleton.h:163
Definition: codec.h:38
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:166
std::shared_ptr< core::dbus::Property< Interface::Properties::DoesSatelliteBasedPositioning > > does_satellite_based_positioning
Definition: skeleton.h:164
Credentials of a remote session.
Skeleton(const Configuration &configuration)
core::ScopedConnection is_online
Definition: skeleton.h:175
core::Property< bool > & does_satellite_based_positioning()
The Interface class models the primary interface to the location service.
Definition: interface.h:47
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:174
core::Property< bool > & is_online()
std::shared_ptr< Skeleton > Ptr
Definition: skeleton.h:46
const core::Property< State > & state() const
A space-vehicle as visible to providers.
Definition: space_vehicle.h:33
core::Property< State > & mutable_state()
std::shared_ptr< core::dbus::Property< Interface::Properties::VisibleSpaceVehicles > > visible_space_vehicles
Definition: skeleton.h:167