Home | History | Annotate | Download | only in trunks
      1 //
      2 // Copyright (C) 2014 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 #ifndef TRUNKS_TRUNKS_DBUS_PROXY_H_
     18 #define TRUNKS_TRUNKS_DBUS_PROXY_H_
     19 
     20 #include <string>
     21 
     22 #include <base/memory/weak_ptr.h>
     23 #include <base/threading/platform_thread.h>
     24 #include <dbus/bus.h>
     25 #include <dbus/object_proxy.h>
     26 
     27 #include "trunks/command_transceiver.h"
     28 #include "trunks/trunks_export.h"
     29 
     30 namespace trunks {
     31 
     32 // TrunksDBusProxy is a CommandTransceiver implementation that forwards all
     33 // commands to the trunksd D-Bus daemon. See TrunksDBusService for details on
     34 // how the commands are handled once they reach trunksd. A TrunksDBusProxy
     35 // instance must be used in only one thread.
     36 class TRUNKS_EXPORT TrunksDBusProxy: public CommandTransceiver {
     37  public:
     38   TrunksDBusProxy();
     39   ~TrunksDBusProxy() override;
     40 
     41   // Initializes the D-Bus client. Returns true on success.
     42   bool Init() override;
     43 
     44   // CommandTransceiver methods.
     45   void SendCommand(const std::string& command,
     46                    const ResponseCallback& callback) override;
     47   std::string SendCommandAndWait(const std::string& command) override;
     48 
     49  private:
     50   base::WeakPtr<TrunksDBusProxy> GetWeakPtr() {
     51     return weak_factory_.GetWeakPtr();
     52   }
     53 
     54   base::PlatformThreadId origin_thread_id_;
     55   scoped_refptr<dbus::Bus> bus_;
     56   dbus::ObjectProxy* object_proxy_;
     57 
     58   // Declared last so weak pointers are invalidated first on destruction.
     59   base::WeakPtrFactory<TrunksDBusProxy> weak_factory_;
     60 
     61   DISALLOW_COPY_AND_ASSIGN(TrunksDBusProxy);
     62 };
     63 
     64 }  // namespace trunks
     65 
     66 #endif  // TRUNKS_TRUNKS_DBUS_PROXY_H_
     67