15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef CHROME_UTILITY_MEDIA_GALLERIES_IPC_DATA_SOURCE_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define CHROME_UTILITY_MEDIA_GALLERIES_IPC_DATA_SOURCE_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <map>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/ref_counted.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/threading/thread_checker.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/utility/utility_message_handler.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/base/data_source.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace base {
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class TaskRunner;
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace metadata {
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Provides the metadata parser with bytes from the browser process via IPC.
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Class must be created and destroyed on the utility thread. Class may be used
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// as a DataSource on a different thread. The utility thread must not be blocked
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// for read operations to succeed.
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class IPCDataSource: public media::DataSource,
27f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                     public UtilityMessageHandler {
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // May only be called on the utility thread.
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  explicit IPCDataSource(int64 total_size);
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~IPCDataSource();
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Implementation of DataSource. These methods may be called on any single
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // thread. First usage of these methods attaches a thread checker.
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  virtual void Stop() OVERRIDE;
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Read(int64 position, int size, uint8* data,
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                    const ReadCB& read_cb) OVERRIDE;
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool GetSize(int64* size_out) OVERRIDE;
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool IsStreaming() OVERRIDE;
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void SetBitrate(int bitrate) OVERRIDE;
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Implementation of UtilityMessageHandler. May only be called on the utility
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // thread.
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  struct Request {
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Request();
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ~Request();
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    uint8* destination;
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ReadCB callback;
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void ReadOnUtilityThread(int64 position, int size, uint8* data,
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                           const ReadCB& read_cb);
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnRequestBlobBytesFinished(int64 request_id,
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                  const std::string& bytes);
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const int64 total_size_;
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_refptr<base::TaskRunner> utility_task_runner_;
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::map<int64, Request> pending_requests_;
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int64 next_request_id_;
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::ThreadChecker utility_thread_checker_;
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Enforces that the DataSource methods are called on one other thread only.
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::ThreadChecker data_source_thread_checker_;
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace metadata
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // CHROME_UTILITY_MEDIA_GALLERIES_IPC_DATA_SOURCE_H_
75