OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ |
| 6 #define EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <memory> |
| 10 |
| 11 #include "base/macros.h" |
| 12 #include "base/threading/thread_checker.h" |
| 13 #include "components/keyed_service/core/keyed_service.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 #include "extensions/browser/api/cast_channel/cast_socket.h" |
| 16 |
| 17 namespace extensions { |
| 18 namespace api { |
| 19 namespace cast_channel { |
| 20 |
| 21 // This class adds, removes, and returns cast sockets created by CastChannelAPI |
| 22 // to underlying storage. |
| 23 // This class is not thread safe. All methods must be called from the IO thread. |
| 24 class CastSocketRegistry { |
| 25 public: |
| 26 CastSocketRegistry(); |
| 27 ~CastSocketRegistry(); |
| 28 |
| 29 // Adds |socket| to |sockets_| and returns the new channel_id. Takes ownership |
| 30 // of |socket|. |
| 31 int AddSocket(std::unique_ptr<CastSocket> socket); |
| 32 |
| 33 // Removes the CastSocket corresponding to |channel_id| from the |
| 34 // CastSocketRegistry. Returns nullptr if no such CastSocket exists. |
| 35 std::unique_ptr<CastSocket> RemoveSocket(int channel_id); |
| 36 |
| 37 // Returns the socket corresponding to |channel_id| if one exists, or nullptr |
| 38 // otherwise. |
| 39 CastSocket* GetSocket(int channel_id) const; |
| 40 |
| 41 private: |
| 42 // Used to generate CastSocket id. |
| 43 static int last_channel_id_; |
| 44 |
| 45 // The collection of CastSocket keyed by channel_id. |
| 46 std::map<int, std::unique_ptr<CastSocket>> sockets_; |
| 47 |
| 48 THREAD_CHECKER(thread_checker_); |
| 49 |
| 50 DISALLOW_COPY_AND_ASSIGN(CastSocketRegistry); |
| 51 }; |
| 52 |
| 53 // This class associates underlying CastSocketRegistry instance with |
| 54 // BrowserContext and makes sure CastSocketRegistry instance is created and |
| 55 // destroyed on the IO thread. |
| 56 // Instance of this class is created and destroyed on the UI thread. |
| 57 class CastSocketService : public KeyedService { |
| 58 public: |
| 59 CastSocketService(); |
| 60 ~CastSocketService() override; |
| 61 |
| 62 // Creates CastSocketRegistry instance if none exists. Should be called on the |
| 63 // IO thread. |
| 64 CastSocketRegistry* GetOrCreateSocketRegistry(); |
| 65 |
| 66 private: |
| 67 std::unique_ptr<CastSocketRegistry, content::BrowserThread::DeleteOnIOThread> |
| 68 sockets_; |
| 69 |
| 70 DISALLOW_COPY_AND_ASSIGN(CastSocketService); |
| 71 }; |
| 72 |
| 73 } // namespace cast_channel |
| 74 } // namespace api |
| 75 } // namespace extensions |
| 76 |
| 77 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ |
OLD | NEW |