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"
12 IPCDataSource::IPCDataSource(int64 total_size
)
13 : total_size_(total_size
),
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
) {
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);
36 // Cap position and size within bounds.
37 position
= std::min(position
, total_size_
);
39 std::min(static_cast<int64
>(size
), total_size_
- position
);
41 int64 request_id
= ++next_request_id_
;
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_
;
57 bool IPCDataSource::IsStreaming() {
61 void IPCDataSource::SetBitrate(int bitrate
) {
64 bool IPCDataSource::OnMessageReceived(const IPC::Message
& message
) {
66 IPC_BEGIN_MESSAGE_MAP(IPCDataSource
, message
)
67 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RequestBlobBytes_Finished
,
68 OnRequestBlobBytesFinished
)
69 IPC_MESSAGE_UNHANDLED(handled
= false)
74 IPCDataSource::Request::Request()
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())
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