1 // 2 // Copyright (C) 2015 Google, Inc. 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 #pragma once 18 19 #include <atomic> 20 21 #include <base/files/file_path.h> 22 #include <base/files/scoped_file.h> 23 #include <base/macros.h> 24 #include <base/threading/thread.h> 25 26 #include "service/ipc/ipc_handler.h" 27 #include "service/ipc/ipc_manager.h" 28 29 namespace base { 30 class SingleThreadTaskRunner; 31 } // namespace base 32 33 namespace ipc { 34 35 // Implements a Linux sequential packet domain-socket based IPCHandler 36 class IPCHandlerLinux : public IPCHandler { 37 public: 38 IPCHandlerLinux(bluetooth::Adapter* adapter, 39 IPCManager::Delegate* delegate); 40 ~IPCHandlerLinux() override; 41 42 // IPCHandler overrides: 43 bool Run() override; 44 void Stop() override; 45 46 private: 47 IPCHandlerLinux() = default; 48 49 // Starts listening for incoming connections. Posted on |thread_| by Run(). 50 void StartListeningOnThread(); 51 52 // Stops the IPC thread. This helper is needed since base::Thread requires 53 // threads to be stopped on the thread that started them. 54 void ShutDownOnOriginThread(); 55 56 // Notifies the delegate that we started or stoppedlistening for incoming 57 // connections. 58 void NotifyStartedOnOriginThread(); 59 void NotifyStartedOnCurrentThread(); 60 void NotifyStoppedOnOriginThread(); 61 void NotifyStoppedOnCurrentThread(); 62 63 // True, if the IPC mechanism is running. 64 bool running_; 65 66 // The server socket on which we listen to incoming connections. 67 base::ScopedFD socket_; 68 69 // The file path to |socket_|. This is only set if we create and manage the 70 // life time of the socket. 71 base::FilePath socket_path_; 72 73 // We use a dedicated thread for listening to incoming connections and 74 // polling from the socket to avoid blocking the main thread. 75 base::Thread thread_; 76 77 // Whether or not the listening thread should continue to run. 78 std::atomic<bool> keep_running_; 79 80 // The origin thread's task runner. 81 scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; 82 83 DISALLOW_COPY_AND_ASSIGN(IPCHandlerLinux); 84 }; 85 86 } // namespace ipc 87