Home | History | Annotate | Download | only in trunks
      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 #ifndef TRUNKS_BACKGROUND_COMMAND_TRANSCEIVER_H_
     18 #define TRUNKS_BACKGROUND_COMMAND_TRANSCEIVER_H_
     19 
     20 #include "trunks/command_transceiver.h"
     21 
     22 #include <string>
     23 
     24 #include <base/memory/ref_counted.h>
     25 #include <base/memory/weak_ptr.h>
     26 #include <base/sequenced_task_runner.h>
     27 
     28 #include "trunks/trunks_export.h"
     29 
     30 namespace trunks {
     31 
     32 // Sends commands to another CommandTransceiver on a background thread. Response
     33 // callbacks are called on the original calling thread.
     34 // Example:
     35 //   base::Thread background_thread("my thread");
     36 //   ...
     37 //   BackgroundCommandTransceiver background_transceiver(
     38 //       next_transceiver,
     39 //       background_thread.message_loop_proxy());
     40 //   ...
     41 //   background_transceiver.SendCommand(my_command, MyCallback);
     42 class TRUNKS_EXPORT BackgroundCommandTransceiver : public CommandTransceiver {
     43  public:
     44   // All commands will be forwarded to |next_transceiver| on |task_runner|,
     45   // regardless of whether the synchronous or asynchronous method is used. This
     46   // class will hold a reference count to |task_runner|. If |task_runner| is
     47   // nullptr, all commands will be forwarded on the current thread. This class
     48   // does not take ownership of |next_transceiver|; it must remain valid for
     49   // the lifetime of the object.
     50   explicit BackgroundCommandTransceiver(
     51       CommandTransceiver* next_transceiver,
     52       const scoped_refptr<base::SequencedTaskRunner>& task_runner);
     53   ~BackgroundCommandTransceiver() override;
     54 
     55   // CommandTranceiver methods.
     56   void SendCommand(const std::string& command,
     57                    const ResponseCallback& callback) override;
     58   std::string SendCommandAndWait(const std::string& command) override;
     59 
     60  private:
     61   // Sends a |command| to the |next_transceiver_| and invokes a |callback| with
     62   // the command response.
     63   void SendCommandTask(const std::string& command,
     64                        const ResponseCallback& callback);
     65 
     66   base::WeakPtr<BackgroundCommandTransceiver> GetWeakPtr() {
     67     return weak_factory_.GetWeakPtr();
     68   }
     69 
     70   CommandTransceiver* next_transceiver_;
     71   scoped_refptr<base::SequencedTaskRunner> task_runner_;
     72 
     73   // Declared last so weak pointers are invalidated first on destruction.
     74   base::WeakPtrFactory<BackgroundCommandTransceiver> weak_factory_;
     75 
     76   DISALLOW_COPY_AND_ASSIGN(BackgroundCommandTransceiver);
     77 };
     78 
     79 }  // namespace trunks
     80 
     81 #endif  // TRUNKS_BACKGROUND_COMMAND_TRANSCEIVER_H_
     82