simple_entry_operation.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Copyright 2013 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. #ifndef NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_OPERATION_H_
  5. #define NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_OPERATION_H_
  6. #include <stdint.h>
  7. #include "base/memory/ref_counted.h"
  8. #include "net/base/completion_once_callback.h"
  9. #include "net/disk_cache/disk_cache.h"
  10. #include "net/disk_cache/simple/simple_histogram_enums.h"
  11. namespace net {
  12. class IOBuffer;
  13. }
  14. namespace disk_cache {
  15. class SimpleEntryImpl;
  16. // SimpleEntryOperation stores the information regarding operations in
  17. // SimpleEntryImpl, between the moment they are issued by users of the backend,
  18. // and the moment when they are executed.
  19. class SimpleEntryOperation {
  20. public:
  21. typedef net::CompletionOnceCallback CompletionOnceCallback;
  22. enum EntryOperationType {
  23. TYPE_OPEN = 0,
  24. TYPE_CREATE = 1,
  25. TYPE_OPEN_OR_CREATE = 2,
  26. TYPE_CLOSE = 3,
  27. TYPE_READ = 4,
  28. TYPE_WRITE = 5,
  29. TYPE_READ_SPARSE = 6,
  30. TYPE_WRITE_SPARSE = 7,
  31. TYPE_GET_AVAILABLE_RANGE = 8,
  32. TYPE_DOOM = 9,
  33. };
  34. // Whether an open/create method has returned an entry (optimistically)
  35. // already, or if it still needs to be delivered via a callback.
  36. enum EntryResultState {
  37. ENTRY_ALREADY_RETURNED = 0,
  38. ENTRY_NEEDS_CALLBACK = 1,
  39. };
  40. SimpleEntryOperation(SimpleEntryOperation&& other);
  41. ~SimpleEntryOperation();
  42. static SimpleEntryOperation OpenOperation(SimpleEntryImpl* entry,
  43. EntryResultState result_state,
  44. EntryResultCallback);
  45. static SimpleEntryOperation CreateOperation(SimpleEntryImpl* entry,
  46. EntryResultState result_state,
  47. EntryResultCallback);
  48. static SimpleEntryOperation OpenOrCreateOperation(
  49. SimpleEntryImpl* entry,
  50. OpenEntryIndexEnum index_state,
  51. EntryResultState result_state,
  52. EntryResultCallback);
  53. static SimpleEntryOperation CloseOperation(SimpleEntryImpl* entry);
  54. static SimpleEntryOperation ReadOperation(SimpleEntryImpl* entry,
  55. int index,
  56. int offset,
  57. int length,
  58. net::IOBuffer* buf,
  59. CompletionOnceCallback callback);
  60. static SimpleEntryOperation WriteOperation(SimpleEntryImpl* entry,
  61. int index,
  62. int offset,
  63. int length,
  64. net::IOBuffer* buf,
  65. bool truncate,
  66. bool optimistic,
  67. CompletionOnceCallback callback);
  68. static SimpleEntryOperation ReadSparseOperation(
  69. SimpleEntryImpl* entry,
  70. int64_t sparse_offset,
  71. int length,
  72. net::IOBuffer* buf,
  73. CompletionOnceCallback callback);
  74. static SimpleEntryOperation WriteSparseOperation(
  75. SimpleEntryImpl* entry,
  76. int64_t sparse_offset,
  77. int length,
  78. net::IOBuffer* buf,
  79. CompletionOnceCallback callback);
  80. static SimpleEntryOperation GetAvailableRangeOperation(
  81. SimpleEntryImpl* entry,
  82. int64_t sparse_offset,
  83. int length,
  84. RangeResultCallback callback);
  85. static SimpleEntryOperation DoomOperation(SimpleEntryImpl* entry,
  86. CompletionOnceCallback callback);
  87. EntryOperationType type() const {
  88. return static_cast<EntryOperationType>(type_);
  89. }
  90. CompletionOnceCallback ReleaseCallback() { return std::move(callback_); }
  91. EntryResultCallback ReleaseEntryResultCallback() {
  92. return std::move(entry_callback_);
  93. }
  94. RangeResultCallback ReleaseRangeResultCalback() {
  95. return std::move(range_callback_);
  96. }
  97. EntryResultState entry_result_state() { return entry_result_state_; }
  98. OpenEntryIndexEnum index_state() const { return index_state_; }
  99. int index() const { return index_; }
  100. int offset() const { return offset_; }
  101. int64_t sparse_offset() const { return sparse_offset_; }
  102. int length() const { return length_; }
  103. net::IOBuffer* buf() { return buf_.get(); }
  104. bool truncate() const { return truncate_; }
  105. bool optimistic() const { return optimistic_; }
  106. private:
  107. SimpleEntryOperation(SimpleEntryImpl* entry,
  108. net::IOBuffer* buf,
  109. CompletionOnceCallback callback,
  110. int offset,
  111. int64_t sparse_offset,
  112. int length,
  113. EntryOperationType type,
  114. OpenEntryIndexEnum index_state,
  115. int index,
  116. bool truncate,
  117. bool optimistic);
  118. // This ensures entry will not be deleted until the operation has ran.
  119. scoped_refptr<SimpleEntryImpl> entry_;
  120. scoped_refptr<net::IOBuffer> buf_;
  121. CompletionOnceCallback callback_;
  122. // Used in open and create operations.
  123. EntryResultCallback entry_callback_;
  124. EntryResultState entry_result_state_;
  125. // Used in write and read operations.
  126. const int offset_;
  127. const int64_t sparse_offset_;
  128. const int length_;
  129. // Used in get available range operations.
  130. RangeResultCallback range_callback_;
  131. const EntryOperationType type_;
  132. // Used in the "open or create" operation.
  133. const OpenEntryIndexEnum index_state_;
  134. // Used in write and read operations.
  135. const unsigned int index_;
  136. // Used only in write operations.
  137. const bool truncate_;
  138. const bool optimistic_;
  139. };
  140. } // namespace disk_cache
  141. #endif // NET_DISK_CACHE_SIMPLE_SIMPLE_ENTRY_OPERATION_H_