| 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 // | |
| 5 #include <fcntl.h> | |
| 6 #include <linux/videodev2.h> | |
| 7 #include <poll.h> | |
| 8 #include <sys/eventfd.h> | |
| 9 #include <sys/ioctl.h> | |
| 10 #include <sys/mman.h> | |
| 11 | 4 |
| 12 #include "base/debug/trace_event.h" | 5 #include "content/browser/shared_worker/shared_worker_instance.h" |
| 13 #include "base/posix/eintr_wrapper.h" | 6 |
| 14 #include "content/common/gpu/media/exynos_v4l2_video_device.h" | 7 #include "base/logging.h" |
| 8 #include "content/browser/worker_host/worker_document_set.h" |
| 15 | 9 |
| 16 namespace content { | 10 namespace content { |
| 17 | 11 |
| 18 namespace { | 12 SharedWorkerInstance::SharedWorkerInstance( |
| 19 const char kDevice[] = "/dev/mfc-dec"; | 13 const GURL& url, |
| 14 const base::string16& name, |
| 15 const base::string16& content_security_policy, |
| 16 blink::WebContentSecurityPolicyType security_policy_type, |
| 17 ResourceContext* resource_context, |
| 18 const WorkerStoragePartition& partition) |
| 19 : url_(url), |
| 20 closed_(false), |
| 21 name_(name), |
| 22 content_security_policy_(content_security_policy), |
| 23 security_policy_type_(security_policy_type), |
| 24 worker_document_set_(new WorkerDocumentSet()), |
| 25 resource_context_(resource_context), |
| 26 partition_(partition), |
| 27 load_failed_(false) { |
| 28 DCHECK(resource_context_); |
| 20 } | 29 } |
| 21 | 30 |
| 22 ExynosV4L2Device::ExynosV4L2Device() | 31 SharedWorkerInstance::~SharedWorkerInstance() { |
| 23 : device_fd_(-1), device_poll_interrupt_fd_(-1) {} | 32 } |
| 24 | 33 |
| 25 ExynosV4L2Device::~ExynosV4L2Device() { | 34 void SharedWorkerInstance::SetMessagePortID( |
| 26 if (device_poll_interrupt_fd_ != -1) { | 35 SharedWorkerMessageFilter* filter, |
| 27 close(device_poll_interrupt_fd_); | 36 int route_id, |
| 28 device_poll_interrupt_fd_ = -1; | 37 int message_port_id) { |
| 29 } | 38 for (FilterList::iterator i = filters_.begin(); i != filters_.end(); ++i) { |
| 30 if (device_fd_ != -1) { | 39 if (i->filter() == filter && i->route_id() == route_id) { |
| 31 close(device_fd_); | 40 i->set_message_port_id(message_port_id); |
| 32 device_fd_ = -1; | 41 return; |
| 42 } |
| 33 } | 43 } |
| 34 } | 44 } |
| 35 | 45 |
| 36 int ExynosV4L2Device::Ioctl(int request, void* arg) { | 46 bool SharedWorkerInstance::Matches(const GURL& match_url, |
| 37 return ioctl(device_fd_, request, arg); | 47 const base::string16& match_name, |
| 48 const WorkerStoragePartition& partition, |
| 49 ResourceContext* resource_context) const { |
| 50 // Only match open shared workers. |
| 51 if (closed_) |
| 52 return false; |
| 53 |
| 54 // ResourceContext equivalence is being used as a proxy to ensure we only |
| 55 // matched shared workers within the same BrowserContext. |
| 56 if (resource_context_ != resource_context) |
| 57 return false; |
| 58 |
| 59 // We must be in the same storage partition otherwise sharing will violate |
| 60 // isolation. |
| 61 if (!partition_.Equals(partition)) |
| 62 return false; |
| 63 |
| 64 if (url_.GetOrigin() != match_url.GetOrigin()) |
| 65 return false; |
| 66 |
| 67 if (name_.empty() && match_name.empty()) |
| 68 return url_ == match_url; |
| 69 |
| 70 return name_ == match_name; |
| 38 } | 71 } |
| 39 | 72 |
| 40 bool ExynosV4L2Device::Poll(bool poll_device, bool* event_pending) { | 73 void SharedWorkerInstance::AddFilter(SharedWorkerMessageFilter* filter, |
| 41 struct pollfd pollfds[2]; | 74 int route_id) { |
| 42 nfds_t nfds; | 75 CHECK(filter); |
| 43 int pollfd = -1; | 76 if (!HasFilter(filter, route_id)) { |
| 44 | 77 FilterInfo info(filter, route_id); |
| 45 pollfds[0].fd = device_poll_interrupt_fd_; | 78 filters_.push_back(info); |
| 46 pollfds[0].events = POLLIN | POLLERR; | |
| 47 nfds = 1; | |
| 48 | |
| 49 if (poll_device) { | |
| 50 DVLOG(3) << "Poll(): adding device fd to poll() set"; | |
| 51 pollfds[nfds].fd = device_fd_; | |
| 52 pollfds[nfds].events = POLLIN | POLLOUT | POLLERR | POLLPRI; | |
| 53 pollfd = nfds; | |
| 54 nfds++; | |
| 55 } | 79 } |
| 56 | |
| 57 if (HANDLE_EINTR(poll(pollfds, nfds, -1)) == -1) { | |
| 58 DPLOG(ERROR) << "poll() failed"; | |
| 59 return false; | |
| 60 } | |
| 61 *event_pending = (pollfd != -1 && pollfds[pollfd].revents & POLLPRI); | |
| 62 return true; | |
| 63 } | 80 } |
| 64 | 81 |
| 65 void* ExynosV4L2Device::Mmap(void* addr, | 82 void SharedWorkerInstance::RemoveFilters(SharedWorkerMessageFilter* filter) { |
| 66 unsigned int len, | 83 for (FilterList::iterator i = filters_.begin(); i != filters_.end();) { |
| 67 int prot, | 84 if (i->filter() == filter) |
| 68 int flags, | 85 i = filters_.erase(i); |
| 69 unsigned int offset) { | 86 else |
| 70 return mmap(addr, len, prot, flags, device_fd_, offset); | 87 ++i; |
| 88 } |
| 71 } | 89 } |
| 72 | 90 |
| 73 void ExynosV4L2Device::Munmap(void* addr, unsigned int len) { | 91 bool SharedWorkerInstance::HasFilter(SharedWorkerMessageFilter* filter, |
| 74 munmap(addr, len); | 92 int route_id) const { |
| 93 for (FilterList::const_iterator i = filters_.begin(); i != filters_.end(); |
| 94 ++i) { |
| 95 if (i->filter() == filter && i->route_id() == route_id) |
| 96 return true; |
| 97 } |
| 98 return false; |
| 75 } | 99 } |
| 76 | 100 |
| 77 bool ExynosV4L2Device::SetDevicePollInterrupt() { | 101 } // namespace content |
| 78 DVLOG(3) << "SetDevicePollInterrupt()"; | |
| 79 | |
| 80 const uint64 buf = 1; | |
| 81 if (HANDLE_EINTR(write(device_poll_interrupt_fd_, &buf, sizeof(buf))) == -1) { | |
| 82 return false; | |
| 83 } | |
| 84 return true; | |
| 85 } | |
| 86 | |
| 87 bool ExynosV4L2Device::ClearDevicePollInterrupt() { | |
| 88 DVLOG(3) << "ClearDevicePollInterrupt()"; | |
| 89 | |
| 90 uint64 buf; | |
| 91 if (HANDLE_EINTR(read(device_poll_interrupt_fd_, &buf, sizeof(buf))) == -1) { | |
| 92 if (errno == EAGAIN) { | |
| 93 // No interrupt flag set, and we're reading nonblocking. Not an error. | |
| 94 return true; | |
| 95 } else { | |
| 96 DPLOG(ERROR) << "ClearDevicePollInterrupt(): read() failed"; | |
| 97 return false; | |
| 98 } | |
| 99 } | |
| 100 return true; | |
| 101 } | |
| 102 | |
| 103 bool ExynosV4L2Device::Initialize() { | |
| 104 DVLOG(2) << "Initialize(): opening device: " << kDevice; | |
| 105 // Open the video device. | |
| 106 device_fd_ = HANDLE_EINTR(open(kDevice, O_RDWR | O_NONBLOCK | O_CLOEXEC)); | |
| 107 if (device_fd_ == -1) { | |
| 108 return false; | |
| 109 } | |
| 110 | |
| 111 device_poll_interrupt_fd_ = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC); | |
| 112 if (device_poll_interrupt_fd_ == -1) { | |
| 113 return false; | |
| 114 } | |
| 115 return true; | |
| 116 } | |
| 117 } // namespace content | |
| OLD | NEW |