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"
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
<
31 BrowserThread::DeleteOnUIThread
>,
32 public net::NetLog::ThreadSafeObserver
,
33 public NotificationObserver
{
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.
48 // Have MediaInternals send all the data it has.
51 // net::NetLog::ThreadSafeObserver implementation. Callable from any thread:
52 void OnAddEntry(const net::NetLog::Entry
& entry
) override
;
55 friend struct BrowserThread::DeleteOnThread
<BrowserThread::UI
>;
56 friend class base::DeleteHelper
<MediaInternalsProxy
>;
57 ~MediaInternalsProxy() override
;
59 // Build a dictionary mapping constant names to values.
60 base::Value
* GetConstants();
62 void ObserveMediaInternalsOnIOThread();
63 void StopObservingMediaInternalsOnIOThread();
64 void GetEverythingOnIOThread();
66 // Callback for MediaInternals to update. Must be called on UI thread.
67 void UpdateUIOnUIThread(const base::string16
& update
);
69 // Put |entry| on a list of events to be sent to the page.
70 void AddNetEventOnUIThread(base::Value
* entry
);
72 // Send all pending events to the page.
73 void SendNetEventsOnUIThread();
75 // Call a JavaScript function on the page. Takes ownership of |args|.
76 void CallJavaScriptFunctionOnUIThread(const std::string
& function
,
79 MediaInternalsMessageHandler
* handler_
;
80 scoped_ptr
<base::ListValue
> pending_net_updates_
;
81 NotificationRegistrar registrar_
;
82 MediaInternals::UpdateCallback update_callback_
;
84 DISALLOW_COPY_AND_ASSIGN(MediaInternalsProxy
);
87 } // namespace content
89 #endif // CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_PROXY_H_