1// Copyright (c) 2012 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 CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_
6#define CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_
7
8#include <string>
9#include <vector>
10
11#include "base/callback_forward.h"
12#include "base/compiler_specific.h"
13#include "base/lazy_instance.h"
14#include "base/strings/string16.h"
15#include "base/synchronization/lock.h"
16#include "base/values.h"
17#include "content/common/content_export.h"
18#include "media/audio/audio_logging.h"
19
20namespace media {
21class AudioParameters;
22struct MediaLogEvent;
23}
24
25namespace content {
26
27// This class stores information about currently active media.
28class CONTENT_EXPORT MediaInternals
29    : NON_EXPORTED_BASE(public media::AudioLogFactory) {
30 public:
31  static MediaInternals* GetInstance();
32
33  virtual ~MediaInternals();
34
35  // Called when a MediaEvent occurs.
36  void OnMediaEvents(int render_process_id,
37                     const std::vector<media::MediaLogEvent>& events);
38
39  // Called with the update string.
40  typedef base::Callback<void(const base::string16&)> UpdateCallback;
41
42  // Add/remove update callbacks (see above).  Must be called on the IO thread.
43  void AddUpdateCallback(const UpdateCallback& callback);
44  void RemoveUpdateCallback(const UpdateCallback& callback);
45
46  // Sends all cached data to each registered UpdateCallback.
47  void SendEverything();
48
49  // AudioLogFactory implementation.  Safe to call from any thread.
50  virtual scoped_ptr<media::AudioLog> CreateAudioLog(
51      AudioComponent component) OVERRIDE;
52
53 private:
54  friend class AudioLogImpl;
55  friend class MediaInternalsTest;
56  friend struct base::DefaultLazyInstanceTraits<MediaInternals>;
57
58  MediaInternals();
59
60  // Sends |update| to each registered UpdateCallback.  Safe to call from any
61  // thread, but will forward to the IO thread.
62  void SendUpdate(const base::string16& update);
63
64  // Caches |value| under |cache_key| so that future SendEverything() calls will
65  // include the current data.  Calls JavaScript |function|(|value|) for each
66  // registered UpdateCallback.  SendUpdateAndPurgeCache() is similar but purges
67  // the cache entry after completion instead.
68  void SendUpdateAndCache(const std::string& cache_key,
69                          const std::string& function,
70                          const base::DictionaryValue* value);
71  void SendUpdateAndPurgeCache(const std::string& cache_key,
72                               const std::string& function,
73                               const base::DictionaryValue* value);
74  // Must only be accessed on the IO thread.
75  std::vector<UpdateCallback> update_callbacks_;
76
77  // All variables below must be accessed under |lock_|.
78  base::Lock lock_;
79  base::DictionaryValue cached_data_;
80  int owner_ids_[AUDIO_COMPONENT_MAX];
81
82  DISALLOW_COPY_AND_ASSIGN(MediaInternals);
83};
84
85} // namespace content
86
87#endif  // CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_H_
88