connection_manager.cc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // Copyright (C) 2012 The Android Open Source Project
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. #include "update_engine/connection_manager.h"
  17. #include <memory>
  18. #include <set>
  19. #include <string>
  20. #include <base/stl_util.h>
  21. #include <base/strings/string_util.h>
  22. #include <policy/device_policy.h>
  23. #include <shill/dbus-constants.h>
  24. #include <shill/dbus-proxies.h>
  25. #include "update_engine/common/prefs.h"
  26. #include "update_engine/common/utils.h"
  27. #include "update_engine/connection_utils.h"
  28. #include "update_engine/shill_proxy.h"
  29. #include "update_engine/system_state.h"
  30. #include "update_engine/update_attempter.h"
  31. using org::chromium::flimflam::ManagerProxyInterface;
  32. using org::chromium::flimflam::ServiceProxyInterface;
  33. using std::set;
  34. using std::string;
  35. namespace chromeos_update_engine {
  36. namespace connection_manager {
  37. std::unique_ptr<ConnectionManagerInterface> CreateConnectionManager(
  38. SystemState* system_state) {
  39. return std::unique_ptr<ConnectionManagerInterface>(
  40. new ConnectionManager(new ShillProxy(), system_state));
  41. }
  42. } // namespace connection_manager
  43. ConnectionManager::ConnectionManager(ShillProxyInterface* shill_proxy,
  44. SystemState* system_state)
  45. : shill_proxy_(shill_proxy), system_state_(system_state) {}
  46. bool ConnectionManager::IsUpdateAllowedOver(
  47. ConnectionType type, ConnectionTethering tethering) const {
  48. switch (type) {
  49. case ConnectionType::kBluetooth:
  50. return false;
  51. case ConnectionType::kCellular: {
  52. set<string> allowed_types;
  53. const policy::DevicePolicy* device_policy =
  54. system_state_->device_policy();
  55. // The device_policy is loaded in a lazy way before an update check. Load
  56. // it now from the libbrillo cache if it wasn't already loaded.
  57. if (!device_policy) {
  58. UpdateAttempter* update_attempter = system_state_->update_attempter();
  59. if (update_attempter) {
  60. update_attempter->RefreshDevicePolicy();
  61. device_policy = system_state_->device_policy();
  62. }
  63. }
  64. if (!device_policy) {
  65. // Device policy fails to be loaded (possibly due to guest account). We
  66. // do not check the local user setting here, which should be checked by
  67. // |OmahaRequestAction| during checking for update.
  68. LOG(INFO) << "Allowing updates over cellular as device policy "
  69. "fails to be loaded.";
  70. return true;
  71. }
  72. if (device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
  73. // The update setting is enforced by the device policy.
  74. if (!base::ContainsKey(allowed_types, shill::kTypeCellular)) {
  75. LOG(INFO) << "Disabling updates over cellular connection as it's not "
  76. "allowed in the device policy.";
  77. return false;
  78. }
  79. LOG(INFO) << "Allowing updates over cellular per device policy.";
  80. return true;
  81. }
  82. // If there's no update setting in the device policy, we do not check
  83. // the local user setting here, which should be checked by
  84. // |OmahaRequestAction| during checking for update.
  85. LOG(INFO) << "Allowing updates over cellular as device policy does "
  86. "not include update setting.";
  87. return true;
  88. }
  89. default:
  90. if (tethering == ConnectionTethering::kConfirmed) {
  91. // Treat this connection as if it is a cellular connection.
  92. LOG(INFO) << "Current connection is confirmed tethered, using Cellular "
  93. "setting.";
  94. return IsUpdateAllowedOver(ConnectionType::kCellular,
  95. ConnectionTethering::kUnknown);
  96. }
  97. return true;
  98. }
  99. }
  100. bool ConnectionManager::IsAllowedConnectionTypesForUpdateSet() const {
  101. const policy::DevicePolicy* device_policy = system_state_->device_policy();
  102. if (!device_policy) {
  103. LOG(INFO) << "There's no device policy loaded yet.";
  104. return false;
  105. }
  106. set<string> allowed_types;
  107. if (!device_policy->GetAllowedConnectionTypesForUpdate(&allowed_types)) {
  108. return false;
  109. }
  110. return true;
  111. }
  112. bool ConnectionManager::GetConnectionProperties(
  113. ConnectionType* out_type, ConnectionTethering* out_tethering) {
  114. dbus::ObjectPath default_service_path;
  115. TEST_AND_RETURN_FALSE(GetDefaultServicePath(&default_service_path));
  116. if (!default_service_path.IsValid())
  117. return false;
  118. // Shill uses the "/" service path to indicate that it is not connected.
  119. if (default_service_path.value() == "/") {
  120. *out_type = ConnectionType::kDisconnected;
  121. *out_tethering = ConnectionTethering::kUnknown;
  122. return true;
  123. }
  124. TEST_AND_RETURN_FALSE(
  125. GetServicePathProperties(default_service_path, out_type, out_tethering));
  126. return true;
  127. }
  128. bool ConnectionManager::GetDefaultServicePath(dbus::ObjectPath* out_path) {
  129. brillo::VariantDictionary properties;
  130. brillo::ErrorPtr error;
  131. ManagerProxyInterface* manager_proxy = shill_proxy_->GetManagerProxy();
  132. if (!manager_proxy)
  133. return false;
  134. TEST_AND_RETURN_FALSE(manager_proxy->GetProperties(&properties, &error));
  135. const auto& prop_default_service =
  136. properties.find(shill::kDefaultServiceProperty);
  137. if (prop_default_service == properties.end())
  138. return false;
  139. *out_path = prop_default_service->second.TryGet<dbus::ObjectPath>();
  140. return out_path->IsValid();
  141. }
  142. bool ConnectionManager::GetServicePathProperties(
  143. const dbus::ObjectPath& path,
  144. ConnectionType* out_type,
  145. ConnectionTethering* out_tethering) {
  146. // We create and dispose the ServiceProxyInterface on every request.
  147. std::unique_ptr<ServiceProxyInterface> service =
  148. shill_proxy_->GetServiceForPath(path);
  149. brillo::VariantDictionary properties;
  150. brillo::ErrorPtr error;
  151. TEST_AND_RETURN_FALSE(service->GetProperties(&properties, &error));
  152. // Populate the out_tethering.
  153. const auto& prop_tethering = properties.find(shill::kTetheringProperty);
  154. if (prop_tethering == properties.end()) {
  155. // Set to Unknown if not present.
  156. *out_tethering = ConnectionTethering::kUnknown;
  157. } else {
  158. // If the property doesn't contain a string value, the empty string will
  159. // become kUnknown.
  160. *out_tethering = connection_utils::ParseConnectionTethering(
  161. prop_tethering->second.TryGet<string>());
  162. }
  163. // Populate the out_type property.
  164. const auto& prop_type = properties.find(shill::kTypeProperty);
  165. if (prop_type == properties.end()) {
  166. // Set to Unknown if not present.
  167. *out_type = ConnectionType::kUnknown;
  168. return false;
  169. }
  170. string type_str = prop_type->second.TryGet<string>();
  171. if (type_str == shill::kTypeVPN) {
  172. const auto& prop_physical =
  173. properties.find(shill::kPhysicalTechnologyProperty);
  174. if (prop_physical == properties.end()) {
  175. LOG(ERROR) << "No PhysicalTechnology property found for a VPN"
  176. " connection (service: "
  177. << path.value() << "). Returning default kUnknown value.";
  178. *out_type = ConnectionType::kUnknown;
  179. } else {
  180. *out_type = connection_utils::ParseConnectionType(
  181. prop_physical->second.TryGet<string>());
  182. }
  183. } else {
  184. *out_type = connection_utils::ParseConnectionType(type_str);
  185. }
  186. return true;
  187. }
  188. } // namespace chromeos_update_engine