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::Stop(const base::Closure
& callback
) {
25 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
29 void IPCDataSource::Read(int64 position
, int size
, uint8
* data
,
30 const DataSource::ReadCB
& read_cb
) {
31 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
32 utility_task_runner_
->PostTask(
34 base::Bind(&IPCDataSource::ReadOnUtilityThread
, base::Unretained(this),
35 position
, size
, data
, read_cb
));
38 bool IPCDataSource::GetSize(int64
* size_out
) {
39 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
40 *size_out
= total_size_
;
44 bool IPCDataSource::IsStreaming() {
45 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
49 void IPCDataSource::SetBitrate(int bitrate
) {
50 DCHECK(data_source_thread_checker_
.CalledOnValidThread());
53 bool IPCDataSource::OnMessageReceived(const IPC::Message
& message
) {
54 DCHECK(utility_thread_checker_
.CalledOnValidThread());
56 IPC_BEGIN_MESSAGE_MAP(IPCDataSource
, message
)
57 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_RequestBlobBytes_Finished
,
58 OnRequestBlobBytesFinished
)
59 IPC_MESSAGE_UNHANDLED(handled
= false)
64 IPCDataSource::Request::Request()
68 IPCDataSource::Request::~Request() {
71 void IPCDataSource::ReadOnUtilityThread(int64 position
, int size
, uint8
* data
,
72 const DataSource::ReadCB
& read_cb
) {
73 DCHECK(utility_thread_checker_
.CalledOnValidThread());
74 CHECK_GE(total_size_
, 0);
75 CHECK_GE(position
, 0);
78 // Cap position and size within bounds.
79 position
= std::min(position
, total_size_
);
81 std::min(static_cast<int64
>(size
), total_size_
- position
);
83 int64 request_id
= ++next_request_id_
;
86 request
.destination
= data
;
87 request
.callback
= read_cb
;
89 pending_requests_
[request_id
] = request
;
90 content::UtilityThread::Get()->Send(new ChromeUtilityHostMsg_RequestBlobBytes(
91 request_id
, position
, clamped_size
));
94 void IPCDataSource::OnRequestBlobBytesFinished(int64 request_id
,
95 const std::string
& bytes
) {
96 DCHECK(utility_thread_checker_
.CalledOnValidThread());
97 std::map
<int64
, Request
>::iterator it
= pending_requests_
.find(request_id
);
99 if (it
== pending_requests_
.end())
102 std::copy(bytes
.begin(), bytes
.end(), it
->second
.destination
);
103 it
->second
.callback
.Run(bytes
.size());
105 pending_requests_
.erase(it
);
108 } // namespace metadata