Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/utility/media_galleries/ipc_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 IPCDataSource::IPCDataSource(int64 total_size) | |
| 13 : total_size_(total_size), | |
| 14 next_request_id_(0) { | |
| 15 } | |
| 16 | |
| 17 IPCDataSource::~IPCDataSource() {} | |
| 18 | |
| 19 void IPCDataSource::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 IPCDataSource::Stop(const base::Closure& callback) { | |
| 27 callback.Run(); | |
| 28 } | |
| 29 | |
| 30 void IPCDataSource::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(new ChromeUtilityHostMsg_RequestBlobBytes( | |
|
vandebo (ex-Chrome)
2014/01/08 17:48:06
note: I guess this is more correctly named Utility
tommycli
2014/01/08 18:59:37
Yeah that's true. It's hard to name this thing. Ma
| |
| 49 request_id, position, clamped_size)); | |
| 50 } | |
| 51 | |
| 52 bool IPCDataSource::GetSize(int64* size_out) { | |
| 53 *size_out = total_size_; | |
| 54 return true; | |
| 55 } | |
| 56 | |
| 57 bool IPCDataSource::IsStreaming() { | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 void IPCDataSource::SetBitrate(int bitrate) { | |
| 62 } | |
| 63 | |
| 64 bool IPCDataSource::OnMessageReceived(const IPC::Message& message) { | |
| 65 bool handled = true; | |
| 66 IPC_BEGIN_MESSAGE_MAP(IPCDataSource, message) | |
| 67 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RequestBlobBytes_Finished, | |
| 68 OnRequestBlobBytesFinished) | |
| 69 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 70 IPC_END_MESSAGE_MAP() | |
| 71 return handled; | |
| 72 } | |
| 73 | |
| 74 IPCDataSource::Request::Request() | |
| 75 : destination(NULL) { | |
| 76 } | |
| 77 | |
| 78 IPCDataSource::Request::~Request() { | |
| 79 } | |
| 80 | |
| 81 void IPCDataSource::OnRequestBlobBytesFinished( | |
| 82 int64 request_id, | |
| 83 const std::vector<unsigned char>& bytes) { | |
| 84 std::map<int64, Request>::iterator it = pending_requests_.find(request_id); | |
| 85 | |
| 86 if (it == pending_requests_.end()) | |
| 87 return; | |
| 88 | |
| 89 std::copy(bytes.begin(), bytes.end(), it->second.destination); | |
| 90 it->second.callback.Run(bytes.size()); | |
| 91 | |
| 92 pending_requests_.erase(it); | |
| 93 } | |
| 94 | |
| 95 } // namespace metadata | |
| OLD | NEW |