OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/chromeos/dbus/sensors_client.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
| 9 #include "chrome/browser/chromeos/system/runtime_environment.h" |
| 10 #include "content/browser/browser_thread.h" |
| 11 #include "content/browser/sensors/sensors_provider.h" |
| 12 #include "dbus/bus.h" |
| 13 #include "dbus/message.h" |
| 14 #include "dbus/object_proxy.h" |
| 15 |
| 16 // TODO(cwolfe): Fix the DEPs so that these can be pulled in from |
| 17 // "chromeos/dbus/service_constants.h". |
| 18 namespace chromeos { |
| 19 // Sensors service identifiers. |
| 20 const char kSensorsServiceName[] = "org.chromium.Sensors"; |
| 21 const char kSensorsServicePath[] = "/org/chromium/Sensors"; |
| 22 const char kSensorsServiceInterface[] = "org.chromium.Sensors"; |
| 23 // Sensors signal names. |
| 24 const char kScreenOrientationChanged[] = "ScreenOrientationChanged"; |
| 25 |
| 26 // The SensorsClient implementation used in production. |
| 27 class SensorsClientImpl : public SensorsClient { |
| 28 public: |
| 29 explicit SensorsClientImpl(dbus::Bus* bus) |
| 30 : sensors_proxy_(NULL), |
| 31 weak_ptr_factory_(this) { |
| 32 sensors_proxy_ = bus->GetObjectProxy(chromeos::kSensorsServiceName, |
| 33 chromeos::kSensorsServicePath); |
| 34 sensors_proxy_->ConnectToSignal( |
| 35 chromeos::kSensorsServiceInterface, |
| 36 chromeos::kScreenOrientationChanged, |
| 37 base::Bind(&SensorsClientImpl::OrientationChangedReceived, |
| 38 weak_ptr_factory_.GetWeakPtr()), |
| 39 base::Bind(&SensorsClientImpl::OrientationChangedConnected, |
| 40 weak_ptr_factory_.GetWeakPtr())); |
| 41 } |
| 42 |
| 43 virtual ~SensorsClientImpl() { |
| 44 } |
| 45 |
| 46 private: |
| 47 void OrientationChangedReceived(dbus::Signal* signal) { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 49 |
| 50 sensors::ScreenOrientation orientation; |
| 51 |
| 52 dbus::MessageReader reader(signal); |
| 53 int32 upward = 0; |
| 54 if (!reader.PopInt32(&upward)) { |
| 55 LOG(WARNING) << "Orientation changed signal had incorrect parameters: " |
| 56 << signal->ToString(); |
| 57 return; |
| 58 } |
| 59 VLOG(1) << "Orientation changed to upward " << upward; |
| 60 orientation.upward = static_cast<sensors::ScreenOrientation::Side>(upward); |
| 61 |
| 62 sensors::Provider::GetInstance()->ScreenOrientationChanged(orientation); |
| 63 } |
| 64 |
| 65 void OrientationChangedConnected( |
| 66 const std::string& interface_name, |
| 67 const std::string& signal_name, |
| 68 bool success) { |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 70 if (!success) |
| 71 LOG(WARNING) << "Failed to connect to orientation changed signal."; |
| 72 } |
| 73 |
| 74 dbus::ObjectProxy* sensors_proxy_; |
| 75 base::WeakPtrFactory<SensorsClientImpl> weak_ptr_factory_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(SensorsClientImpl); |
| 78 }; |
| 79 |
| 80 // The SensorsClient implementation used on Linux desktop, |
| 81 // which does nothing. |
| 82 class SensorsClientStubImpl : public SensorsClient { |
| 83 }; |
| 84 |
| 85 SensorsClient::SensorsClient() { |
| 86 } |
| 87 |
| 88 SensorsClient::~SensorsClient() { |
| 89 } |
| 90 |
| 91 SensorsClient* SensorsClient::Create(dbus::Bus* bus) { |
| 92 if (system::runtime_environment::IsRunningOnChromeOS()) { |
| 93 return new SensorsClientImpl(bus); |
| 94 } else { |
| 95 return new SensorsClientStubImpl(); |
| 96 } |
| 97 } |
| 98 |
| 99 } // namespace chromeos |
OLD | NEW |