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 #ifndef CHROME_UTILITY_MEDIA_GALLERIES_IPC_DATA_SOURCE_H_
6 #define CHROME_UTILITY_MEDIA_GALLERIES_IPC_DATA_SOURCE_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/threading/thread_checker.h"
13 #include "chrome/utility/utility_message_handler.h"
14 #include "media/base/data_source.h"
22 // Provides the metadata parser with bytes from the browser process via IPC.
23 // Class must be created and destroyed on the utility thread. Class may be used
24 // as a DataSource on a different thread. The utility thread must not be blocked
25 // for read operations to succeed.
26 class IPCDataSource
: public media::DataSource
,
27 public chrome::UtilityMessageHandler
{
29 // May only be called on the utility thread.
30 explicit IPCDataSource(int64 total_size
);
31 virtual ~IPCDataSource();
33 // Implementation of DataSource. These methods may be called on any single
34 // thread. First usage of these methods attaches a thread checker.
35 virtual void set_host(media::DataSourceHost
* host
) OVERRIDE
;
36 virtual void Stop(const base::Closure
& callback
) OVERRIDE
;
37 virtual void Read(int64 position
, int size
, uint8
* data
,
38 const ReadCB
& read_cb
) OVERRIDE
;
39 virtual bool GetSize(int64
* size_out
) OVERRIDE
;
40 virtual bool IsStreaming() OVERRIDE
;
41 virtual void SetBitrate(int bitrate
) OVERRIDE
;
43 // Implementation of UtilityMessageHandler. May only be called on the utility
45 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
55 void ReadOnUtilityThread(int64 position
, int size
, uint8
* data
,
56 const ReadCB
& read_cb
);
58 void OnRequestBlobBytesFinished(int64 request_id
,
59 const std::string
& bytes
);
61 const int64 total_size_
;
63 scoped_refptr
<base::TaskRunner
> utility_task_runner_
;
64 std::map
<int64
, Request
> pending_requests_
;
65 int64 next_request_id_
;
67 base::ThreadChecker utility_thread_checker_
;
69 // Enforces that the DataSource methods are called on one other thread only.
70 base::ThreadChecker data_source_thread_checker_
;
73 } // namespace metadata
75 #endif // CHROME_UTILITY_MEDIA_GALLERIES_IPC_DATA_SOURCE_H_