OLD | NEW |
(Empty) | |
| 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 #include "chrome/browser/extensions/api/tab_capture/tab_capture_event_router.h" |
| 6 |
| 7 #include "base/string_number_conversions.h" |
| 8 #include "base/string_piece.h" |
| 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 11 #include "chrome/browser/media/media_internals.h" |
| 12 #include "chrome/browser/extensions/event_names.h" |
| 13 #include "chrome/browser/extensions/event_router.h" |
| 14 |
| 15 namespace events = extensions::event_names; |
| 16 |
| 17 namespace extensions { |
| 18 |
| 19 // TODO(justinlin): This should maybe be filtered/partitioned by extension. |
| 20 TabCaptureEventRouter::TabCaptureEventRouter(Profile* profile) |
| 21 : profile_(profile) { |
| 22 MediaInternals::GetInstance()->AddObserver(this); |
| 23 } |
| 24 |
| 25 TabCaptureEventRouter::~TabCaptureEventRouter() { |
| 26 MediaInternals::GetInstance()->RemoveObserver(this); |
| 27 } |
| 28 |
| 29 void TabCaptureEventRouter::OnRequestUpdate( |
| 30 const content::MediaStreamDevice& device, |
| 31 const content::MediaStreamRequest::RequestState new_state) { |
| 32 EventRouter* router = profile_ ? profile_->GetExtensionEventRouter() : NULL; |
| 33 if (!router) |
| 34 return; |
| 35 |
| 36 if (device.type != content::MEDIA_TAB_VIDEO_CAPTURE && |
| 37 device.type != content::MEDIA_TAB_AUDIO_CAPTURE) |
| 38 return; |
| 39 |
| 40 // Parse out the tab_id. |
| 41 const size_t sep_pos = device.device_id.find(':'); |
| 42 if (sep_pos == std::string::npos) |
| 43 return; |
| 44 |
| 45 int tabId = -1; |
| 46 if (!base::StringToInt( |
| 47 base::StringPiece(device.device_id.data(), sep_pos), &tabId)) |
| 48 return; |
| 49 |
| 50 tab_capture::TabCaptureState state = |
| 51 tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_NONE; |
| 52 switch (new_state) { |
| 53 case content::MediaStreamRequest::STATE_REQUESTED: |
| 54 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_REQUESTED; |
| 55 break; |
| 56 case content::MediaStreamRequest::STATE_PENDING_APPROVAL: |
| 57 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_PENDING; |
| 58 break; |
| 59 case content::MediaStreamRequest::STATE_OPENING: |
| 60 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_ACTIVE; |
| 61 break; |
| 62 case content::MediaStreamRequest::STATE_DONE: |
| 63 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_STOPPED; |
| 64 break; |
| 65 case content::MediaStreamRequest::STATE_ERROR: |
| 66 state = tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_ERROR; |
| 67 break; |
| 68 default: |
| 69 // TODO(justinlin): Implement muted, cancelled state notification. |
| 70 break; |
| 71 } |
| 72 |
| 73 if (state == tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_NONE) { |
| 74 VLOG(1) << "Observed an unhandled capture state."; |
| 75 return; |
| 76 } |
| 77 |
| 78 scoped_ptr<tab_capture::CaptureInfo> info(new tab_capture::CaptureInfo()); |
| 79 info->tab_id = tabId; |
| 80 info->status = state; |
| 81 |
| 82 scoped_ptr<base::ListValue> args(new ListValue()); |
| 83 args->Append(info->ToValue().release()); |
| 84 router->DispatchEventToRenderers(events::kOnTabCaptured, |
| 85 args.Pass(), |
| 86 profile_, |
| 87 GURL()); |
| 88 |
| 89 if (state != tab_capture::EXPERIMENTAL_TAB_CAPTURE_TAB_CAPTURE_STATE_ERROR) |
| 90 requests_[tabId] = state; |
| 91 else |
| 92 requests_.erase(tabId); |
| 93 } |
| 94 |
| 95 std::map<int, tab_capture::TabCaptureState> |
| 96 TabCaptureEventRouter::GetCapturedTabs() { |
| 97 return requests_; |
| 98 } |
| 99 |
| 100 } // namespace extensions |
OLD | NEW |