Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/offscreen_presentation.h" | 5 #include "chrome/browser/extensions/api/tab_capture/offscreen_presentation.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/rand_util.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 10 #include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h" | 12 #include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/ui/web_contents_sizer.h" | 14 #include "chrome/browser/ui/web_contents_sizer.h" |
| 13 #include "content/public/browser/render_widget_host_view.h" | 15 #include "content/public/browser/render_widget_host_view.h" |
| 14 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 15 #include "extensions/browser/extension_host.h" | 17 #include "extensions/browser/extension_host.h" |
| 16 #include "extensions/browser/process_manager.h" | 18 #include "extensions/browser/process_manager.h" |
| 17 | 19 |
| 18 #if defined(USE_AURA) | 20 #if defined(USE_AURA) |
| 19 #include "base/memory/weak_ptr.h" | 21 #include "base/memory/weak_ptr.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 131 OffscreenPresentationsOwner::~OffscreenPresentationsOwner() {} | 133 OffscreenPresentationsOwner::~OffscreenPresentationsOwner() {} |
| 132 | 134 |
| 133 // static | 135 // static |
| 134 OffscreenPresentationsOwner* OffscreenPresentationsOwner::Get( | 136 OffscreenPresentationsOwner* OffscreenPresentationsOwner::Get( |
| 135 content::WebContents* extension_web_contents) { | 137 content::WebContents* extension_web_contents) { |
| 136 // CreateForWebContents() really means "create if not exists." | 138 // CreateForWebContents() really means "create if not exists." |
| 137 CreateForWebContents(extension_web_contents); | 139 CreateForWebContents(extension_web_contents); |
| 138 return FromWebContents(extension_web_contents); | 140 return FromWebContents(extension_web_contents); |
| 139 } | 141 } |
| 140 | 142 |
| 141 OffscreenPresentation* OffscreenPresentationsOwner::FindOrStartPresentation( | 143 OffscreenPresentation* OffscreenPresentationsOwner::StartPresentation( |
| 142 const GURL& start_url, | 144 const GURL& start_url, |
| 143 const std::string& presentation_id, | |
| 144 const gfx::Size& initial_size) { | 145 const gfx::Size& initial_size) { |
| 145 OffscreenPresentation* presentation = | |
| 146 FindPresentation(start_url, presentation_id); | |
| 147 if (presentation) { | |
| 148 DVLOG(1) << "Returning already-running OffscreenPresentation for start_url=" | |
| 149 << presentation->start_url(); | |
| 150 return presentation; | |
| 151 } | |
| 152 | |
| 153 if (presentations_.size() >= kMaxPresentationsPerExtension) | 146 if (presentations_.size() >= kMaxPresentationsPerExtension) |
| 154 return nullptr; // Maximum number of presentations reached. | 147 return nullptr; // Maximum number of presentations reached. |
| 155 | 148 |
| 156 presentation = new OffscreenPresentation(this, start_url, presentation_id); | 149 presentations_.push_back(new OffscreenPresentation( |
| 157 presentations_.push_back(presentation); | 150 this, |
| 158 presentation->Start(initial_size); | 151 start_url, |
| 159 return presentation; | 152 CreateUniqueRandomId())); |
| 153 presentations_.back()->Start(initial_size); | |
| 154 return presentations_.back(); | |
| 155 } | |
| 156 | |
| 157 OffscreenPresentation* OffscreenPresentationsOwner::FindByOffscreenTabId( | |
| 158 const std::string& offscreen_tab_id) const { | |
| 159 for (OffscreenPresentation* p : presentations_) { | |
| 160 if (p->id() == offscreen_tab_id) | |
| 161 return p; | |
| 162 } | |
| 163 return nullptr; | |
| 160 } | 164 } |
| 161 | 165 |
| 162 void OffscreenPresentationsOwner::ClosePresentation( | 166 void OffscreenPresentationsOwner::ClosePresentation( |
| 163 OffscreenPresentation* presentation) { | 167 OffscreenPresentation* presentation) { |
| 164 const auto it = | 168 const auto it = |
| 165 std::find(presentations_.begin(), presentations_.end(), presentation); | 169 std::find(presentations_.begin(), presentations_.end(), presentation); |
| 166 if (it != presentations_.end()) | 170 if (it != presentations_.end()) |
| 167 presentations_.erase(it); | 171 presentations_.erase(it); |
| 168 } | 172 } |
| 169 | 173 |
| 170 OffscreenPresentation* OffscreenPresentationsOwner::FindPresentation( | 174 std::string OffscreenPresentationsOwner::CreateUniqueRandomId() const { |
| 171 const GURL& start_url, const std::string& presentation_id) const { | 175 while (true) { |
|
not at google - send to devlin
2015/10/02 01:17:26
Curious: what is the advantage in generating a ran
miu
2015/10/02 18:58:21
No longer applicable.
| |
| 172 for (OffscreenPresentation* presentation : presentations_) { | 176 const uint64 random_number = base::RandUint64(); |
| 173 if (presentation->start_url() == start_url && | 177 const std::string candidate_id = |
| 174 presentation->presentation_id() == presentation_id) | 178 base::HexEncode(&random_number, sizeof(random_number)); |
| 175 return presentation; | 179 if (!FindByOffscreenTabId(candidate_id)) |
| 180 return candidate_id; | |
| 176 } | 181 } |
| 177 return nullptr; | |
| 178 } | 182 } |
| 179 | 183 |
| 180 OffscreenPresentation::OffscreenPresentation(OffscreenPresentationsOwner* owner, | 184 OffscreenPresentation::OffscreenPresentation(OffscreenPresentationsOwner* owner, |
| 181 const GURL& start_url, | 185 const GURL& start_url, |
| 182 const std::string& id) | 186 const std::string& id) |
| 183 : owner_(owner), | 187 : owner_(owner), |
| 184 start_url_(start_url), | 188 start_url_(start_url), |
| 185 presentation_id_(id), | 189 id_(id), |
| 186 profile_(Profile::FromBrowserContext( | 190 profile_(Profile::FromBrowserContext( |
| 187 owner->extension_web_contents()->GetBrowserContext()) | 191 owner->extension_web_contents()->GetBrowserContext()) |
| 188 ->CreateOffTheRecordProfile()), | 192 ->CreateOffTheRecordProfile()), |
| 189 capture_poll_timer_(false, false), | 193 capture_poll_timer_(false, false), |
| 190 content_capture_was_detected_(false) { | 194 content_capture_was_detected_(false) { |
| 191 DCHECK(profile_); | 195 DCHECK(profile_); |
| 192 } | 196 } |
| 193 | 197 |
| 194 OffscreenPresentation::~OffscreenPresentation() { | 198 OffscreenPresentation::~OffscreenPresentation() { |
| 195 DVLOG(1) << "Destroying OffscreenPresentation for start_url=" | 199 DVLOG(1) << "Destroying OffscreenPresentation for start_url=" |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 374 DCHECK_EQ(presentation_web_contents_.get(), contents); | 378 DCHECK_EQ(presentation_web_contents_.get(), contents); |
| 375 | 379 |
| 376 // This method is being called to check whether an extension is permitted to | 380 // This method is being called to check whether an extension is permitted to |
| 377 // capture the page. Verify that the request is being made by the extension | 381 // capture the page. Verify that the request is being made by the extension |
| 378 // that spawned this OffscreenPresentation. | 382 // that spawned this OffscreenPresentation. |
| 379 | 383 |
| 380 // Find the extension ID associated with the extension background page's | 384 // Find the extension ID associated with the extension background page's |
| 381 // WebContents. | 385 // WebContents. |
| 382 content::BrowserContext* const extension_browser_context = | 386 content::BrowserContext* const extension_browser_context = |
| 383 owner_->extension_web_contents()->GetBrowserContext(); | 387 owner_->extension_web_contents()->GetBrowserContext(); |
| 384 std::string extension_id; | 388 const extensions::Extension* const extension = |
| 385 for (const ExtensionHost* host : | 389 ProcessManager::Get(extension_browser_context)-> |
| 386 ProcessManager::Get(extension_browser_context)->background_hosts()) { | 390 GetExtensionForWebContents(owner_->extension_web_contents()); |
| 387 if (host->host_contents() == owner_->extension_web_contents()) { | 391 const std::string extension_id = extension ? extension->id() : ""; |
| 388 extension_id = host->extension_id(); | 392 LOG_IF(DFATAL, extension_id.empty()) |
| 389 break; | 393 << "Extension that started this OffscreenPresentation was not found."; |
| 390 } | |
| 391 } | |
| 392 | 394 |
| 393 // If verified, allow any tab capture audio/video devices that were requested. | 395 // If verified, allow any tab capture audio/video devices that were requested. |
| 394 extensions::TabCaptureRegistry* const tab_capture_registry = | 396 extensions::TabCaptureRegistry* const tab_capture_registry = |
| 395 extensions::TabCaptureRegistry::Get(extension_browser_context); | 397 extensions::TabCaptureRegistry::Get(extension_browser_context); |
| 396 content::MediaStreamDevices devices; | 398 content::MediaStreamDevices devices; |
| 397 if (tab_capture_registry && tab_capture_registry->VerifyRequest( | 399 if (tab_capture_registry && tab_capture_registry->VerifyRequest( |
| 398 request.render_process_id, | 400 request.render_process_id, |
| 399 request.render_frame_id, | 401 request.render_frame_id, |
| 400 extension_id)) { | 402 extension_id)) { |
| 401 if (request.audio_type == content::MEDIA_TAB_AUDIO_CAPTURE) { | 403 if (request.audio_type == content::MEDIA_TAB_AUDIO_CAPTURE) { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 | 470 |
| 469 // Schedule the timer to check again in a second. | 471 // Schedule the timer to check again in a second. |
| 470 capture_poll_timer_.Start( | 472 capture_poll_timer_.Start( |
| 471 FROM_HERE, | 473 FROM_HERE, |
| 472 base::TimeDelta::FromSeconds(kPollIntervalInSeconds), | 474 base::TimeDelta::FromSeconds(kPollIntervalInSeconds), |
| 473 base::Bind(&OffscreenPresentation::DieIfContentCaptureEnded, | 475 base::Bind(&OffscreenPresentation::DieIfContentCaptureEnded, |
| 474 base::Unretained(this))); | 476 base::Unretained(this))); |
| 475 } | 477 } |
| 476 | 478 |
| 477 } // namespace extensions | 479 } // namespace extensions |
| OLD | NEW |