Chromium Code Reviews| Index: chrome/browser/extensions/api/tab_capture/tab_capture_api.cc |
| diff --git a/chrome/browser/extensions/api/tab_capture/tab_capture_api.cc b/chrome/browser/extensions/api/tab_capture/tab_capture_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b1c2f76e5c504348927c33712cf82a60e919364b |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/tab_capture/tab_capture_api.cc |
| @@ -0,0 +1,141 @@ |
| +// 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. |
| + |
| +// Implements the Chrome Extensions Tab Capture API. |
| + |
| +#include "chrome/browser/extensions/api/tab_capture/tab_capture_api.h" |
| + |
| +#include "base/stringprintf.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/extensions/api/tab_capture/tab_capture_registry.h" |
| +#include "chrome/browser/extensions/api/tab_capture/tab_capture_registry_factory.h" |
| +#include "chrome/browser/extensions/browser_event_router.h" |
| +#include "chrome/browser/extensions/event_names.h" |
| +#include "chrome/browser/extensions/extension_tab_id_map.h" |
| + |
| +namespace TabCapture = extensions::api::tab_capture; |
| +namespace GetCapturedTabs = TabCapture::GetCapturedTabs; |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +const char kPermissionError[] = "Extension does not have tabs permission"; |
| +const char kErrorTabIdNotFound[] = "Could not find the specified tab."; |
| +const char kCapturingSameTab[] = "Cannot capture a tab with an active stream."; |
| + |
| +// Keys/values for media stream constraints. |
| +const char kMediaStreamSource[] = "chromeMediaSource"; |
| +const char kMediaStreamSourceId[] = "chromeMediaSourceId"; |
| +const char kMediaStreamSourceTab[] = "tab"; |
| + |
| +} // namespace |
| + |
| +TabCaptureCaptureFunction::TabCaptureCaptureFunction() { } |
| +TabCaptureCaptureFunction::~TabCaptureCaptureFunction() { } |
| + |
| +bool TabCaptureCaptureFunction::Prepare() { |
| + if (!GetExtension()->HasAPIPermission(APIPermission::kTab)) { |
|
Aaron Boodman
2012/10/16 04:19:27
Two things:
(1) The 'tabs' permission is not the
justinlin
2012/10/17 08:32:26
Changed it back to kTabCapture permission as sugge
|
| + error_ = kPermissionError; |
| + return false; |
| + } |
| + |
| + params_ = TabCapture::Capture::Params::Create(*args_); |
| + EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| + return true; |
| +} |
| + |
| +void TabCaptureCaptureFunction::Work() { |
|
Aaron Boodman
2012/10/16 04:19:27
Wait a second... Why are you using AsyncApiFunctio
justinlin
2012/10/17 08:32:26
Converting process and tab id to process and routi
Aaron Boodman
2012/10/17 20:28:14
You can get both the routing and process ID for a
justinlin
2012/10/17 23:41:48
Done. OK, sorry for the contention surrounding Asy
|
| + ExtensionTabIdMap* tab_id_map = ExtensionTabIdMap::GetInstance(); |
| + int render_process_id = -1; |
| + int routing_id = -1; |
| + int tab_id = *params_->tab_id.get(); |
| + if (!tab_id_map || |
| + !tab_id_map->GetProcessAndRoutingId(tab_id, |
| + &render_process_id, |
| + &routing_id)) { |
| + error_ = kErrorTabIdNotFound; |
| + SetResult(base::Value::CreateIntegerValue(0)); |
| + return; |
| + } |
| + |
| + std::string device_id = base::StringPrintf("%i:%i", render_process_id, |
| + routing_id); |
| + |
| + request_key_ = device_id; |
| + request_ = |
| + (TabCaptureRegistry::TabCaptureRequest) { GetExtension()->id(), tab_id, |
|
Aaron Boodman
2012/10/16 04:19:27
Is the cast necessary? Seems like it wouldn't be.
justinlin
2012/10/17 08:32:26
Done.
|
| + tab_capture::TAB_CAPTURE_TAB_CAPTURE_STATE_NONE }; |
| + |
| + bool audio = false; |
| + bool video = false; |
| + if (params_->options.get()) { |
| + if (params_->options->audio.get()) { |
|
Aaron Boodman
2012/10/16 04:19:27
Nit: this brace is not necessary. It's optional th
justinlin
2012/10/17 08:32:26
Done.
|
| + audio = *params_->options->audio.get(); |
| + } |
| + if (params_->options->video.get()) { |
| + video = *params_->options->video.get(); |
| + } |
| + } |
| + |
| + base::DictionaryValue* result = new base::DictionaryValue(); |
|
Aaron Boodman
2012/10/16 04:19:27
JSON Schema Compiler generates structs you can use
justinlin
2012/10/17 08:32:26
These are sort of intermediate results that we are
Aaron Boodman
2012/10/17 20:28:14
I see.
|
| + if (video) { |
| + result->SetString(std::string("videoConstraints.mandatory.") + |
|
Aaron Boodman
2012/10/16 04:19:27
Question: why "constraints". Seem more like "detai
Aaron Boodman
2012/10/16 04:22:28
I take this back. 'constraints' is fine.
justinlin
2012/10/17 08:32:26
On 2012/10/16 04:22:28, Aaron Boodman wrote:
> On
justinlin
2012/10/17 08:32:26
On 2012/10/16 04:19:27, Aaron Boodman wrote:
> Que
|
| + kMediaStreamSource, kMediaStreamSourceTab); |
| + result->SetString(std::string("videoConstraints.mandatory.") + |
| + kMediaStreamSourceId, device_id); |
| + } else { |
| + result->SetBoolean(std::string("videoConstraints"), false); |
| + } |
| + |
| + if (audio) { |
| + result->SetString(std::string("audioConstraints.mandatory.") + |
| + kMediaStreamSource, kMediaStreamSourceTab); |
| + result->SetString(std::string("audioConstraints.mandatory.") + |
| + kMediaStreamSourceId, device_id); |
| + } else { |
| + result->SetBoolean(std::string("audioConstraints"), false); |
| + } |
| + |
| + SetResult(result); |
| +} |
| + |
| +bool TabCaptureCaptureFunction::Respond() { |
| + extensions::TabCaptureRegistry* registry = |
| + extensions::TabCaptureRegistryFactory::GetForProfile(profile()); |
| + if (!request_key_.empty() && !registry->AddRequest(request_key_, request_)) { |
| + error_ = kCapturingSameTab; |
| + SetResult(base::Value::CreateIntegerValue(0)); |
| + } |
| + |
| + return error_.empty(); |
| +} |
| + |
| +bool TabCaptureGetCapturedTabsFunction::RunImpl() { |
| + if (!GetExtension()->HasAPIPermission(APIPermission::kTab)) { |
| + error_ = kPermissionError; |
| + return false; |
| + } |
| + |
| + extensions::TabCaptureRegistry* registry = |
| + extensions::TabCaptureRegistryFactory::GetForProfile(profile()); |
| + |
| + const TabCaptureRegistry::CaptureRequestList& captured_tabs = |
| + registry->GetCapturedTabs(GetExtension()->id()); |
| + |
| + base::ListValue *list = new base::ListValue(); |
| + for (TabCaptureRegistry::CaptureRequestList::const_iterator it = |
| + captured_tabs.begin(); it != captured_tabs.end(); it++) { |
| + scoped_ptr<tab_capture::CaptureInfo> info(new tab_capture::CaptureInfo()); |
| + info->tab_id = it->tab_id; |
| + info->status = it->status; |
| + list->Append(info->ToValue().release()); |
| + } |
| + |
| + SetResult(list); |
| + return true; |
| +} |
| + |
| +} // namespace extensions |