| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/service_manager.h" | 5 #include "src/service_manager.h" |
| 6 | 6 |
| 7 #include <glog/logging.h> | 7 #include <glog/logging.h> |
| 8 | 8 |
| 9 #include "src/cashew_server.h" |
| 9 #include "src/service.h" | 10 #include "src/service.h" |
| 10 | 11 |
| 11 namespace cashew { | 12 namespace cashew { |
| 12 | 13 |
| 13 // Flimflam D-Bus indentifiers | 14 // Flimflam D-Bus indentifiers |
| 14 static const char* kFlimflamManagerName = "org.chromium.flimflam"; | 15 static const char* kFlimflamManagerName = "org.chromium.flimflam"; |
| 15 static const char* kFlimflamManagerPath = "/"; | 16 static const char* kFlimflamManagerPath = "/"; |
| 16 | 17 |
| 17 // Flimflam property names | 18 // Flimflam property names |
| 18 static const char* kServicesProperty = "Services"; | 19 static const char* kServicesProperty = "Services"; |
| 19 | 20 |
| 20 // Flimflam service types | 21 // Flimflam service types |
| 21 static const char* kTypeCellular = "cellular"; | 22 static const char* kTypeCellular = "cellular"; |
| 22 | 23 |
| 23 ServiceManager::ServiceManager(DBus::Connection& connection) // NOLINT | 24 ServiceManager::ServiceManager(DBus::Connection& connection) // NOLINT |
| 24 : DBus::ObjectProxy(connection, kFlimflamManagerPath, | 25 : DBus::ObjectProxy(connection, kFlimflamManagerPath, |
| 25 kFlimflamManagerName), | 26 kFlimflamManagerName), |
| 26 connection_(connection) { | 27 connection_(connection), cashew_server_(NULL) { |
| 27 // init our state with a GetProperties() call to Flimflam | 28 // init our state with a GetProperties() call to Flimflam |
| 28 // we'll update this state by monitoring PropertyChanged signals | 29 // we'll update this state by monitoring PropertyChanged signals |
| 29 GetFlimflamServices(); | 30 GetFlimflamServices(); |
| 30 } | 31 } |
| 31 | 32 |
| 32 ServiceManager::~ServiceManager() { | 33 ServiceManager::~ServiceManager() { |
| 33 DeleteServices(&services_); | 34 DeleteServices(&services_); |
| 34 } | 35 } |
| 35 | 36 |
| 36 const Service* ServiceManager::GetService(const std::string& service_path) | 37 const Service* ServiceManager::GetService(const std::string& service_path) |
| 37 const { | 38 const { |
| 38 ServiceMap::const_iterator it = services_.find(service_path); | 39 ServiceMap::const_iterator it = services_.find(service_path); |
| 39 if (it == services_.end()) { | 40 if (it == services_.end()) { |
| 40 return NULL; | 41 return NULL; |
| 41 } | 42 } |
| 42 DCHECK(it->second != NULL); | 43 DCHECK(it->second != NULL); |
| 43 return static_cast<const Service *>(it->second); | 44 return static_cast<const Service *>(it->second); |
| 44 } | 45 } |
| 45 | 46 |
| 47 void ServiceManager::SetCashewServer(CashewServer *cashew_server) { |
| 48 DLOG(INFO) << "SetCashewServer"; |
| 49 cashew_server_ = cashew_server; |
| 50 } |
| 51 |
| 46 // Flimflam Manager D-Bus Proxy methods | 52 // Flimflam Manager D-Bus Proxy methods |
| 47 | 53 |
| 48 void ServiceManager::PropertyChanged(const std::string& property_name, | 54 void ServiceManager::PropertyChanged(const std::string& property_name, |
| 49 const DBus::Variant& new_value) { | 55 const DBus::Variant& new_value) { |
| 50 DLOG(INFO) << "PropertyChanged: property_name = " << property_name; | 56 DLOG(INFO) << "PropertyChanged: property_name = " << property_name; |
| 51 | 57 |
| 52 // we only care about Services changes | 58 // we only care about Services changes |
| 53 if (property_name.compare(kServicesProperty)) { | 59 if (property_name.compare(kServicesProperty)) { |
| 54 return; | 60 return; |
| 55 } | 61 } |
| 56 | 62 |
| 57 // interpret new_value as a vector of service paths | 63 // interpret new_value as a vector of service paths |
| 58 ServicePathList paths; | 64 ServicePathList paths; |
| 59 DBus::MessageIter reader = new_value.reader(); | 65 DBus::MessageIter reader = new_value.reader(); |
| 60 reader >> paths; | 66 reader >> paths; |
| 61 | 67 |
| 62 OnServicesUpdate(paths); | 68 OnServicesUpdate(paths); |
| 63 } | 69 } |
| 64 | 70 |
| 65 void ServiceManager::StateChanged(const std::string& new_state) { | 71 void ServiceManager::StateChanged(const std::string& new_state) { |
| 66 DLOG(INFO) << "StateChanged: new_state = " << new_state; | 72 DLOG(INFO) << "StateChanged: new_state = " << new_state; |
| 67 // TODO(vlaviano): switch on state and call private onFlimflamOffline(), | 73 // TODO(vlaviano): switch on state and call private onFlimflamOffline(), |
| 68 // onFlimflamOnline() hooks. If we care. | 74 // onFlimflamOnline() hooks. If we care. |
| 69 } | 75 } |
| 70 | 76 |
| 77 // Service methods |
| 78 |
| 79 void ServiceManager::EmitDataPlansUpdate(const Service& service) { |
| 80 DLOG(INFO) << "EmitDataPlansUpdate: service = " << service.GetPath(); |
| 81 if (cashew_server_ == NULL) { |
| 82 DLOG(WARNING) << "EmitDataPlansUpdate: no Cashew server"; |
| 83 return; |
| 84 } |
| 85 cashew_server_->EmitDataPlansUpdate(service); |
| 86 } |
| 87 |
| 71 // Private methods | 88 // Private methods |
| 72 | 89 |
| 73 void ServiceManager::DeleteServices(ServiceMap *service_map) { | 90 void ServiceManager::DeleteServices(ServiceMap *service_map) { |
| 74 DCHECK(service_map != NULL); | 91 DCHECK(service_map != NULL); |
| 75 while (!service_map->empty()) { | 92 while (!service_map->empty()) { |
| 76 const std::string& path = | 93 const std::string& path = |
| 77 static_cast<std::string>(service_map->begin()->first); | 94 static_cast<std::string>(service_map->begin()->first); |
| 78 DLOG(INFO) << "DeleteServices: deleting service: " << path; | 95 DLOG(INFO) << "DeleteServices: deleting service: " << path; |
| 79 Service *service = static_cast<Service *>(service_map->begin()->second); | 96 Service *service = static_cast<Service *>(service_map->begin()->second); |
| 80 delete service; | 97 delete service; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 } | 170 } |
| 154 } | 171 } |
| 155 | 172 |
| 156 // services left in old_services failed to appear in the update: delete them | 173 // services left in old_services failed to appear in the update: delete them |
| 157 DeleteServices(&old_services); | 174 DeleteServices(&old_services); |
| 158 } | 175 } |
| 159 | 176 |
| 160 void ServiceManager::OnNewService(const DBus::Path& path) { | 177 void ServiceManager::OnNewService(const DBus::Path& path) { |
| 161 DLOG(INFO) << "OnNewService: " << path; | 178 DLOG(INFO) << "OnNewService: " << path; |
| 162 DCHECK(GetService(path) == NULL); | 179 DCHECK(GetService(path) == NULL); |
| 163 Service *service = new(std::nothrow) Service(connection_, path); | 180 Service *service = new(std::nothrow) Service(this, connection_, path); |
| 164 if (service == NULL) { | 181 if (service == NULL) { |
| 165 LOG(ERROR) << "OnNewService: couldn't create service for " << path; | 182 LOG(ERROR) << "OnNewService: couldn't create service for " << path; |
| 166 return; | 183 return; |
| 167 } | 184 } |
| 168 services_[path] = service; | 185 services_[path] = service; |
| 169 } | 186 } |
| 170 | 187 |
| 171 } // namespace cashew | 188 } // namespace cashew |
| OLD | NEW |