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_PROXY_H_ 6 #define CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_PROXY_H_ 7 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/base/net_log.h" 15 16 namespace base { 17 class ListValue; 18 class Value; 19 } 20 21 namespace content { 22 class MediaInternalsMessageHandler; 23 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(); 36 37 // NotificationObserver implementation. 38 virtual void Observe(int type, 39 const NotificationSource& source, 40 const NotificationDetails& details) OVERRIDE; 41 42 // Register a Handler and start receiving callbacks from MediaInternals. 43 void Attach(MediaInternalsMessageHandler* handler); 44 45 // Unregister the same and stop receiving callbacks. 46 void Detach(); 47 48 // Have MediaInternals send all the data it has. 49 void GetEverything(); 50 51 // MediaInternals callback. Called on the IO thread. 52 void OnUpdate(const base::string16& update); 53 54 // net::NetLog::ThreadSafeObserver implementation. Callable from any thread: 55 virtual void OnAddEntry(const net::NetLog::Entry& entry) OVERRIDE; 56 57 private: 58 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; 59 friend class base::DeleteHelper<MediaInternalsProxy>; 60 virtual ~MediaInternalsProxy(); 61 62 // Build a dictionary mapping constant names to values. 63 base::Value* GetConstants(); 64 65 void ObserveMediaInternalsOnIOThread(); 66 void StopObservingMediaInternalsOnIOThread(); 67 void GetEverythingOnIOThread(); 68 void UpdateUIOnUIThread(const base::string16& update); 69 70 // Put |entry| on a list of events to be sent to the page. 71 void AddNetEventOnUIThread(base::Value* entry); 72 73 // Send all pending events to the page. 74 void SendNetEventsOnUIThread(); 75 76 // Call a JavaScript function on the page. Takes ownership of |args|. 77 void CallJavaScriptFunctionOnUIThread(const std::string& function, 78 base::Value* args); 79 80 MediaInternalsMessageHandler* handler_; 81 scoped_ptr<base::ListValue> pending_net_updates_; 82 NotificationRegistrar registrar_; 83 MediaInternals::UpdateCallback update_callback_; 84 85 DISALLOW_COPY_AND_ASSIGN(MediaInternalsProxy); 86 }; 87 88 } // namespace content 89 90 #endif // CONTENT_BROWSER_MEDIA_MEDIA_INTERNALS_PROXY_H_ 91