NaCl: Update revision in DEPS, r12770 -> r12773
[chromium-blink-merge.git] / chrome / utility / media_galleries / ipc_data_source.cc
blob0fd69012913e5f655847039ecbc98795fac6222d
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.
5 #include "chrome/utility/media_galleries/ipc_data_source.h"
7 #include "chrome/common/chrome_utility_messages.h"
8 #include "content/public/utility/utility_thread.h"
10 namespace metadata {
12 IPCDataSource::IPCDataSource(int64 total_size)
13 : total_size_(total_size),
14 next_request_id_(0) {
17 IPCDataSource::~IPCDataSource() {}
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_);
26 void IPCDataSource::Stop(const base::Closure& callback) {
27 callback.Run();
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);
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);
41 int64 request_id = ++next_request_id_;
43 Request request;
44 request.destination = data;
45 request.callback = read_cb;
47 pending_requests_[request_id] = request;
48 content::UtilityThread::Get()->Send(new ChromeUtilityHostMsg_RequestBlobBytes(
49 request_id, position, clamped_size));
52 bool IPCDataSource::GetSize(int64* size_out) {
53 *size_out = total_size_;
54 return true;
57 bool IPCDataSource::IsStreaming() {
58 return false;
61 void IPCDataSource::SetBitrate(int bitrate) {
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;
74 IPCDataSource::Request::Request()
75 : destination(NULL) {
78 IPCDataSource::Request::~Request() {
81 void IPCDataSource::OnRequestBlobBytesFinished(int64 request_id,
82 const std::string& bytes) {
83 std::map<int64, Request>::iterator it = pending_requests_.find(request_id);
85 if (it == pending_requests_.end())
86 return;
88 std::copy(bytes.begin(), bytes.end(), it->second.destination);
89 it->second.callback.Run(bytes.size());
91 pending_requests_.erase(it);
94 } // namespace metadata