in_memory_video_decode_stats_db_impl.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 MEDIA_CAPABILITIES_IN_MEMORY_VIDEO_DECODE_STATS_DB_IMPL_H_
  5. #define MEDIA_CAPABILITIES_IN_MEMORY_VIDEO_DECODE_STATS_DB_IMPL_H_
  6. #include <map>
  7. #include <memory>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "components/leveldb_proto/public/proto_database.h"
  11. #include "media/base/media_export.h"
  12. #include "media/base/video_codecs.h"
  13. #include "media/capabilities/video_decode_stats_db.h"
  14. #include "ui/gfx/geometry/size.h"
  15. namespace media {
  16. class VideoDecodeStatsDBProvider;
  17. // The in-memory database disappears with profile shutdown to preserve the
  18. // privacy of off-the-record (OTR) browsing profiles (Guest and Incognito). It
  19. // also allows the MediaCapabilities API to behave the same both on and
  20. // off-the-record which prevents sites from detecting when users are OTR modes.
  21. // VideoDecodeStatsDBProvider gives incognito profiles a hook to read the stats
  22. // of the of the originating profile. Guest profiles are conceptually a blank
  23. // slate and will not have a "seed" DB.
  24. class MEDIA_EXPORT InMemoryVideoDecodeStatsDBImpl : public VideoDecodeStatsDB {
  25. public:
  26. // |seed_db_provider| provides access to a seed (read-only) DB instance.
  27. // Callers must ensure the |seed_db_provider| outlives this factory and any
  28. // databases it creates via CreateDB(). |seed_db_provider| may be null when no
  29. // seed DB is available.
  30. explicit InMemoryVideoDecodeStatsDBImpl(
  31. VideoDecodeStatsDBProvider* seed_db_provider);
  32. InMemoryVideoDecodeStatsDBImpl(const InMemoryVideoDecodeStatsDBImpl&) =
  33. delete;
  34. InMemoryVideoDecodeStatsDBImpl& operator=(
  35. const InMemoryVideoDecodeStatsDBImpl&) = delete;
  36. ~InMemoryVideoDecodeStatsDBImpl() override;
  37. // Implement VideoDecodeStatsDB.
  38. void Initialize(InitializeCB init_cb) override;
  39. void AppendDecodeStats(const VideoDescKey& key,
  40. const DecodeStatsEntry& entry,
  41. AppendDecodeStatsCB append_done_cb) override;
  42. void GetDecodeStats(const VideoDescKey& key,
  43. GetDecodeStatsCB get_stats_cb) override;
  44. void ClearStats(base::OnceClosure destroy_done_cb) override;
  45. private:
  46. // Called when the |seed_db_provider_| returns an initialized seed DB. Will
  47. // run |init_cb|, marking the completion of Initialize().
  48. void OnGotSeedDB(base::OnceCallback<void(bool)> init_cb,
  49. VideoDecodeStatsDB* seed_db);
  50. // Passed as the callback for |OnGotDecodeStats| by |AppendDecodeStats| to
  51. // update the database once we've read the existing stats entry.
  52. void CompleteAppendWithSeedData(const VideoDescKey& key,
  53. const DecodeStatsEntry& entry,
  54. AppendDecodeStatsCB append_done_cb,
  55. bool read_success,
  56. std::unique_ptr<DecodeStatsEntry> seed_entry);
  57. // Called when GetDecodeStats() operation was performed. |get_stats_cb|
  58. // will be run with |success| and a |DecodeStatsEntry| created from
  59. // |stats_proto| or nullptr if no entry was found for the requested key.
  60. void OnGotSeedEntry(const VideoDescKey& key,
  61. GetDecodeStatsCB get_stats_cb,
  62. bool success,
  63. std::unique_ptr<DecodeStatsEntry> seed_entry);
  64. // Indicates whether initialization is completed.
  65. bool db_init_ = false;
  66. // Lazily provides |seed_db_| from original profile. Owned by original profile
  67. // and may be null.
  68. raw_ptr<VideoDecodeStatsDBProvider> seed_db_provider_ = nullptr;
  69. // On-disk DB owned by the base profile for the off-the-record session. For
  70. // incognito sessions, this will contain the original profile's stats. For
  71. // guest sessions, this will be null (no notion of base profile). See
  72. // |in_memory_db_|.
  73. raw_ptr<VideoDecodeStatsDB> seed_db_ = nullptr;
  74. // In-memory DB, mapping VideoDescKey strings -> DecodeStatsEntries. This is
  75. // the primary storage (read and write) for this class. The |seed_db_| is
  76. // read-only, and will only be queried when the |in_memory_db_| lacks an
  77. // entry for a given key.
  78. std::map<std::string, DecodeStatsEntry> in_memory_db_;
  79. // Ensures all access to class members come on the same sequence. API calls
  80. // and callbacks should occur on the same sequence used during construction.
  81. // LevelDB operations happen on a separate task runner, but all LevelDB
  82. // callbacks to this happen on the checked sequence.
  83. SEQUENCE_CHECKER(sequence_checker_);
  84. base::WeakPtrFactory<InMemoryVideoDecodeStatsDBImpl> weak_ptr_factory_{this};
  85. };
  86. } // namespace media
  87. #endif // MEDIA_CAPABILITIES_IN_MEMORY_VIDEO_DECODE_STATS_DB_IMPL_H_