 Chromium Code Reviews
 Chromium Code Reviews Issue 2891023002:
  [cast_channel] Make CastSocket not inherit from ApiResource  (Closed)
    
  
    Issue 2891023002:
  [cast_channel] Make CastSocket not inherit from ApiResource  (Closed) 
  | 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 null | |
| 
mark a. foltz
2017/05/24 17:44:10
nit: nullptr
 
zhaobin
2017/05/24 18:12:58
Done.
 | |
| 38 // otherwise. | |
| 39 CastSocket* GetSocket(int channel_id) const; | |
| 40 | |
| 41 private: | |
| 42 // Used to generate CastSocket id. | |
| 43 static int id_; | |
| 
mark a. foltz
2017/05/24 17:44:10
last_channel_id_
 
zhaobin
2017/05/24 18:12:58
Done.
 | |
| 44 | |
| 45 // The collection of CastSocket. | |
| 
mark a. foltz
2017/05/24 17:44:10
.. keyed by channel_id
 
zhaobin
2017/05/24 18:12:58
Done.
 | |
| 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 class CastSocketService : public KeyedService { | |
| 57 public: | |
| 58 CastSocketService(); | |
| 59 ~CastSocketService() override; | |
| 60 | |
| 61 // Creates CastSocketRegistry instance if none exists. Should be called on the | |
| 62 // IO thread. | |
| 63 CastSocketRegistry* GetOrCreateSocketRegistry(); | |
| 64 | |
| 65 private: | |
| 66 std::unique_ptr<CastSocketRegistry, content::BrowserThread::DeleteOnIOThread> | |
| 67 sockets_; | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(CastSocketService); | |
| 70 }; | |
| 71 | |
| 72 } // namespace cast_channel | |
| 73 } // namespace api | |
| 74 } // namespace extensions | |
| 75 | |
| 76 #endif // EXTENSIONS_BROWSER_API_CAST_CHANNEL_CAST_CHANNEL_SERVICE_H_ | |
| OLD | NEW |