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 "base/message_loop/message_loop_proxy.h"
8 #include "chrome/common/chrome_utility_messages.h"
9 #include "content/public/utility/utility_thread.h"
13 IPCDataSource::IPCDataSource(int64 total_size
)
14 : total_size_(total_size
),
15 utility_task_runner_(base::MessageLoopProxy::current()),
17 data_source_thread_checker_
.DetachFromThread();
20 IPCDataSource::~IPCDataSource() {
21 DCHECK(utility_thread_checker_
.CalledOnValidThread());
24 void IPCDataSource::set_host(media::DataSourceHost
* host
) {
25 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
26 DataSource::set_host(host
);
27 if (media::DataSource::host()) {
28 media::DataSource::host()->SetTotalBytes(total_size_
);
32 void IPCDataSource::Stop(const base::Closure
& callback
) {
33 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
37 void IPCDataSource::Read(int64 position
, int size
, uint8
* data
,
38 const DataSource::ReadCB
& read_cb
) {
39 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
40 utility_task_runner_
->PostTask(
42 base::Bind(&IPCDataSource::ReadOnUtilityThread
, base::Unretained(this),
43 position
, size
, data
, read_cb
));
46 bool IPCDataSource::GetSize(int64
* size_out
) {
47 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
48 *size_out
= total_size_
;
52 bool IPCDataSource::IsStreaming() {
53 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
57 void IPCDataSource::SetBitrate(int bitrate
) {
58 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
61 bool IPCDataSource::OnMessageReceived(const IPC::Message
& message
) {
62 DCHECK(utility_thread_checker_
.CalledOnValidThread());
64 IPC_BEGIN_MESSAGE_MAP(IPCDataSource
, message
)
65 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RequestBlobBytes_Finished
,
66 OnRequestBlobBytesFinished
)
67 IPC_MESSAGE_UNHANDLED(handled
= false)
72 IPCDataSource::Request::Request()
76 IPCDataSource::Request::~Request() {
79 void IPCDataSource::ReadOnUtilityThread(int64 position
, int size
, uint8
* data
,
80 const DataSource::ReadCB
& read_cb
) {
81 DCHECK(utility_thread_checker_
.CalledOnValidThread());
82 CHECK_GE(total_size_
, 0);
83 CHECK_GE(position
, 0);
86 // Cap position and size within bounds.
87 position
= std::min(position
, total_size_
);
89 std::min(static_cast<int64
>(size
), total_size_
- position
);
91 int64 request_id
= ++next_request_id_
;
94 request
.destination
= data
;
95 request
.callback
= read_cb
;
97 pending_requests_
[request_id
] = request
;
98 content::UtilityThread::Get()->Send(new ChromeUtilityHostMsg_RequestBlobBytes(
99 request_id
, position
, clamped_size
));
102 void IPCDataSource::OnRequestBlobBytesFinished(int64 request_id
,
103 const std::string
& bytes
) {
104 DCHECK(utility_thread_checker_
.CalledOnValidThread());
105 std::map
<int64
, Request
>::iterator it
= pending_requests_
.find(request_id
);
107 if (it
== pending_requests_
.end())
110 std::copy(bytes
.begin(), bytes
.end(), it
->second
.destination
);
111 it
->second
.callback
.Run(bytes
.size());
113 pending_requests_
.erase(it
);
116 } // namespace metadata