Chromium Code Reviews| Index: chrome/browser/extensions/api/tab_capture/tab_capture_event_router.cc |
| diff --git a/chrome/browser/extensions/api/tab_capture/tab_capture_event_router.cc b/chrome/browser/extensions/api/tab_capture/tab_capture_event_router.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f72bb400dac87b204ea711b58131426610b4994b |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/tab_capture/tab_capture_event_router.cc |
| @@ -0,0 +1,124 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/extensions/api/tab_capture/tab_capture_event_router.h" |
| + |
| +#include "base/string_number_conversions.h" |
| +#include "base/string_piece.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_dependency_manager.h" |
| +#include "chrome/browser/media/media_internals.h" |
| +#include "chrome/browser/extensions/event_names.h" |
| +#include "chrome/browser/extensions/event_router.h" |
|
Alpha Left Google
2012/10/04 20:52:01
Reorder includes.
justinlin
2012/10/08 09:58:31
Done.
|
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace events = extensions::event_names; |
| +using content::BrowserThread; |
| + |
| +namespace extensions { |
| + |
| +void RegisterAsMediaObserver(TabCaptureEventRouter* tabCaptureRouter) { |
|
Aaron Boodman
2012/10/04 07:21:43
unix_hacker_style
Aaron Boodman
2012/10/04 07:21:43
Free module-local functions should be defined in a
justinlin
2012/10/08 09:58:31
Done.
justinlin
2012/10/08 09:58:31
Done.
|
| + DCHECK(MediaInternals::GetInstance()); |
| + MediaInternals::GetInstance()->AddObserver(tabCaptureRouter); |
| +} |
| + |
| +// TODO(justinlin): This should maybe be filtered/partitioned by extension. |
| +TabCaptureEventRouter::TabCaptureEventRouter(Profile* profile) |
| + : profile_(profile) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&RegisterAsMediaObserver, this)); |
|
Alpha Left Google
2012/10/04 20:52:01
indent by 4 spaces when you wrap a line
justinlin
2012/10/08 09:58:31
Done.
|
| +} |
| + |
| +void UnRegisterAsMediaObserver(TabCaptureEventRouter* tabCaptureRouter) { |
|
Alpha Left Google
2012/10/04 20:52:01
like aa@ suggested, hacker_style.
justinlin
2012/10/08 09:58:31
Done.
|
| + if (MediaInternals::GetInstance()) |
| + MediaInternals::GetInstance()->RemoveObserver(tabCaptureRouter); |
| +} |
| + |
| +TabCaptureEventRouter::~TabCaptureEventRouter() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
| + base::Bind(&UnRegisterAsMediaObserver, this)); |
|
Aaron Boodman
2012/10/04 07:21:43
Are you sure about the lifetime issues here? It se
Alpha Left Google
2012/10/04 20:52:01
indent by 4 spaces when you wrap a line
justinlin
2012/10/08 09:58:31
Done.
justinlin
2012/10/11 07:30:14
Done, from what I can tell. Used a proxy class.
|
| +} |
| + |
| +void TabCaptureEventRouter::OnRequestUpdate( |
| + const content::MediaStreamDevice& device, |
| + const content::MediaStreamRequest::RequestState new_state) { |
| + BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| + base::Bind(&TabCaptureEventRouter::HandleRequestUpdate, |
|
Alpha Left Google
2012/10/04 20:52:01
indent by 4 spaces when you wrap a line
justinlin
2012/10/08 09:58:31
Done.
|
| + base::Unretained(this), device, new_state)); |
|
Alpha Left Google
2012/10/04 20:52:01
indent by 4 spaces when you wrap a line
justinlin
2012/10/08 09:58:31
Done.
|
| +} |
| + |
| +void TabCaptureEventRouter::HandleRequestUpdate( |
| + const content::MediaStreamDevice& device, |
| + const content::MediaStreamRequest::RequestState new_state) { |
| + EventRouter* router = profile_ ? profile_->GetExtensionEventRouter() : NULL; |
| + if (!router) |
| + return; |
| + |
| + if (device.type != content::MEDIA_TAB_VIDEO_CAPTURE && |
| + device.type != content::MEDIA_TAB_AUDIO_CAPTURE) |
| + return; |
| + |
| + // Parse out the tab_id. |
| + const size_t sep_pos = device.device_id.find(':'); |
| + if (sep_pos == std::string::npos) |
| + return; |
| + |
| + int tabId = -1; |
|
Aaron Boodman
2012/10/04 07:21:43
unix_hacker_style!
justinlin
2012/10/08 09:58:31
Done.
|
| + if (!base::StringToInt( |
| + base::StringPiece(device.device_id.data(), sep_pos), &tabId)) |
|
Alpha Left Google
2012/10/04 20:52:01
indentation is wrong
justinlin
2012/10/08 09:58:31
Done.
|
| + return; |
| + |
| + tab_capture::TabCaptureState state = |
| + tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_NONE; |
| + switch (new_state) { |
| + case content::MediaStreamRequest::STATE_REQUESTED: |
| + state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_REQUESTED; |
| + break; |
| + case content::MediaStreamRequest::STATE_PENDING_APPROVAL: |
| + state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_PENDING; |
| + break; |
| + case content::MediaStreamRequest::STATE_OPENING: |
| + state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_ACTIVE; |
| + break; |
| + case content::MediaStreamRequest::STATE_DONE: |
| + state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_STOPPED; |
| + break; |
| + case content::MediaStreamRequest::STATE_ERROR: |
| + state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_ERROR; |
| + break; |
| + default: |
| + // TODO(justinlin): Implement muted, cancelled state notification. |
| + break; |
| + } |
| + |
| + if (state == tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_NONE) { |
| + VLOG(1) << "Observed an unhandled capture state."; |
| + return; |
| + } |
| + |
| + scoped_ptr<tab_capture::CaptureInfo> info(new tab_capture::CaptureInfo()); |
| + info->tab_id = tabId; |
| + info->status = state; |
| + |
| + scoped_ptr<base::ListValue> args(new ListValue()); |
| + args->Append(info->ToValue().release()); |
| + router->DispatchEventToRenderers(events::kOnTabCaptured, |
| + args.Pass(), |
| + profile_, |
| + GURL()); |
| + |
| + if (state != tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_ERROR) |
| + requests_[tabId] = state; |
| + else |
| + requests_.erase(tabId); |
| +} |
| + |
| +std::map<int, tab_capture::TabCaptureState> |
|
Alpha Left Google
2012/10/04 20:52:01
Use a typedef it will be look better.
justinlin
2012/10/08 09:58:31
Done.
|
| + TabCaptureEventRouter::GetCapturedTabs() { |
| + return requests_; |
| +} |
| + |
| +} // namespace extensions |