Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium 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 "chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_hand ler.h" | 5 #include "chrome/browser/ui/webui/chromeos/emulator/device_emulator_message_hand ler.h" |
| 6 | 6 |
| 7 #include <stdlib.h> | 7 #include <stdlib.h> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chromeos/dbus/dbus_thread_manager.h" | 11 #include "chromeos/dbus/dbus_thread_manager.h" |
| 12 #include "chromeos/dbus/fake_power_manager_client.h" | 12 #include "chromeos/dbus/fake_power_manager_client.h" |
| 13 #include "content/public/browser/web_ui.h" | 13 #include "content/public/browser/web_ui.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Define the name of the callback functions that will be used by JavaScript. | 17 // Define the name of the callback functions that will be used by JavaScript. |
| 18 const char kBluetoothDiscoverFunction[] = "requestBluetoothDiscover"; | 18 const char kBluetoothDiscoverFunction[] = "requestBluetoothDiscover"; |
| 19 const char kBluetoothPairFunction[] = "requestBluetoothPair"; | 19 const char kBluetoothPairFunction[] = "requestBluetoothPair"; |
| 20 const char kRequestPowerInfo[] = "requestPowerInfo"; | 20 const char kRequestPowerInfo[] = "requestPowerInfo"; |
| 21 | 21 |
| 22 // Define update functions that will update the power properties to the | 22 // Define update functions that will update the power properties to the |
| 23 // variables defined in the web UI. | 23 // variables defined in the web UI. |
| 24 const char kUpdateBatteryPercent[] = "updateBatteryPercent"; | 24 const char kUpdateBatteryPercent[] = "updateBatteryPercent"; |
| 25 const char kUpdateBatteryState[] = "updateBatteryState"; | |
| 25 const char kUpdateExternalPower[] = "updateExternalPower"; | 26 const char kUpdateExternalPower[] = "updateExternalPower"; |
| 26 const char kUpdateTimeToEmpty[] = "updateTimeToEmpty"; | 27 const char kUpdateTimeToEmpty[] = "updateTimeToEmpty"; |
| 27 const char kUpdateTimeToFull[] = "updateTimeToFull"; | 28 const char kUpdateTimeToFull[] = "updateTimeToFull"; |
| 28 | 29 |
| 29 // Define callback functions that will update the JavaScript variable | 30 // Define callback functions that will update the JavaScript variable |
| 30 // and the web UI. | 31 // and the web UI. |
| 31 const char kUpdatePowerPropertiesJSCallback[] = | 32 const char kUpdatePowerPropertiesJSCallback[] = |
| 32 "device_emulator.batterySettings.updatePowerProperties"; | 33 "device_emulator.batterySettings.updatePowerProperties"; |
| 33 | 34 |
| 34 } // namespace | 35 } // namespace |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 power_manager::PowerSupplyProperties props = | 72 power_manager::PowerSupplyProperties props = |
| 72 fake_power_manager_client_->props(); | 73 fake_power_manager_client_->props(); |
| 73 | 74 |
| 74 int new_percent; | 75 int new_percent; |
| 75 if (args->GetInteger(0, &new_percent)) | 76 if (args->GetInteger(0, &new_percent)) |
| 76 props.set_battery_percent(new_percent); | 77 props.set_battery_percent(new_percent); |
| 77 | 78 |
| 78 fake_power_manager_client_->UpdatePowerProperties(props); | 79 fake_power_manager_client_->UpdatePowerProperties(props); |
| 79 } | 80 } |
| 80 | 81 |
| 82 void DeviceEmulatorMessageHandler::UpdateBatteryState( | |
| 83 const base::ListValue* args) { | |
| 84 power_manager::PowerSupplyProperties props = | |
| 85 fake_power_manager_client_->props(); | |
| 86 | |
| 87 int battery_state; | |
| 88 if (args->GetInteger(0, &battery_state)) | |
|
oshima
2015/07/29 21:25:12
nit: you need {} in this case
mozartalouis
2015/07/29 21:55:47
Done.
| |
| 89 props.set_battery_state( | |
| 90 static_cast<power_manager::PowerSupplyProperties_BatteryState>( | |
| 91 battery_state)); | |
| 92 | |
| 93 fake_power_manager_client_->UpdatePowerProperties(props); | |
| 94 } | |
| 95 | |
| 81 void DeviceEmulatorMessageHandler::UpdateExternalPower( | 96 void DeviceEmulatorMessageHandler::UpdateExternalPower( |
| 82 const base::ListValue* args) { | 97 const base::ListValue* args) { |
| 83 int power_source; | |
| 84 args->GetInteger(0, &power_source); | |
| 85 | |
| 86 power_manager::PowerSupplyProperties props = | 98 power_manager::PowerSupplyProperties props = |
| 87 fake_power_manager_client_->props(); | 99 fake_power_manager_client_->props(); |
| 100 | |
| 101 int power_source; | |
| 102 if (args->GetInteger(0, &power_source)) | |
| 88 props.set_external_power( | 103 props.set_external_power( |
|
oshima
2015/07/29 21:25:12
nit: indent and {}
mozartalouis
2015/07/29 21:55:47
Done.
| |
| 89 static_cast<power_manager::PowerSupplyProperties_ExternalPower>( | 104 static_cast<power_manager::PowerSupplyProperties_ExternalPower>( |
| 90 power_source)); | 105 power_source)); |
| 106 | |
| 91 fake_power_manager_client_->UpdatePowerProperties(props); | 107 fake_power_manager_client_->UpdatePowerProperties(props); |
| 92 } | 108 } |
| 93 | 109 |
| 94 void DeviceEmulatorMessageHandler::UpdateTimeToEmpty( | 110 void DeviceEmulatorMessageHandler::UpdateTimeToEmpty( |
| 95 const base::ListValue* args) { | 111 const base::ListValue* args) { |
| 96 power_manager::PowerSupplyProperties props = | 112 power_manager::PowerSupplyProperties props = |
| 97 fake_power_manager_client_->props(); | 113 fake_power_manager_client_->props(); |
| 98 | 114 |
| 99 int new_time; | 115 int new_time; |
| 100 if (args->GetInteger(0, &new_time)) | 116 if (args->GetInteger(0, &new_time)) |
| 101 props.set_battery_time_to_empty_sec(new_time); | 117 props.set_battery_time_to_empty_sec(new_time); |
| 102 | 118 |
| 103 fake_power_manager_client_->UpdatePowerProperties(props); | 119 fake_power_manager_client_->UpdatePowerProperties(props); |
| 104 } | 120 } |
| 105 | 121 |
| 106 void DeviceEmulatorMessageHandler::UpdateTimeToFull( | 122 void DeviceEmulatorMessageHandler::UpdateTimeToFull( |
| 107 const base::ListValue* args) { | 123 const base::ListValue* args) { |
| 108 power_manager::PowerSupplyProperties props = | 124 power_manager::PowerSupplyProperties props = |
| 109 fake_power_manager_client_->props(); | 125 fake_power_manager_client_->props(); |
| 126 | |
| 110 int new_time; | 127 int new_time; |
| 111 if (args->GetInteger(0, &new_time)) | 128 if (args->GetInteger(0, &new_time)) |
| 112 props.set_battery_time_to_full_sec(new_time); | 129 props.set_battery_time_to_full_sec(new_time); |
| 130 | |
| 113 fake_power_manager_client_->UpdatePowerProperties(props); | 131 fake_power_manager_client_->UpdatePowerProperties(props); |
| 114 } | 132 } |
| 115 | 133 |
| 116 void DeviceEmulatorMessageHandler::PowerChanged( | 134 void DeviceEmulatorMessageHandler::PowerChanged( |
| 117 const power_manager::PowerSupplyProperties& proto) { | 135 const power_manager::PowerSupplyProperties& proto) { |
| 118 web_ui()->CallJavascriptFunction( | 136 // Create a list that holds all the values that the ui will need |
| 119 kUpdatePowerPropertiesJSCallback, | 137 // when this function is called. |
|
oshima
2015/07/29 21:25:13
This comment isn't much useful. You can omit it.
mozartalouis
2015/07/29 21:55:47
Done.
| |
| 120 base::FundamentalValue(proto.battery_percent()), | 138 scoped_ptr<base::ListValue> power_properties(new base::ListValue); |
|
oshima
2015/07/29 21:25:13
you can just create an object on stack like
base:
mozartalouis
2015/07/29 21:55:47
Done.
| |
| 121 base::FundamentalValue(proto.external_power()), | 139 |
| 122 base::FundamentalValue( | 140 power_properties->AppendInteger(static_cast<int>(proto.battery_percent())); |
| 123 static_cast<int>(proto.battery_time_to_empty_sec())), | 141 power_properties->AppendInteger(static_cast<int>(proto.battery_state())); |
| 124 base::FundamentalValue( | 142 power_properties->AppendInteger(static_cast<int>(proto.external_power())); |
| 125 static_cast<int>(proto.battery_time_to_full_sec()))); | 143 power_properties->AppendInteger( |
| 144 static_cast<int>(proto.battery_time_to_empty_sec())); | |
| 145 power_properties->AppendInteger( | |
| 146 static_cast<int>(proto.battery_time_to_full_sec())); | |
| 147 | |
| 148 web_ui()->CallJavascriptFunction(kUpdatePowerPropertiesJSCallback, | |
| 149 *power_properties); | |
|
oshima
2015/07/29 21:25:13
I thought you wanted to use
CallJavascriptFunctio
mozartalouis
2015/07/29 21:55:47
Done.
| |
| 126 } | 150 } |
| 127 | 151 |
| 128 void DeviceEmulatorMessageHandler::RegisterMessages() { | 152 void DeviceEmulatorMessageHandler::RegisterMessages() { |
| 129 web_ui()->RegisterMessageCallback( | 153 web_ui()->RegisterMessageCallback( |
| 130 kRequestPowerInfo, | 154 kRequestPowerInfo, |
| 131 base::Bind(&DeviceEmulatorMessageHandler::RequestPowerInfo, | 155 base::Bind(&DeviceEmulatorMessageHandler::RequestPowerInfo, |
| 132 base::Unretained(this))); | 156 base::Unretained(this))); |
| 133 web_ui()->RegisterMessageCallback( | 157 web_ui()->RegisterMessageCallback( |
| 134 kUpdateBatteryPercent, | 158 kUpdateBatteryPercent, |
| 135 base::Bind(&DeviceEmulatorMessageHandler::UpdateBatteryPercent, | 159 base::Bind(&DeviceEmulatorMessageHandler::UpdateBatteryPercent, |
| 136 base::Unretained(this))); | 160 base::Unretained(this))); |
| 137 web_ui()->RegisterMessageCallback( | 161 web_ui()->RegisterMessageCallback( |
| 162 kUpdateBatteryState, | |
| 163 base::Bind(&DeviceEmulatorMessageHandler::UpdateBatteryState, | |
| 164 base::Unretained(this))); | |
| 165 web_ui()->RegisterMessageCallback( | |
| 138 kUpdateExternalPower, | 166 kUpdateExternalPower, |
| 139 base::Bind(&DeviceEmulatorMessageHandler::UpdateExternalPower, | 167 base::Bind(&DeviceEmulatorMessageHandler::UpdateExternalPower, |
| 140 base::Unretained(this))); | 168 base::Unretained(this))); |
| 141 web_ui()->RegisterMessageCallback( | 169 web_ui()->RegisterMessageCallback( |
| 142 kUpdateTimeToEmpty, | 170 kUpdateTimeToEmpty, |
| 143 base::Bind(&DeviceEmulatorMessageHandler::UpdateTimeToEmpty, | 171 base::Bind(&DeviceEmulatorMessageHandler::UpdateTimeToEmpty, |
| 144 base::Unretained(this))); | 172 base::Unretained(this))); |
| 145 web_ui()->RegisterMessageCallback( | 173 web_ui()->RegisterMessageCallback( |
| 146 kUpdateTimeToFull, | 174 kUpdateTimeToFull, |
| 147 base::Bind(&DeviceEmulatorMessageHandler::UpdateTimeToFull, | 175 base::Bind(&DeviceEmulatorMessageHandler::UpdateTimeToFull, |
| 148 base::Unretained(this))); | 176 base::Unretained(this))); |
| 149 web_ui()->RegisterMessageCallback( | 177 web_ui()->RegisterMessageCallback( |
| 150 kBluetoothDiscoverFunction, | 178 kBluetoothDiscoverFunction, |
| 151 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover, | 179 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothDiscover, |
| 152 base::Unretained(this))); | 180 base::Unretained(this))); |
| 153 web_ui()->RegisterMessageCallback( | 181 web_ui()->RegisterMessageCallback( |
| 154 kBluetoothPairFunction, | 182 kBluetoothPairFunction, |
| 155 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair, | 183 base::Bind(&DeviceEmulatorMessageHandler::HandleRequestBluetoothPair, |
| 156 base::Unretained(this))); | 184 base::Unretained(this))); |
| 157 } | 185 } |
| OLD | NEW |