| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/webcam_private/webcam_private_api.h" | 5 #include "extensions/browser/api/webcam_private/webcam_private_api.h" |
| 6 | 6 |
| 7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
| 8 #include "content/public/browser/browser_context.h" | 8 #include "content/public/browser/browser_context.h" |
| 9 #include "content/public/browser/media_device_id.h" | 9 #include "content/public/browser/media_device_id.h" |
| 10 #include "content/public/browser/resource_context.h" | 10 #include "content/public/browser/resource_context.h" |
| 11 #include "content/public/common/media_stream_request.h" | 11 #include "content/public/common/media_stream_request.h" |
| 12 #include "extensions/browser/api/webcam_private/v4l2_webcam.h" | 12 #include "extensions/browser/api/webcam_private/v4l2_webcam.h" |
| 13 #include "extensions/browser/api/webcam_private/webcam.h" | 13 #include "extensions/browser/api/webcam_private/visca_webcam.h" |
| 14 #include "extensions/browser/process_manager.h" | 14 #include "extensions/browser/process_manager.h" |
| 15 #include "extensions/browser/process_manager_factory.h" | 15 #include "extensions/browser/process_manager_factory.h" |
| 16 #include "extensions/common/api/webcam_private.h" | 16 #include "extensions/common/api/webcam_private.h" |
| 17 | 17 |
| 18 namespace webcam_private = extensions::core_api::webcam_private; | 18 namespace webcam_private = extensions::core_api::webcam_private; |
| 19 | 19 |
| 20 namespace content { | 20 namespace content { |
| 21 class BrowserContext; | 21 class BrowserContext; |
| 22 } // namespace content | 22 } // namespace content |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 const char kPathInUse[] = "Path in use"; |
| 25 const char kUnknownWebcam[] = "Unknown webcam id"; | 26 const char kUnknownWebcam[] = "Unknown webcam id"; |
| 26 } // namespace | 27 } // namespace |
| 27 | 28 |
| 28 namespace extensions { | 29 namespace extensions { |
| 29 | 30 |
| 30 // static | 31 // static |
| 31 WebcamPrivateAPI* WebcamPrivateAPI::Get(content::BrowserContext* context) { | 32 WebcamPrivateAPI* WebcamPrivateAPI::Get(content::BrowserContext* context) { |
| 32 return GetFactoryInstance()->Get(context); | 33 return GetFactoryInstance()->Get(context); |
| 33 } | 34 } |
| 34 | 35 |
| 35 WebcamPrivateAPI::WebcamPrivateAPI(content::BrowserContext* context) | 36 WebcamPrivateAPI::WebcamPrivateAPI(content::BrowserContext* context) |
| 36 : browser_context_(context), | 37 : browser_context_(context), |
| 37 process_manager_observer_(this), | 38 process_manager_observer_(this) { |
| 38 weak_ptr_factory_(this) { | |
| 39 process_manager_observer_.Add(ProcessManager::Get(browser_context_)); | 39 process_manager_observer_.Add(ProcessManager::Get(browser_context_)); |
| 40 } | 40 } |
| 41 | 41 |
| 42 WebcamPrivateAPI::~WebcamPrivateAPI() {} | 42 WebcamPrivateAPI::~WebcamPrivateAPI() {} |
| 43 | 43 |
| 44 Webcam* WebcamPrivateAPI::GetWebcam(const std::string& extension_id, | 44 Webcam* WebcamPrivateAPI::GetWebcam(const std::string& extension_id, |
| 45 const std::string& webcam_id) { | 45 const std::string& webcam_id) { |
| 46 std::string device_id; | 46 std::string device_id; |
| 47 if (!GetDeviceId(extension_id, webcam_id, &device_id)) { | 47 if (!GetDeviceId(extension_id, webcam_id, &device_id)) { |
| 48 return nullptr; | 48 return nullptr; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 60 } | 60 } |
| 61 | 61 |
| 62 linked_ptr<Webcam> webcam(v4l2_webcam.release()); | 62 linked_ptr<Webcam> webcam(v4l2_webcam.release()); |
| 63 | 63 |
| 64 webcams_[device_id] = webcam; | 64 webcams_[device_id] = webcam; |
| 65 webcam->AddExtensionRef(extension_id); | 65 webcam->AddExtensionRef(extension_id); |
| 66 | 66 |
| 67 return webcam.get(); | 67 return webcam.get(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 bool WebcamPrivateAPI::OpenSerialWebcam( |
| 71 const std::string& extension_id, |
| 72 const std::string& device_id, |
| 73 const base::Callback<void(const std::string&, bool)>& callback) { |
| 74 auto ix = webcams_.find(device_id); |
| 75 if (ix != webcams_.end()) { |
| 76 return false; |
| 77 } |
| 78 |
| 79 ViscaWebcam* visca_webcam(new ViscaWebcam(device_id, extension_id)); |
| 80 visca_webcam->AddExtensionRef(extension_id); |
| 81 webcams_[device_id] = linked_ptr<Webcam>(visca_webcam); |
| 82 |
| 83 std::string webcam_id = GetWebcamId(extension_id, device_id); |
| 84 visca_webcam->Open(base::Bind(&WebcamPrivateAPI::OnOpenSerialWebcam, |
| 85 AsWeakPtr(), |
| 86 extension_id, device_id, callback)); |
| 87 |
| 88 return true; |
| 89 } |
| 90 |
| 91 bool WebcamPrivateAPI::CloseWebcam(const std::string& extension_id, |
| 92 const std::string& webcam_id) { |
| 93 std::string device_id; |
| 94 if (!GetDeviceId(extension_id, webcam_id, &device_id)) { |
| 95 return false; |
| 96 } |
| 97 |
| 98 auto webcam = webcams_.find(device_id); |
| 99 if (webcam == webcams_.end()) |
| 100 return false; |
| 101 |
| 102 webcam->second->RemoveExtensionRef(extension_id); |
| 103 if (webcam->second->ShouldDelete()) |
| 104 webcams_.erase(webcam); |
| 105 |
| 106 return true; |
| 107 } |
| 108 |
| 109 void WebcamPrivateAPI::OnOpenSerialWebcam( |
| 110 const std::string& extension_id, |
| 111 const std::string& device_id, |
| 112 const base::Callback<void(const std::string&, bool)>& callback, |
| 113 bool success) { |
| 114 if (success) { |
| 115 std::string webcam_id = GetWebcamId(extension_id, device_id); |
| 116 callback.Run(webcam_id, true); |
| 117 } else { |
| 118 webcams_.erase(device_id); |
| 119 callback.Run("", false); |
| 120 } |
| 121 } |
| 122 |
| 70 bool WebcamPrivateAPI::GetDeviceId(const std::string& extension_id, | 123 bool WebcamPrivateAPI::GetDeviceId(const std::string& extension_id, |
| 71 const std::string& webcam_id, | 124 const std::string& webcam_id, |
| 72 std::string* device_id) { | 125 std::string* device_id) { |
| 73 GURL security_origin = | 126 GURL security_origin = |
| 74 extensions::Extension::GetBaseURLFromExtensionId(extension_id); | 127 extensions::Extension::GetBaseURLFromExtensionId(extension_id); |
| 75 | 128 |
| 76 return content::GetMediaDeviceIDForHMAC( | 129 return content::GetMediaDeviceIDForHMAC( |
| 77 content::MEDIA_DEVICE_VIDEO_CAPTURE, | 130 content::MEDIA_DEVICE_VIDEO_CAPTURE, |
| 78 browser_context_->GetResourceContext()->GetMediaDeviceIDSalt(), | 131 browser_context_->GetResourceContext()->GetMediaDeviceIDSalt(), |
| 79 security_origin, | 132 security_origin, |
| 80 webcam_id, | 133 webcam_id, |
| 81 device_id); | 134 device_id); |
| 82 } | 135 } |
| 83 | 136 |
| 137 std::string WebcamPrivateAPI::GetWebcamId(const std::string& extension_id, |
| 138 const std::string& device_id) { |
| 139 GURL security_origin = |
| 140 extensions::Extension::GetBaseURLFromExtensionId(extension_id); |
| 141 |
| 142 return content::GetHMACForMediaDeviceID( |
| 143 browser_context_->GetResourceContext()->GetMediaDeviceIDSalt(), |
| 144 security_origin, device_id); |
| 145 } |
| 146 |
| 84 void WebcamPrivateAPI::OnBackgroundHostClose(const std::string& extension_id) { | 147 void WebcamPrivateAPI::OnBackgroundHostClose(const std::string& extension_id) { |
| 85 for (auto webcam = webcams_.begin(); | 148 for (auto webcam = webcams_.begin(); |
| 86 webcam != webcams_.end(); /* No increment */ ) { | 149 webcam != webcams_.end(); /* No increment */ ) { |
| 87 auto next = std::next(webcam); | 150 auto next = std::next(webcam); |
| 88 webcam->second->RemoveExtensionRef(extension_id); | 151 webcam->second->RemoveExtensionRef(extension_id); |
| 89 if (webcam->second->ShouldDelete()) | 152 if (webcam->second->ShouldDelete()) |
| 90 webcams_.erase(webcam); | 153 webcams_.erase(webcam); |
| 91 webcam = next; | 154 webcam = next; |
| 92 } | 155 } |
| 93 } | 156 } |
| 94 | 157 |
| 95 WebcamPrivateSetFunction::WebcamPrivateSetFunction() { | 158 WebcamPrivateOpenSerialWebcamFunction:: |
| 159 WebcamPrivateOpenSerialWebcamFunction() {} |
| 160 |
| 161 WebcamPrivateOpenSerialWebcamFunction:: |
| 162 ~WebcamPrivateOpenSerialWebcamFunction() {} |
| 163 |
| 164 bool WebcamPrivateOpenSerialWebcamFunction::RunAsync() { |
| 165 // Get parameters |
| 166 scoped_ptr<webcam_private::OpenSerialWebcam::Params> params( |
| 167 webcam_private::OpenSerialWebcam::Params::Create(*args_)); |
| 168 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 169 |
| 170 std::string webcam_id; |
| 171 if (WebcamPrivateAPI::Get(browser_context())->OpenSerialWebcam( |
| 172 extension_id(), params->path, |
| 173 base::Bind(&WebcamPrivateOpenSerialWebcamFunction::OnOpenWebcam, this))) { |
| 174 return true; |
| 175 } else { |
| 176 SetError(kPathInUse); |
| 177 return false; |
| 178 } |
| 96 } | 179 } |
| 97 | 180 |
| 98 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() { | 181 void WebcamPrivateOpenSerialWebcamFunction::OnOpenWebcam( |
| 182 const std::string& webcam_id, bool success) { |
| 183 if (success) { |
| 184 SetResult(new base::StringValue(webcam_id)); |
| 185 SendResponse(true); |
| 186 } else { |
| 187 SetError(kUnknownWebcam); |
| 188 SendResponse(false); |
| 189 } |
| 99 } | 190 } |
| 100 | 191 |
| 192 WebcamPrivateCloseWebcamFunction::WebcamPrivateCloseWebcamFunction() {} |
| 193 |
| 194 WebcamPrivateCloseWebcamFunction::~WebcamPrivateCloseWebcamFunction() {} |
| 195 |
| 196 bool WebcamPrivateCloseWebcamFunction::RunAsync() { |
| 197 // Get parameters |
| 198 scoped_ptr<webcam_private::CloseWebcam::Params> params( |
| 199 webcam_private::CloseWebcam::Params::Create(*args_)); |
| 200 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 201 |
| 202 return WebcamPrivateAPI::Get(browser_context())->CloseWebcam( |
| 203 extension_id(), params->webcam_id); |
| 204 } |
| 205 |
| 206 WebcamPrivateSetFunction::WebcamPrivateSetFunction() {} |
| 207 |
| 208 WebcamPrivateSetFunction::~WebcamPrivateSetFunction() {} |
| 209 |
| 101 bool WebcamPrivateSetFunction::RunSync() { | 210 bool WebcamPrivateSetFunction::RunSync() { |
| 102 // Get parameters | 211 // Get parameters |
| 103 scoped_ptr<webcam_private::Set::Params> params( | 212 scoped_ptr<webcam_private::Set::Params> params( |
| 104 webcam_private::Set::Params::Create(*args_)); | 213 webcam_private::Set::Params::Create(*args_)); |
| 105 EXTENSION_FUNCTION_VALIDATE(params.get()); | 214 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 106 | 215 |
| 107 Webcam* webcam = WebcamPrivateAPI::Get(browser_context())-> | 216 Webcam* webcam = WebcamPrivateAPI::Get(browser_context())-> |
| 108 GetWebcam(extension_id(), params->webcam_id); | 217 GetWebcam(extension_id(), params->webcam_id); |
| 109 if (!webcam) { | 218 if (!webcam) { |
| 110 SetError(kUnknownWebcam); | 219 SetError(kUnknownWebcam); |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 } | 267 } |
| 159 | 268 |
| 160 if (params->config.zoom) { | 269 if (params->config.zoom) { |
| 161 webcam->SetZoom(*(params->config.zoom)); | 270 webcam->SetZoom(*(params->config.zoom)); |
| 162 } | 271 } |
| 163 | 272 |
| 164 | 273 |
| 165 return true; | 274 return true; |
| 166 } | 275 } |
| 167 | 276 |
| 168 WebcamPrivateGetFunction::WebcamPrivateGetFunction() { | 277 WebcamPrivateGetFunction::WebcamPrivateGetFunction() {} |
| 169 } | |
| 170 | 278 |
| 171 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() { | 279 WebcamPrivateGetFunction::~WebcamPrivateGetFunction() {} |
| 172 } | |
| 173 | 280 |
| 174 bool WebcamPrivateGetFunction::RunSync() { | 281 bool WebcamPrivateGetFunction::RunSync() { |
| 175 // Get parameters | 282 // Get parameters |
| 176 scoped_ptr<webcam_private::Get::Params> params( | 283 scoped_ptr<webcam_private::Get::Params> params( |
| 177 webcam_private::Get::Params::Create(*args_)); | 284 webcam_private::Get::Params::Create(*args_)); |
| 178 EXTENSION_FUNCTION_VALIDATE(params.get()); | 285 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 179 | 286 |
| 180 Webcam* webcam = WebcamPrivateAPI::Get(browser_context())-> | 287 Webcam* webcam = WebcamPrivateAPI::Get(browser_context())-> |
| 181 GetWebcam(extension_id(), params->webcam_id); | 288 GetWebcam(extension_id(), params->webcam_id); |
| 182 if (!webcam) { | 289 if (!webcam) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 196 | 303 |
| 197 int zoom; | 304 int zoom; |
| 198 if (webcam->GetZoom(&zoom)) | 305 if (webcam->GetZoom(&zoom)) |
| 199 result.zoom.reset(new double(zoom)); | 306 result.zoom.reset(new double(zoom)); |
| 200 | 307 |
| 201 SetResult(result.ToValue().release()); | 308 SetResult(result.ToValue().release()); |
| 202 | 309 |
| 203 return true; | 310 return true; |
| 204 } | 311 } |
| 205 | 312 |
| 206 WebcamPrivateResetFunction::WebcamPrivateResetFunction() { | 313 WebcamPrivateResetFunction::WebcamPrivateResetFunction() {} |
| 207 } | |
| 208 | 314 |
| 209 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() { | 315 WebcamPrivateResetFunction::~WebcamPrivateResetFunction() {} |
| 210 } | |
| 211 | 316 |
| 212 bool WebcamPrivateResetFunction::RunSync() { | 317 bool WebcamPrivateResetFunction::RunSync() { |
| 213 // Get parameters | 318 // Get parameters |
| 214 scoped_ptr<webcam_private::Reset::Params> params( | 319 scoped_ptr<webcam_private::Reset::Params> params( |
| 215 webcam_private::Reset::Params::Create(*args_)); | 320 webcam_private::Reset::Params::Create(*args_)); |
| 216 EXTENSION_FUNCTION_VALIDATE(params.get()); | 321 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 217 | 322 |
| 218 Webcam* webcam = WebcamPrivateAPI::Get(browser_context())-> | 323 Webcam* webcam = WebcamPrivateAPI::Get(browser_context())-> |
| 219 GetWebcam(extension_id(), params->webcam_id); | 324 GetWebcam(extension_id(), params->webcam_id); |
| 220 if (!webcam) { | 325 if (!webcam) { |
| (...skipping 16 matching lines...) Expand all Loading... |
| 237 } | 342 } |
| 238 | 343 |
| 239 template <> | 344 template <> |
| 240 void BrowserContextKeyedAPIFactory<WebcamPrivateAPI> | 345 void BrowserContextKeyedAPIFactory<WebcamPrivateAPI> |
| 241 ::DeclareFactoryDependencies() { | 346 ::DeclareFactoryDependencies() { |
| 242 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); | 347 DependsOn(ExtensionsBrowserClient::Get()->GetExtensionSystemFactory()); |
| 243 DependsOn(ProcessManagerFactory::GetInstance()); | 348 DependsOn(ProcessManagerFactory::GetInstance()); |
| 244 } | 349 } |
| 245 | 350 |
| 246 } // namespace extensions | 351 } // namespace extensions |
| OLD | NEW |