Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/media_internals_ui.h" | 5 #include "chrome/browser/ui/webui/media_internals_ui.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted_memory.h" | 7 #include "base/memory/ref_counted_memory.h" |
| 8 #include "base/values.h" | 8 #include "base/values.h" |
| 9 #include "chrome/browser/browser_process.h" | |
| 10 #include "chrome/browser/io_thread.h" | |
| 11 #include "chrome/browser/media/media_internals.h" | |
| 12 #include "chrome/browser/media/media_internals_observer.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 14 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 11 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 15 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 12 #include "chrome/common/jstemplate_builder.h" | 16 #include "chrome/common/jstemplate_builder.h" |
| 13 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "content/browser/renderer_host/render_view_host.h" | |
| 14 #include "grit/browser_resources.h" | 19 #include "grit/browser_resources.h" |
| 15 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
| 16 | 21 |
| 17 namespace { | 22 namespace { |
| 18 | 23 |
| 24 class MediaInternalsProxy; | |
| 25 | |
| 19 class MediaInternalsHTMLSource : public ChromeURLDataManager::DataSource { | 26 class MediaInternalsHTMLSource : public ChromeURLDataManager::DataSource { |
| 20 public: | 27 public: |
| 21 MediaInternalsHTMLSource(); | 28 MediaInternalsHTMLSource(); |
| 22 | 29 |
| 23 // Called when the network layer has requested a resource underneath | 30 // Called when the network layer has requested a resource underneath |
| 24 // the path we registered. | 31 // the path we registered. |
| 25 virtual void StartDataRequest(const std::string& path, | 32 virtual void StartDataRequest(const std::string& path, |
| 26 bool is_incognito, | 33 bool is_incognito, |
| 27 int request_id); | 34 int request_id); |
| 28 virtual std::string GetMimeType(const std::string& path) const; | 35 virtual std::string GetMimeType(const std::string& path) const; |
| 29 | 36 |
| 30 private: | 37 private: |
| 31 virtual ~MediaInternalsHTMLSource() {} | 38 virtual ~MediaInternalsHTMLSource() {} |
| 32 DISALLOW_COPY_AND_ASSIGN(MediaInternalsHTMLSource); | 39 DISALLOW_COPY_AND_ASSIGN(MediaInternalsHTMLSource); |
| 33 }; | 40 }; |
| 34 | 41 |
| 42 // This class handles messages to and from MediaInternalsUI. | |
| 43 // It does all its work on the IO thread through the proxy below. | |
| 44 class MediaInternalsMessageHandler : public WebUIMessageHandler { | |
|
Evan Stade
2011/06/30 22:24:27
please do break this into a separate file. net_int
Scott Franklin
2011/07/01 01:42:32
Alright, split and filed (directoried?). I'm assum
| |
| 45 public: | |
| 46 MediaInternalsMessageHandler(); | |
| 47 virtual ~MediaInternalsMessageHandler(); | |
| 48 | |
| 49 // WebUIMessageHandler implementation. | |
| 50 virtual WebUIMessageHandler* Attach(WebUI* web_ui); | |
| 51 virtual void RegisterMessages(); | |
| 52 | |
| 53 // Javascript message handlers. | |
| 54 void OnGetEverything(const ListValue* list); | |
| 55 | |
| 56 // MediaInternals message handlers. | |
| 57 void OnUpdate(const string16& update); | |
| 58 | |
| 59 private: | |
| 60 scoped_refptr<MediaInternalsProxy> proxy_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(MediaInternalsMessageHandler); | |
| 63 }; | |
| 64 | |
| 65 // This class is a proxy between MediaInternals (on the IO thread) and | |
| 66 // MediaInternalsMessageHandler (on the UI thread). | |
| 67 // It is ref_counted to ensure that it completes all pending Tasks on both | |
| 68 // threads before destruction. | |
| 69 class MediaInternalsProxy | |
| 70 : public MediaInternalsObserver, | |
| 71 public base::RefCountedThreadSafe<MediaInternalsProxy> { | |
| 72 public: | |
| 73 MediaInternalsProxy(); | |
| 74 | |
| 75 // Register a Handler and start receiving callbacks from MediaInternals. | |
| 76 void Attach(MediaInternalsMessageHandler* handler); | |
| 77 | |
| 78 // Unregister the same and stop receiving callbacks. | |
| 79 void Detach(); | |
| 80 | |
| 81 // Have MediaInternals send all the data it has. | |
| 82 void GetEverything(); | |
| 83 | |
| 84 // MediaInternalsObserver implementation. Called on the IO thread. | |
| 85 virtual void OnUpdate(const string16& update); | |
| 86 | |
| 87 private: | |
| 88 friend class base::RefCountedThreadSafe<MediaInternalsProxy>; | |
| 89 virtual ~MediaInternalsProxy(); | |
| 90 | |
| 91 void ObserveMediaInternalsOnIOThread(); | |
| 92 void StopObservingMediaInternalsOnIOThread(); | |
| 93 void GetEverythingOnIOThread(); | |
| 94 void UpdateUIOnUIThread(const string16& update); | |
| 95 | |
| 96 MediaInternalsMessageHandler* handler_; | |
| 97 IOThread* io_thread_; | |
| 98 | |
| 99 DISALLOW_COPY_AND_ASSIGN(MediaInternalsProxy); | |
| 100 }; | |
| 101 | |
| 35 //////////////////////////////////////////////////////////////////////////////// | 102 //////////////////////////////////////////////////////////////////////////////// |
| 36 // | 103 // |
| 37 // MediaInternalsHTMLSource | 104 // MediaInternalsHTMLSource |
| 38 // | 105 // |
| 39 //////////////////////////////////////////////////////////////////////////////// | 106 //////////////////////////////////////////////////////////////////////////////// |
| 40 | 107 |
| 41 MediaInternalsHTMLSource::MediaInternalsHTMLSource() | 108 MediaInternalsHTMLSource::MediaInternalsHTMLSource() |
| 42 : DataSource(chrome::kChromeUIMediaInternalsHost, MessageLoop::current()) { | 109 : DataSource(chrome::kChromeUIMediaInternalsHost, MessageLoop::current()) { |
| 43 } | 110 } |
| 44 | 111 |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 62 std::copy(html.begin(), html.end(), bytes->data.begin()); | 129 std::copy(html.begin(), html.end(), bytes->data.begin()); |
| 63 | 130 |
| 64 SendResponse(request_id, bytes); | 131 SendResponse(request_id, bytes); |
| 65 } | 132 } |
| 66 | 133 |
| 67 std::string MediaInternalsHTMLSource::GetMimeType( | 134 std::string MediaInternalsHTMLSource::GetMimeType( |
| 68 const std::string& path) const { | 135 const std::string& path) const { |
| 69 return "text/html"; | 136 return "text/html"; |
| 70 } | 137 } |
| 71 | 138 |
| 72 } // namespace | 139 //////////////////////////////////////////////////////////////////////////////// |
| 140 // | |
| 141 // MediaInternalsMessageHandler | |
| 142 // | |
| 143 //////////////////////////////////////////////////////////////////////////////// | |
| 73 | 144 |
| 145 MediaInternalsMessageHandler::MediaInternalsMessageHandler() | |
| 146 : proxy_(new MediaInternalsProxy()) {} | |
| 147 | |
| 148 MediaInternalsMessageHandler::~MediaInternalsMessageHandler() { | |
| 149 proxy_->Detach(); | |
| 150 } | |
| 151 | |
| 152 WebUIMessageHandler* MediaInternalsMessageHandler::Attach(WebUI* web_ui) { | |
| 153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 154 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); | |
| 155 proxy_->Attach(this); | |
| 156 return result; | |
| 157 } | |
| 158 | |
| 159 void MediaInternalsMessageHandler::RegisterMessages() { | |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 161 | |
| 162 web_ui_->RegisterMessageCallback( | |
| 163 "getEverything", | |
| 164 NewCallback(this, &MediaInternalsMessageHandler::OnGetEverything)); | |
| 165 } | |
| 166 | |
| 167 void MediaInternalsMessageHandler::OnGetEverything(const ListValue* list) { | |
| 168 proxy_->GetEverything(); | |
| 169 } | |
| 170 | |
| 171 void MediaInternalsMessageHandler::OnUpdate(const string16& update) { | |
| 172 web_ui_->GetRenderViewHost()->ExecuteJavascriptInWebFrame(string16(), update); | |
| 173 } | |
| 74 | 174 |
| 75 //////////////////////////////////////////////////////////////////////////////// | 175 //////////////////////////////////////////////////////////////////////////////// |
| 76 // | 176 // |
| 177 // MediaInternalsProxy | |
| 178 // | |
| 179 //////////////////////////////////////////////////////////////////////////////// | |
| 180 | |
| 181 MediaInternalsProxy::MediaInternalsProxy() { | |
| 182 io_thread_ = g_browser_process->io_thread(); | |
| 183 } | |
| 184 | |
| 185 void MediaInternalsProxy::Attach(MediaInternalsMessageHandler* handler) { | |
| 186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 187 handler_ = handler; | |
| 188 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 189 NewRunnableMethod(this, | |
| 190 &MediaInternalsProxy::ObserveMediaInternalsOnIOThread)); | |
| 191 } | |
| 192 | |
| 193 void MediaInternalsProxy::Detach() { | |
| 194 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 195 handler_ = NULL; | |
| 196 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 197 NewRunnableMethod(this, | |
| 198 &MediaInternalsProxy::StopObservingMediaInternalsOnIOThread)); | |
| 199 } | |
| 200 | |
| 201 void MediaInternalsProxy::GetEverything() { | |
| 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 203 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 204 NewRunnableMethod(this, &MediaInternalsProxy::GetEverythingOnIOThread)); | |
| 205 } | |
| 206 | |
| 207 void MediaInternalsProxy::OnUpdate(const string16& update) { | |
| 208 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 209 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 210 NewRunnableMethod(this, | |
| 211 &MediaInternalsProxy::UpdateUIOnUIThread, update)); | |
| 212 } | |
| 213 | |
| 214 MediaInternalsProxy::~MediaInternalsProxy() {} | |
| 215 | |
| 216 void MediaInternalsProxy::ObserveMediaInternalsOnIOThread() { | |
| 217 io_thread_->globals()->media.media_internals->AddUI(this); | |
| 218 } | |
| 219 | |
| 220 void MediaInternalsProxy::StopObservingMediaInternalsOnIOThread() { | |
| 221 io_thread_->globals()->media.media_internals->RemoveUI(this); | |
| 222 } | |
| 223 | |
| 224 void MediaInternalsProxy::GetEverythingOnIOThread() { | |
| 225 io_thread_->globals()->media.media_internals->SendEverything(); | |
| 226 } | |
| 227 | |
| 228 void MediaInternalsProxy::UpdateUIOnUIThread(const string16& update) { | |
| 229 // Don't forward updates to a destructed UI. | |
| 230 if (handler_) | |
| 231 handler_->OnUpdate(update); | |
| 232 } | |
| 233 | |
| 234 } // namespace | |
| 235 | |
| 236 //////////////////////////////////////////////////////////////////////////////// | |
| 237 // | |
| 77 // MediaInternalsUI | 238 // MediaInternalsUI |
| 78 // | 239 // |
| 79 //////////////////////////////////////////////////////////////////////////////// | 240 //////////////////////////////////////////////////////////////////////////////// |
| 80 | 241 |
| 81 MediaInternalsUI::MediaInternalsUI(TabContents* contents) | 242 MediaInternalsUI::MediaInternalsUI(TabContents* contents) |
| 82 : ChromeWebUI(contents) { | 243 : ChromeWebUI(contents) { |
| 244 AddMessageHandler((new MediaInternalsMessageHandler())->Attach(this)); | |
| 245 | |
| 83 contents->profile()->GetChromeURLDataManager()->AddDataSource( | 246 contents->profile()->GetChromeURLDataManager()->AddDataSource( |
| 84 new MediaInternalsHTMLSource()); | 247 new MediaInternalsHTMLSource()); |
| 85 } | 248 } |
| 86 | |
| OLD | NEW |