Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / media / media_internals_proxy.h
blobaf6740c087b548c36e60d788a05539419a53a918
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.
5 #ifndef CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_PROXY_H_
6 #define CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_PROXY_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/sequenced_task_runner_helpers.h"
10 #include "content/browser/media/media_internals.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/notification_observer.h"
13 #include "content/public/browser/notification_registrar.h"
14 #include "net/log/net_log.h"
16 namespace base {
17 class ListValue;
18 class Value;
21 namespace content {
22 class MediaInternalsMessageHandler;
24 // This class is a proxy between MediaInternals (on the IO thread) and
25 // MediaInternalsMessageHandler (on the UI thread).
26 // It is ref_counted to ensure that it completes all pending Tasks on both
27 // threads before destruction.
28 class MediaInternalsProxy
29 : public base::RefCountedThreadSafe<
30 MediaInternalsProxy,
31 BrowserThread::DeleteOnUIThread>,
32 public net::NetLog::ThreadSafeObserver,
33 public NotificationObserver {
34 public:
35 MediaInternalsProxy();
37 // NotificationObserver implementation.
38 void Observe(int type,
39 const NotificationSource& source,
40 const NotificationDetails& details) override;
42 // Register a Handler and start receiving callbacks from MediaInternals.
43 void Attach(MediaInternalsMessageHandler* handler);
45 // Unregister the same and stop receiving callbacks.
46 void Detach();
48 // Have MediaInternals send all the data it has.
49 void GetEverything();
51 // MediaInternals callback. Called on the IO thread.
52 void OnUpdate(const base::string16& update);
54 // net::NetLog::ThreadSafeObserver implementation. Callable from any thread:
55 void OnAddEntry(const net::NetLog::Entry& entry) override;
57 private:
58 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
59 friend class base::DeleteHelper<MediaInternalsProxy>;
60 ~MediaInternalsProxy() override;
62 // Build a dictionary mapping constant names to values.
63 base::Value* GetConstants();
65 void ObserveMediaInternalsOnIOThread();
66 void StopObservingMediaInternalsOnIOThread();
67 void GetEverythingOnIOThread();
68 void UpdateUIOnUIThread(const base::string16& update);
70 // Put |entry| on a list of events to be sent to the page.
71 void AddNetEventOnUIThread(base::Value* entry);
73 // Send all pending events to the page.
74 void SendNetEventsOnUIThread();
76 // Call a JavaScript function on the page. Takes ownership of |args|.
77 void CallJavaScriptFunctionOnUIThread(const std::string& function,
78 base::Value* args);
80 MediaInternalsMessageHandler* handler_;
81 scoped_ptr<base::ListValue> pending_net_updates_;
82 NotificationRegistrar registrar_;
83 MediaInternals::UpdateCallback update_callback_;
85 DISALLOW_COPY_AND_ASSIGN(MediaInternalsProxy);
88 } // namespace content
90 #endif // CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_PROXY_H_