12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef PPAPI_SHARED_IMPL_FILE_IO_STATE_MANAGER_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define PPAPI_SHARED_IMPL_FILE_IO_STATE_MANAGER_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/basictypes.h"
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/compiler_specific.h"
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/c/pp_stdint.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "ppapi/shared_impl/ppapi_shared_export.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace ppapi {
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// FileIOStateManager is a helper class that maintains the state of operations.
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// For example, some operations are mutually exclusive, meaning that an
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// operation could be rejected because of the current pending operation. Also,
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// most of the operations only work when the file has been opened.
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class PPAPI_SHARED_EXPORT FileIOStateManager {
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  FileIOStateManager();
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ~FileIOStateManager();
232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  enum OperationType {
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // There is no pending operation right now.
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    OPERATION_NONE,
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If there are pending reads, any other kind of async operation is not
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // allowed.
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    OPERATION_READ,
312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If there are pending writes, any other kind of async operation is not
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // allowed.
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    OPERATION_WRITE,
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If there is a pending operation that is neither read nor write, no
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // further async operation is allowed.
382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    OPERATION_EXCLUSIVE
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  };
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  OperationType get_pending_operation() const { return pending_op_; }
422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetOpenSucceed();
442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called at the beginning of each operation. It is responsible to make sure
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that state is correct. For example, some operations are only valid after
472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // the file is opened, or operations might need to run exclusively.
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // It returns |PP_OK| on success, or |PP_ERROR_...| for various reasons.
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int32_t CheckOperationState(OperationType new_op, bool should_be_open);
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Marks the state of current operations as started or finished.
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetPendingOperation(OperationType op);
542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetOperationFinished();
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) private:
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  int num_pending_ops_;
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  OperationType pending_op_;
592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Set to true when the file has been successfully opened.
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool file_open_;
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FileIOStateManager);
642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace ppapi
672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // PPAPI_SHARED_IMPL_FILE_IO_STATE_MANAGER_H_
69