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