cast_session_id_map.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2018 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. #ifndef CHROMECAST_BROWSER_CAST_SESSION_ID_MAP_H_
  5. #define CHROMECAST_BROWSER_CAST_SESSION_ID_MAP_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/containers/flat_map.h"
  10. #include "base/no_destructor.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/task/sequenced_task_runner.h"
  13. #include "base/unguessable_token.h"
  14. #include "chromecast/media/audio/cast_audio_manager_helper.h"
  15. namespace base {
  16. class SequencedTaskRunner;
  17. } // namespace base
  18. namespace content {
  19. class WebContents;
  20. } // namespace content
  21. namespace chromecast {
  22. namespace shell {
  23. class CastSessionIdMap : public media::CastAudioManagerHelper::Delegate {
  24. public:
  25. // Retrieve the map instance. The first time this is called, a task runner can
  26. // be specified for the instance to run on. Any subsequent calls to this
  27. // function will return the same map instance, but will not change the task
  28. // runner.
  29. // This must be called for the first time on the browser main thread.
  30. static CastSessionIdMap* GetInstance(
  31. base::SequencedTaskRunner* task_runner = nullptr);
  32. CastSessionIdMap(const CastSessionIdMap&) = delete;
  33. CastSessionIdMap& operator=(const CastSessionIdMap&) = delete;
  34. // Map a session id to a particular group id in the provided WebContents.
  35. // Record whether the session is an audio only session.
  36. // Can be called on any thread.
  37. void SetAppProperties(std::string session_id,
  38. bool is_audio_app,
  39. content::WebContents* web_contents);
  40. // Record whether the session is launched in a group.
  41. // Can be called on any thread.
  42. void SetGroupInfo(std::string session_id, bool is_group);
  43. // CastAudioManagerHelper::Delegate implementation:
  44. // Fetch the session id that is mapped to the provided group_id. Defaults to
  45. // empty string if the mapping is not found.
  46. // Must be called on the sequence for |task_runner_|.
  47. std::string GetSessionId(const std::string& group_id) override;
  48. // Fetch whether the session is an audio only session based on the provided
  49. // session id. Defaults to false if the mapping is not found.
  50. // Must be called on the sequence for |task_runner_|.
  51. bool IsAudioOnlySession(const std::string& session_id) override;
  52. // Fetch whether the session is launched in a group based on the provided
  53. // session id. Defaults to false if the mapping is not found.
  54. // Must be called on the sequence for |task_runner_|.
  55. bool IsGroup(const std::string& session_id) override;
  56. private:
  57. class GroupObserver;
  58. friend class base::NoDestructor<CastSessionIdMap>;
  59. explicit CastSessionIdMap(base::SequencedTaskRunner* task_runner);
  60. ~CastSessionIdMap() override;
  61. // Callback for the group being destroyed.
  62. void OnGroupDestroyed(base::UnguessableToken group_id);
  63. // Removes the mapping between group_id and session_id and release the
  64. // GroupObserver. This must not be called in the group destructor callback,
  65. // because it releases the GroupObserver who owns the destuctor callback.
  66. void RemoveGroupId(base::UnguessableToken group_id);
  67. // Maps the session id for the provided group id.
  68. // Record whether the session is an audio only session.
  69. // This call be called on any thread.
  70. void SetAppPropertiesInternal(std::string session_id,
  71. bool is_audio_app,
  72. base::UnguessableToken group_id,
  73. std::unique_ptr<GroupObserver> group_observer);
  74. base::flat_map<
  75. std::string,
  76. std::pair<std::string /* group_id */, std::unique_ptr<GroupObserver>>>
  77. mapping_;
  78. base::flat_map<std::string /* session_id */, bool /* is_audio_app */>
  79. application_capability_mapping_;
  80. base::flat_map<std::string /* session_id */, bool /* is_group */>
  81. group_info_mapping_;
  82. base::SequencedTaskRunner* const task_runner_;
  83. SEQUENCE_CHECKER(sequence_checker_);
  84. };
  85. } // namespace shell
  86. } // namespace chromecast
  87. #endif // CHROMECAST_BROWSER_CAST_SESSION_ID_MAP_H_