| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/extensions/api/tab_capture/tab_capture_registry.h" | 5 #include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h" |
| 6 | 6 |
| 7 #include <utility> |
| 8 |
| 7 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 8 #include "chrome/browser/extensions/event_names.h" | 10 #include "chrome/browser/extensions/event_names.h" |
| 9 #include "chrome/browser/extensions/event_router.h" | 11 #include "chrome/browser/extensions/event_router.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" | 12 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/media/media_internals.h" | 13 #include "chrome/browser/media/media_internals.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/profiles/profile_dependency_manager.h" | 15 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" |
| 16 #include "content/public/browser/notification_details.h" | 18 #include "content/public/browser/notification_details.h" |
| 17 #include "content/public/browser/notification_source.h" | 19 #include "content/public/browser/notification_source.h" |
| 18 | 20 |
| 19 namespace events = extensions::event_names; | 21 namespace events = extensions::event_names; |
| 20 using content::BrowserThread; | 22 using content::BrowserThread; |
| 21 | 23 |
| 22 namespace extensions { | 24 namespace extensions { |
| 23 | 25 |
| 26 TabCaptureRegistry::TabCaptureRequest::TabCaptureRequest( |
| 27 std::string extension_id, int tab_id, tab_capture::TabCaptureState status) |
| 28 : extension_id(extension_id), tab_id(tab_id), status(status) { |
| 29 } |
| 30 |
| 31 TabCaptureRegistry::TabCaptureRequest::~TabCaptureRequest() { |
| 32 } |
| 33 |
| 24 TabCaptureRegistry::TabCaptureRegistry(Profile* profile) | 34 TabCaptureRegistry::TabCaptureRegistry(Profile* profile) |
| 25 : proxy_(new MediaObserverProxy()), profile_(profile) { | 35 : proxy_(new MediaObserverProxy()), profile_(profile) { |
| 26 proxy_->Attach(this); | 36 proxy_->Attach(this); |
| 27 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 37 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 28 content::Source<Profile>(profile_)); | 38 content::Source<Profile>(profile_)); |
| 29 } | 39 } |
| 30 | 40 |
| 31 TabCaptureRegistry::~TabCaptureRegistry() { | 41 TabCaptureRegistry::~TabCaptureRegistry() { |
| 32 proxy_->Detach(); | 42 proxy_->Detach(); |
| 33 } | 43 } |
| 34 | 44 |
| 35 void TabCaptureRegistry::HandleRequestUpdateOnUIThread( | 45 void TabCaptureRegistry::HandleRequestUpdateOnUIThread( |
| 36 int render_process_id, | 46 int render_process_id, |
| 37 int render_view_id, | 47 int render_view_id, |
| 38 const content::MediaStreamDevice& device, | 48 const content::MediaStreamDevice& device, |
| 39 const content::MediaRequestState new_state) { | 49 const content::MediaRequestState new_state) { |
| 40 EventRouter* router = profile_ ? | 50 EventRouter* router = profile_ ? |
| 41 extensions::ExtensionSystem::Get(profile_)->event_router() : NULL; | 51 extensions::ExtensionSystem::Get(profile_)->event_router() : NULL; |
| 42 if (!router) | 52 if (!router) |
| 43 return; | 53 return; |
| 44 | 54 |
| 45 std::pair<int, int> key = std::make_pair(render_process_id, render_view_id); | 55 std::pair<int, int> key = std::make_pair(render_process_id, render_view_id); |
| 46 | 56 |
| 47 if (requests_.find(key) == requests_.end()) { | 57 DeviceCaptureRequestMap::iterator request_it = requests_.find(key); |
| 58 if (request_it == requests_.end()) { |
| 48 LOG(ERROR) << "Receiving updates for invalid tab capture request."; | 59 LOG(ERROR) << "Receiving updates for invalid tab capture request."; |
| 49 return; | 60 return; |
| 50 } | 61 } |
| 51 | 62 |
| 52 tab_capture::TabCaptureState state = | 63 tab_capture::TabCaptureState state = |
| 53 tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_NONE; | 64 tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_NONE; |
| 54 switch (new_state) { | 65 switch (new_state) { |
| 55 case content::MEDIA_REQUEST_STATE_REQUESTED: | 66 case content::MEDIA_REQUEST_STATE_REQUESTED: |
| 56 state = tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_REQUESTED; | 67 state = tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_REQUESTED; |
| 57 break; | 68 break; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 70 default: | 81 default: |
| 71 // TODO(justinlin): Implement muted state notification. | 82 // TODO(justinlin): Implement muted state notification. |
| 72 break; | 83 break; |
| 73 } | 84 } |
| 74 | 85 |
| 75 if (state == tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_NONE) { | 86 if (state == tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_NONE) { |
| 76 // This is a state we don't handle. | 87 // This is a state we don't handle. |
| 77 return; | 88 return; |
| 78 } | 89 } |
| 79 | 90 |
| 80 TabCaptureRegistry::TabCaptureRequest& request_info = requests_[key]; | 91 TabCaptureRegistry::TabCaptureRequest* request_info = &request_it->second; |
| 81 request_info.status = state; | 92 request_info->status = state; |
| 82 | 93 |
| 83 scoped_ptr<tab_capture::CaptureInfo> info(new tab_capture::CaptureInfo()); | 94 scoped_ptr<tab_capture::CaptureInfo> info(new tab_capture::CaptureInfo()); |
| 84 info->tab_id = request_info.tab_id; | 95 info->tab_id = request_info->tab_id; |
| 85 info->status = request_info.status; | 96 info->status = request_info->status; |
| 86 | 97 |
| 87 scoped_ptr<base::ListValue> args(new ListValue()); | 98 scoped_ptr<base::ListValue> args(new ListValue()); |
| 88 args->Append(info->ToValue().release()); | 99 args->Append(info->ToValue().release()); |
| 89 scoped_ptr<Event> event(new Event( | 100 scoped_ptr<Event> event(new Event( |
| 90 events::kOnTabCaptureStatusChanged, args.Pass())); | 101 events::kOnTabCaptureStatusChanged, args.Pass())); |
| 91 event->restrict_to_profile = profile_; | 102 event->restrict_to_profile = profile_; |
| 92 router->DispatchEventToExtension(request_info.extension_id, event.Pass()); | 103 router->DispatchEventToExtension(request_info->extension_id, event.Pass()); |
| 93 } | 104 } |
| 94 | 105 |
| 95 const TabCaptureRegistry::CaptureRequestList | 106 const TabCaptureRegistry::CaptureRequestList |
| 96 TabCaptureRegistry::GetCapturedTabs(const std::string& extension_id) { | 107 TabCaptureRegistry::GetCapturedTabs(const std::string& extension_id) { |
| 97 CaptureRequestList list; | 108 CaptureRequestList list; |
| 98 for (DeviceCaptureRequestMap::iterator it = requests_.begin(); | 109 for (DeviceCaptureRequestMap::iterator it = requests_.begin(); |
| 99 it != requests_.end(); ++it) { | 110 it != requests_.end(); ++it) { |
| 100 if (it->second.extension_id == extension_id) { | 111 if (it->second.extension_id == extension_id) { |
| 101 list.push_back(it->second); | 112 list.push_back(it->second); |
| 102 } | 113 } |
| (...skipping 20 matching lines...) Expand all Loading... |
| 123 } | 134 } |
| 124 } | 135 } |
| 125 break; | 136 break; |
| 126 } | 137 } |
| 127 } | 138 } |
| 128 } | 139 } |
| 129 | 140 |
| 130 bool TabCaptureRegistry::AddRequest(const std::pair<int, int> key, | 141 bool TabCaptureRegistry::AddRequest(const std::pair<int, int> key, |
| 131 const TabCaptureRequest& request) { | 142 const TabCaptureRequest& request) { |
| 132 // Currently, we do not allow multiple active captures for same tab. | 143 // Currently, we do not allow multiple active captures for same tab. |
| 133 if (requests_.find(key) != requests_.end()) | 144 DeviceCaptureRequestMap::iterator it = requests_.find(key); |
| 134 if (requests_[key].status != | 145 if (it != requests_.end()) { |
| 135 tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_STOPPED && | 146 const tab_capture::TabCaptureState state = it->second.status; |
| 136 requests_[key].status != | 147 if (state != tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_STOPPED && |
| 137 tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_ERROR) | 148 state != tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_ERROR) |
| 138 return false; | 149 return false; |
| 139 requests_[key] = request; | 150 } |
| 151 requests_.insert(std::make_pair(key, request)); |
| 140 return true; | 152 return true; |
| 141 } | 153 } |
| 142 | 154 |
| 143 bool TabCaptureRegistry::VerifyRequest(int render_process_id, | 155 bool TabCaptureRegistry::VerifyRequest(int render_process_id, |
| 144 int render_view_id) { | 156 int render_view_id) { |
| 145 return requests_.find(std::make_pair( | 157 return requests_.find(std::make_pair( |
| 146 render_process_id, render_view_id)) != requests_.end(); | 158 render_process_id, render_view_id)) != requests_.end(); |
| 147 } | 159 } |
| 148 | 160 |
| 149 void TabCaptureRegistry::MediaObserverProxy::Attach( | 161 void TabCaptureRegistry::MediaObserverProxy::Attach( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 const content::MediaStreamDevice& device, | 209 const content::MediaStreamDevice& device, |
| 198 const content::MediaRequestState new_state) { | 210 const content::MediaRequestState new_state) { |
| 199 if (handler_) | 211 if (handler_) |
| 200 handler_->HandleRequestUpdateOnUIThread(render_process_id, | 212 handler_->HandleRequestUpdateOnUIThread(render_process_id, |
| 201 render_view_id, | 213 render_view_id, |
| 202 device, | 214 device, |
| 203 new_state); | 215 new_state); |
| 204 } | 216 } |
| 205 | 217 |
| 206 } // namespace extensions | 218 } // namespace extensions |
| OLD | NEW |