| 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> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/values.h" | 12 #include "base/values.h" |
| 12 #include "chrome/browser/sessions/session_tab_helper.h" | 13 #include "chrome/browser/sessions/session_tab_helper.h" |
| 13 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 14 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 15 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
| 16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
| 18 #include "extensions/browser/event_router.h" | 19 #include "extensions/browser/event_router.h" |
| 19 #include "extensions/browser/extension_registry.h" | 20 #include "extensions/browser/extension_registry.h" |
| 20 #include "extensions/common/extension.h" | 21 #include "extensions/common/extension.h" |
| (...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 TabCaptureRegistry::GetFactoryInstance() { | 266 TabCaptureRegistry::GetFactoryInstance() { |
| 266 return g_factory.Pointer(); | 267 return g_factory.Pointer(); |
| 267 } | 268 } |
| 268 | 269 |
| 269 void TabCaptureRegistry::GetCapturedTabs( | 270 void TabCaptureRegistry::GetCapturedTabs( |
| 270 const std::string& extension_id, | 271 const std::string& extension_id, |
| 271 base::ListValue* list_of_capture_info) const { | 272 base::ListValue* list_of_capture_info) const { |
| 272 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 273 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 273 DCHECK(list_of_capture_info); | 274 DCHECK(list_of_capture_info); |
| 274 list_of_capture_info->Clear(); | 275 list_of_capture_info->Clear(); |
| 275 for (const LiveRequest* request : requests_) { | 276 for (const std::unique_ptr<LiveRequest>& request : requests_) { |
| 276 if (request->is_anonymous() || !request->is_verified() || | 277 if (request->is_anonymous() || !request->is_verified() || |
| 277 request->extension_id() != extension_id) | 278 request->extension_id() != extension_id) |
| 278 continue; | 279 continue; |
| 279 tab_capture::CaptureInfo info; | 280 tab_capture::CaptureInfo info; |
| 280 request->GetCaptureInfo(&info); | 281 request->GetCaptureInfo(&info); |
| 281 list_of_capture_info->Append(info.ToValue()); | 282 list_of_capture_info->Append(info.ToValue()); |
| 282 } | 283 } |
| 283 } | 284 } |
| 284 | 285 |
| 285 void TabCaptureRegistry::OnExtensionUnloaded( | 286 void TabCaptureRegistry::OnExtensionUnloaded( |
| 286 content::BrowserContext* browser_context, | 287 content::BrowserContext* browser_context, |
| 287 const Extension* extension, | 288 const Extension* extension, |
| 288 UnloadedExtensionInfo::Reason reason) { | 289 UnloadedExtensionInfo::Reason reason) { |
| 289 // Cleanup all the requested media streams for this extension. | 290 // Cleanup all the requested media streams for this extension. |
| 290 for (ScopedVector<LiveRequest>::iterator it = requests_.begin(); | 291 for (std::vector<std::unique_ptr<LiveRequest>>::iterator it = |
| 292 requests_.begin(); |
| 291 it != requests_.end();) { | 293 it != requests_.end();) { |
| 292 if ((*it)->extension_id() == extension->id()) { | 294 if ((*it)->extension_id() == extension->id()) { |
| 293 it = requests_.erase(it); | 295 it = requests_.erase(it); |
| 294 } else { | 296 } else { |
| 295 ++it; | 297 ++it; |
| 296 } | 298 } |
| 297 } | 299 } |
| 298 } | 300 } |
| 299 | 301 |
| 300 bool TabCaptureRegistry::AddRequest(content::WebContents* target_contents, | 302 bool TabCaptureRegistry::AddRequest(content::WebContents* target_contents, |
| 301 const std::string& extension_id, | 303 const std::string& extension_id, |
| 302 bool is_anonymous) { | 304 bool is_anonymous) { |
| 303 LiveRequest* const request = FindRequest(target_contents); | 305 LiveRequest* const request = FindRequest(target_contents); |
| 304 | 306 |
| 305 // Currently, we do not allow multiple active captures for same tab. | 307 // Currently, we do not allow multiple active captures for same tab. |
| 306 if (request != NULL) { | 308 if (request != NULL) { |
| 307 if (request->capture_state() == tab_capture::TAB_CAPTURE_STATE_PENDING || | 309 if (request->capture_state() == tab_capture::TAB_CAPTURE_STATE_PENDING || |
| 308 request->capture_state() == tab_capture::TAB_CAPTURE_STATE_ACTIVE) { | 310 request->capture_state() == tab_capture::TAB_CAPTURE_STATE_ACTIVE) { |
| 309 return false; | 311 return false; |
| 310 } else { | 312 } else { |
| 311 // Delete the request before creating its replacement (below). | 313 // Delete the request before creating its replacement (below). |
| 312 KillRequest(request); | 314 KillRequest(request); |
| 313 } | 315 } |
| 314 } | 316 } |
| 315 | 317 |
| 316 requests_.push_back( | 318 requests_.push_back(base::MakeUnique<LiveRequest>( |
| 317 new LiveRequest(target_contents, extension_id, is_anonymous, this)); | 319 target_contents, extension_id, is_anonymous, this)); |
| 318 return true; | 320 return true; |
| 319 } | 321 } |
| 320 | 322 |
| 321 bool TabCaptureRegistry::VerifyRequest( | 323 bool TabCaptureRegistry::VerifyRequest( |
| 322 int render_process_id, | 324 int render_process_id, |
| 323 int render_frame_id, | 325 int render_frame_id, |
| 324 const std::string& extension_id) { | 326 const std::string& extension_id) { |
| 325 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 327 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 326 | 328 |
| 327 LiveRequest* const request = FindRequest( | 329 LiveRequest* const request = FindRequest( |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 std::unique_ptr<Event> event( | 431 std::unique_ptr<Event> event( |
| 430 new Event(events::TAB_CAPTURE_ON_STATUS_CHANGED, | 432 new Event(events::TAB_CAPTURE_ON_STATUS_CHANGED, |
| 431 tab_capture::OnStatusChanged::kEventName, std::move(args))); | 433 tab_capture::OnStatusChanged::kEventName, std::move(args))); |
| 432 event->restrict_to_browser_context = browser_context_; | 434 event->restrict_to_browser_context = browser_context_; |
| 433 | 435 |
| 434 router->DispatchEventToExtension(request->extension_id(), std::move(event)); | 436 router->DispatchEventToExtension(request->extension_id(), std::move(event)); |
| 435 } | 437 } |
| 436 | 438 |
| 437 TabCaptureRegistry::LiveRequest* TabCaptureRegistry::FindRequest( | 439 TabCaptureRegistry::LiveRequest* TabCaptureRegistry::FindRequest( |
| 438 const content::WebContents* target_contents) const { | 440 const content::WebContents* target_contents) const { |
| 439 for (ScopedVector<LiveRequest>::const_iterator it = requests_.begin(); | 441 for (const auto& request : requests_) { |
| 440 it != requests_.end(); ++it) { | 442 if (request->web_contents() == target_contents) |
| 441 if ((*it)->web_contents() == target_contents) | 443 return request.get(); |
| 442 return *it; | |
| 443 } | 444 } |
| 444 return NULL; | 445 return nullptr; |
| 445 } | 446 } |
| 446 | 447 |
| 447 TabCaptureRegistry::LiveRequest* TabCaptureRegistry::FindRequest( | 448 TabCaptureRegistry::LiveRequest* TabCaptureRegistry::FindRequest( |
| 448 int original_target_render_process_id, | 449 int original_target_render_process_id, |
| 449 int original_target_render_frame_id) const { | 450 int original_target_render_frame_id) const { |
| 450 for (ScopedVector<LiveRequest>::const_iterator it = requests_.begin(); | 451 for (const std::unique_ptr<LiveRequest>& request : requests_) { |
| 451 it != requests_.end(); ++it) { | 452 if (request->WasOriginallyTargettingRenderFrameID( |
| 452 if ((*it)->WasOriginallyTargettingRenderFrameID( | |
| 453 original_target_render_process_id, | 453 original_target_render_process_id, |
| 454 original_target_render_frame_id)) | 454 original_target_render_frame_id)) { |
| 455 return *it; | 455 return request.get(); |
| 456 } |
| 456 } | 457 } |
| 457 return NULL; | 458 return nullptr; |
| 458 } | 459 } |
| 459 | 460 |
| 460 void TabCaptureRegistry::KillRequest(LiveRequest* request) { | 461 void TabCaptureRegistry::KillRequest(LiveRequest* request) { |
| 461 for (ScopedVector<LiveRequest>::iterator it = requests_.begin(); | 462 for (std::vector<std::unique_ptr<LiveRequest>>::iterator it = |
| 463 requests_.begin(); |
| 462 it != requests_.end(); ++it) { | 464 it != requests_.end(); ++it) { |
| 463 if ((*it) == request) { | 465 if (it->get() == request) { |
| 464 requests_.erase(it); | 466 requests_.erase(it); |
| 465 return; | 467 return; |
| 466 } | 468 } |
| 467 } | 469 } |
| 468 NOTREACHED(); | 470 NOTREACHED(); |
| 469 } | 471 } |
| 470 | 472 |
| 471 } // namespace extensions | 473 } // namespace extensions |
| OLD | NEW |