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/refcounted_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 // Instance of this class is created on the UI thread and destroyed on the IO | |
24 // thread. All public API must be called from the IO thread. | |
25 class CastSocketService : public RefcountedKeyedService { | |
26 public: | |
27 CastSocketService(); | |
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 ~CastSocketService() override; | |
43 | |
44 // RefcountedKeyedService implementation. | |
45 void ShutdownOnUIThread() override; | |
46 | |
47 // Used to generate CastSocket id. | |
48 static int last_channel_id_; | |
49 | |
50 // The collection of CastSocket keyed by channel_id. | |
51 std::map<int, std::unique_ptr<CastSocket>> sockets_; | |
52 | |
53 THREAD_CHECKER(thread_checker_); | |
54 | |
55 DISALLOW_COPY_AND_ASSIGN(CastSocketService); | |
56 }; | |
57 | |
58 } // namespace cast_channel | |
59 } // namespace api | |
60 } // namespace extensions | |
61 | |
62 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ | |
OLD | NEW |