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