1 //
2 // Copyright (C) 2015 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 
17 #include "shill/dbus/chromeos_profile_dbus_adaptor.h"
18 
19 #include <string>
20 #include <vector>
21 
22 #include "shill/error.h"
23 #include "shill/logging.h"
24 #include "shill/profile.h"
25 #include "shill/service.h"
26 
27 using brillo::dbus_utils::AsyncEventSequencer;
28 using brillo::dbus_utils::ExportedObjectManager;
29 using std::string;
30 using std::vector;
31 
32 namespace shill {
33 
34 namespace Logging {
35 static auto kModuleLogScope = ScopeLogger::kDBus;
ObjectID(ChromeosProfileDBusAdaptor * p)36 static string ObjectID(ChromeosProfileDBusAdaptor* p) {
37   return p->GetRpcIdentifier();
38 }
39 }
40 
41 
42 // static
43 const char ChromeosProfileDBusAdaptor::kPath[] = "/profile/";
44 
ChromeosProfileDBusAdaptor(const scoped_refptr<dbus::Bus> & bus,Profile * profile)45 ChromeosProfileDBusAdaptor::ChromeosProfileDBusAdaptor(
46     const scoped_refptr<dbus::Bus>& bus,
47     Profile* profile)
48     : org::chromium::flimflam::ProfileAdaptor(this),
49       ChromeosDBusAdaptor(bus, kPath + profile->GetFriendlyName()),
50       profile_(profile) {
51   // Register DBus object.
52   RegisterWithDBusObject(dbus_object());
53   dbus_object()->RegisterAndBlock();
54 }
55 
~ChromeosProfileDBusAdaptor()56 ChromeosProfileDBusAdaptor::~ChromeosProfileDBusAdaptor() {
57   dbus_object()->UnregisterAsync();
58   profile_ = nullptr;
59 }
60 
EmitBoolChanged(const string & name,bool value)61 void ChromeosProfileDBusAdaptor::EmitBoolChanged(const string& name,
62                                                  bool value) {
63   SLOG(this, 2) << __func__ << ": " << name;
64   SendPropertyChangedSignal(name, brillo::Any(value));
65 }
66 
EmitUintChanged(const string & name,uint32_t value)67 void ChromeosProfileDBusAdaptor::EmitUintChanged(const string& name,
68                                                  uint32_t value) {
69   SLOG(this, 2) << __func__ << ": " << name;
70   SendPropertyChangedSignal(name, brillo::Any(value));
71 }
72 
EmitIntChanged(const string & name,int value)73 void ChromeosProfileDBusAdaptor::EmitIntChanged(const string& name, int value) {
74   SLOG(this, 2) << __func__ << ": " << name;
75   SendPropertyChangedSignal(name, brillo::Any(value));
76 }
77 
EmitStringChanged(const string & name,const string & value)78 void ChromeosProfileDBusAdaptor::EmitStringChanged(const string& name,
79                                                    const string& value) {
80   SLOG(this, 2) << __func__ << ": " << name;
81   SendPropertyChangedSignal(name, brillo::Any(value));
82 }
83 
GetProperties(brillo::ErrorPtr * error,brillo::VariantDictionary * properties)84 bool ChromeosProfileDBusAdaptor::GetProperties(
85     brillo::ErrorPtr* error, brillo::VariantDictionary* properties) {
86   SLOG(this, 2) << __func__;
87   return ChromeosDBusAdaptor::GetProperties(profile_->store(),
88                                             properties,
89                                             error);
90 }
91 
SetProperty(brillo::ErrorPtr * error,const string & name,const brillo::Any & value)92 bool ChromeosProfileDBusAdaptor::SetProperty(
93     brillo::ErrorPtr* error, const string& name, const brillo::Any& value) {
94   SLOG(this, 2) << __func__ << ": " << name;
95   return ChromeosDBusAdaptor::SetProperty(profile_->mutable_store(),
96                                           name,
97                                           value,
98                                           error);
99 }
100 
GetEntry(brillo::ErrorPtr * error,const std::string & name,brillo::VariantDictionary * entry_properties)101 bool ChromeosProfileDBusAdaptor::GetEntry(
102     brillo::ErrorPtr* error,
103     const std::string& name,
104     brillo::VariantDictionary* entry_properties) {
105   SLOG(this, 2) << __func__ << ": " << name;
106   Error e;
107   ServiceRefPtr service = profile_->GetServiceFromEntry(name, &e);
108   if (!e.IsSuccess()) {
109     return !e.ToChromeosError(error);
110   }
111   return ChromeosDBusAdaptor::GetProperties(service->store(),
112                                             entry_properties,
113                                             error);
114 }
115 
DeleteEntry(brillo::ErrorPtr * error,const std::string & name)116 bool ChromeosProfileDBusAdaptor::DeleteEntry(brillo::ErrorPtr* error,
117                                              const std::string& name) {
118   SLOG(this, 2) << __func__ << ": " << name;
119   Error e;
120   profile_->DeleteEntry(name, &e);
121   return !e.ToChromeosError(error);
122 }
123 
124 }  // namespace shill
125