15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 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 MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <algorithm>
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string>
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <vector>
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/basictypes.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "media/cdm/ppapi/api/content_decryption_module.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/c/ppb_file_io.h"
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/cpp/file_io.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/cpp/file_ref.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/cpp/instance.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/cpp/module.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/cpp/private/isolated_file_system_private.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ppapi/utility/completion_callback_factory.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace media {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Due to PPAPI limitations, all functions must be called on the main thread.
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Implementation notes about states:
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// 1, When a method is called in an invalid state (e.g. Read() before Open() is
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//    called, Write() before Open() finishes or Open() after Open()), kError
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//    will be returned. The state of |this| will not change.
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// 2, When the file is opened by another CDM instance, or when we call Read()/
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//    Write() during a pending Read()/Write(), kInUse will be returned. The
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//    state of |this| will not change.
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// 3, When a pepper operation failed (either synchronously or asynchronously),
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci//    kError will be returned. The state of |this| will be set to ERROR.
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// 4. Any operation in ERROR state will end up with kError.
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class CdmFileIOImpl : public cdm::FileIO {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A class that helps release |file_lock_map_|.
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // There should be only one instance of ResourceTracker in a process. Also,
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // ResourceTracker should outlive all CdmFileIOImpl instances.
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  class ResourceTracker {
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   public:
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ResourceTracker();
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ~ResourceTracker();
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)   private:
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(ResourceTracker);
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // After the first successful file read, call |first_file_read_cb| to report
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // the file size. |first_file_read_cb| takes one parameter: the file size in
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // bytes.
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  CdmFileIOImpl(cdm::FileIOClient* client,
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                PP_Instance pp_instance,
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                const pp::CompletionCallback& first_file_read_cb);
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // cdm::FileIO implementation.
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Open(const char* file_name, uint32_t file_name_size) OVERRIDE;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Read() OVERRIDE;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Write(const uint8_t* data, uint32_t data_size) OVERRIDE;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void Close() OVERRIDE;
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // TODO(xhwang): Introduce more detailed states for UMA logging if needed.
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  enum State {
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    STATE_UNOPENED,
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    STATE_OPENING_FILE_SYSTEM,
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    STATE_FILE_SYSTEM_OPENED,
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    STATE_READING,
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    STATE_WRITING,
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    STATE_CLOSED,
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    STATE_ERROR
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  enum ErrorType {
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OPEN_WHILE_IN_USE,
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    READ_WHILE_IN_USE,
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    WRITE_WHILE_IN_USE,
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OPEN_ERROR,
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    READ_ERROR,
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    WRITE_ERROR
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  };
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Always use Close() to release |this| object.
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~CdmFileIOImpl();
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |file_id_| -> |is_file_lock_acquired_| map.
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Design detail:
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - We never erase an entry from this map.
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - Pros: When the same file is read or written repeatedly, we don't need to
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //   insert/erase the entry repeatedly, which is expensive.
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - Cons: If there are a lot of one-off files used, this map will be
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //   unnecessarily large. But this should be a rare case.
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - Ideally we could use unordered_map for this. But unordered_set is only
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //   available in C++11.
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef std::map<std::string, bool> FileLockMap;
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // File lock map shared by all CdmFileIOImpl objects to prevent read/write
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // race. A CdmFileIOImpl object tries to acquire a lock before opening a
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // file. If the file open failed, the lock is released. Otherwise, the
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // CdmFileIOImpl object holds the lock until Close() is called.
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // TODO(xhwang): Investigate the following cases and make sure we are good:
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - This assumes all CDM instances run in the same process for a given file
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //   system.
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // - When multiple CDM instances are running in different profiles (e.g.
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //   normal/incognito window, multiple profiles), we may be overlocking.
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  static FileLockMap* file_lock_map_;
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets |file_id_|. Returns false if |file_id_| cannot be set (e.g. origin URL
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // cannot be fetched).
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool SetFileID();
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Acquires the file lock. Returns true if the lock is successfully acquired.
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // After the lock is acquired, other cdm::FileIO objects in the same process
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // and in the same origin will get kInUse when trying to open the same file.
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool AcquireFileLock();
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Releases the file lock so that the file can be opened by other cdm::FileIO
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // objects.
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void ReleaseFileLock();
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Helper functions for Open().
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OpenFileSystem();
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnFileSystemOpened(int32_t result, pp::FileSystem file_system);
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Helper functions for Read().
1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void OpenFileForRead();
1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void OnFileOpenedForRead(int32_t result);
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void ReadFile();
1295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnFileRead(int32_t bytes_read);
1305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Helper functions for Write(). We always write data to a temporary file,
1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // then rename the temporary file to the target file. This can prevent data
1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // corruption if |this| is Close()'ed while waiting for writing to complete.
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // However, if Close() is called after OpenTempFileForWrite() but before
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // RenameTempFile(), we may still end up with an empty, partially written or
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // fully written temporary file in the file system. This temporary file will
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // be truncated next time OpenTempFileForWrite() is called.
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void OpenTempFileForWrite();
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void OnTempFileOpenedForWrite(int32_t result);
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void WriteTempFile();
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void OnTempFileWritten(int32_t bytes_written);
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Note: pp::FileRef::Rename() actually does a "move": if the target file
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // exists, Rename() will succeed and the target file will be overwritten.
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // See PepperInternalFileRefBackend::Rename() for implementation detail.
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void RenameTempFile();
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void OnTempFileRenamed(int32_t result);
1481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Reset |this| to a clean state.
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void Reset();
1511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // For real open/read/write errors, Reset() and set the |state_| to ERROR.
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Calls client_->OnXxxxComplete with kError or kInUse asynchronously. In some
1541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // cases we could actually call them synchronously, but since these errors
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // shouldn't happen in normal cases, we are not optimizing such cases.
1565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void OnError(ErrorType error_type);
1575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Callback to notify client of error asynchronously.
1595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void NotifyClientOfError(int32_t result, ErrorType error_type);
1605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  State state_;
1625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Non-owning pointer.
1645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  cdm::FileIOClient* const client_;
1655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const pp::InstanceHandle pp_instance_handle_;
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Format: /<requested_file_name>
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string file_name_;
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A string ID that uniquely identifies a file in the user's profile.
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // It consists of the origin of the document URL (including scheme, host and
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // port, delimited by colons) and the |file_name_|.
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // For example: http:example.com:8080/foo_file.txt
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string file_id_;
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  pp::IsolatedFileSystemPrivate isolated_file_system_;
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  pp::FileSystem file_system_;
1791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Shared between read and write. During read, |file_ref_| refers to the real
1811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // file to read data from. During write, it refers to the temporary file to
1821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // write data into.
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  pp::FileIO file_io_;
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  pp::FileRef file_ref_;
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // A temporary buffer to hold (partial) data to write or the data that has
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // been read. The size of |io_buffer_| is always "bytes to write" or "bytes to
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // read". Use "char" instead of "unit8_t" because PPB_FileIO uses char* for
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // binary data read and write.
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<char> io_buffer_;
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Offset into the file for reading/writing data. When writing data to the
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // file, this is also the offset to the |io_buffer_|.
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  size_t io_offset_;
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Buffer to hold all read data requested. This buffer is passed to |client_|
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // when read completes.
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<char> cumulative_read_buffer_;
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool first_file_read_reported_;
2011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Callback to report the file size in bytes after the first successful read.
2031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  pp::CompletionCallback first_file_read_cb_;
2041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  pp::CompletionCallbackFactory<CdmFileIOImpl> callback_factory_;
2061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(CdmFileIOImpl);
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}  // namespace media
2115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif  // MEDIA_CDM_PPAPI_CDM_FILE_IO_IMPL_H_
213