file_io_state_manager.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
1// Copyright (c) 2012 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.
4
5#ifndef PPAPI_SHARED_IMPL_FILE_IO_STATE_MANAGER_H_
6#define PPAPI_SHARED_IMPL_FILE_IO_STATE_MANAGER_H_
7
8#include "base/basictypes.h"
9#include "base/compiler_specific.h"
10#include "ppapi/c/pp_stdint.h"
11#include "ppapi/shared_impl/ppapi_shared_export.h"
12
13namespace ppapi {
14
15// FileIOStateManager is a helper class that maintains the state of operations.
16// For example, some operations are mutually exclusive, meaning that an
17// operation could be rejected because of the current pending operation. Also,
18// most of the operations only work when the file has been opened.
19class PPAPI_SHARED_EXPORT FileIOStateManager {
20 public:
21  FileIOStateManager();
22  ~FileIOStateManager();
23
24  enum OperationType {
25    // There is no pending operation right now.
26    OPERATION_NONE,
27
28    // If there are pending reads, any other kind of async operation is not
29    // allowed.
30    OPERATION_READ,
31
32    // If there are pending writes, any other kind of async operation is not
33    // allowed.
34    OPERATION_WRITE,
35
36    // If there is a pending operation that is neither read nor write, no
37    // further async operation is allowed.
38    OPERATION_EXCLUSIVE
39  };
40
41  OperationType get_pending_operation() const { return pending_op_; }
42
43  void SetOpenSucceed();
44
45  // Called at the beginning of each operation. It is responsible to make sure
46  // that state is correct. For example, some operations are only valid after
47  // the file is opened, or operations might need to run exclusively.
48  //
49  // It returns |PP_OK| on success, or |PP_ERROR_...| for various reasons.
50  int32_t CheckOperationState(OperationType new_op, bool should_be_open);
51
52  // Marks the state of current operations as started or finished.
53  void SetPendingOperation(OperationType op);
54  void SetOperationFinished();
55
56 private:
57  int num_pending_ops_;
58  OperationType pending_op_;
59
60  // Set to true when the file has been successfully opened.
61  bool file_open_;
62
63  DISALLOW_COPY_AND_ASSIGN(FileIOStateManager);
64};
65
66}  // namespace ppapi
67
68#endif  // PPAPI_SHARED_IMPL_FILE_IO_STATE_MANAGER_H_
69