OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/utility/media_galleries/ipc_tunnel_data_source.h" |
| 6 |
| 7 #include "chrome/common/chrome_utility_messages.h" |
| 8 #include "content/public/utility/utility_thread.h" |
| 9 |
| 10 namespace metadata { |
| 11 |
| 12 IPCTunnelDataSource::IPCTunnelDataSource(int64 total_size) |
| 13 : total_size_(total_size), |
| 14 next_request_id_(0) { |
| 15 } |
| 16 |
| 17 IPCTunnelDataSource::~IPCTunnelDataSource() {} |
| 18 |
| 19 void IPCTunnelDataSource::set_host(media::DataSourceHost* host) { |
| 20 DataSource::set_host(host); |
| 21 if (media::DataSource::host()) { |
| 22 media::DataSource::host()->SetTotalBytes(total_size_); |
| 23 } |
| 24 } |
| 25 |
| 26 void IPCTunnelDataSource::Stop(const base::Closure& callback) { |
| 27 callback.Run(); |
| 28 } |
| 29 |
| 30 void IPCTunnelDataSource::Read(int64 position, int size, uint8* data, |
| 31 const DataSource::ReadCB& read_cb) { |
| 32 CHECK_GE(total_size_, 0); |
| 33 CHECK_GE(position, 0); |
| 34 CHECK_GE(size, 0); |
| 35 |
| 36 // Cap position and size within bounds. |
| 37 position = std::min(position, total_size_); |
| 38 int64 clamped_size = |
| 39 std::min(static_cast<int64>(size), total_size_ - position); |
| 40 |
| 41 int64 request_id = ++next_request_id_; |
| 42 |
| 43 Request request; |
| 44 request.destination = data; |
| 45 request.callback = read_cb; |
| 46 |
| 47 pending_requests_[request_id] = request; |
| 48 content::UtilityThread::Get()->Send( |
| 49 new ChromeUtilityHostMsg_ParseMediaMetadata_RequestBlobBytes( |
| 50 request_id, position, clamped_size)); |
| 51 } |
| 52 |
| 53 bool IPCTunnelDataSource::GetSize(int64* size_out) { |
| 54 *size_out = total_size_; |
| 55 return true; |
| 56 } |
| 57 |
| 58 bool IPCTunnelDataSource::IsStreaming() { |
| 59 return false; |
| 60 } |
| 61 |
| 62 void IPCTunnelDataSource::SetBitrate(int bitrate) { |
| 63 } |
| 64 |
| 65 void IPCTunnelDataSource::ReceiveBytes(int64 request_id, |
| 66 const std::string& bytes) { |
| 67 std::map<int64, Request>::iterator it = pending_requests_.find(request_id); |
| 68 |
| 69 if (it == pending_requests_.end()) |
| 70 return; |
| 71 |
| 72 std::copy(bytes.begin(), bytes.end(), it->second.destination); |
| 73 it->second.callback.Run(bytes.size()); |
| 74 |
| 75 pending_requests_.erase(it); |
| 76 } |
| 77 |
| 78 IPCTunnelDataSource::Request::Request() |
| 79 : destination(NULL) { |
| 80 } |
| 81 |
| 82 IPCTunnelDataSource::Request::~Request() { |
| 83 } |
| 84 |
| 85 } // namespace metadata |
OLD | NEW |