simple_entry_operation.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include "net/disk_cache/simple/simple_entry_operation.h"
  5. #include <limits.h>
  6. #include "net/base/io_buffer.h"
  7. #include "net/disk_cache/disk_cache.h"
  8. #include "net/disk_cache/simple/simple_entry_impl.h"
  9. namespace disk_cache {
  10. SimpleEntryOperation::SimpleEntryOperation(SimpleEntryOperation&& other) =
  11. default;
  12. SimpleEntryOperation::~SimpleEntryOperation() = default;
  13. // static
  14. SimpleEntryOperation SimpleEntryOperation::OpenOperation(
  15. SimpleEntryImpl* entry,
  16. EntryResultState result_state,
  17. EntryResultCallback callback) {
  18. SimpleEntryOperation op(entry, nullptr, CompletionOnceCallback(), 0, 0, 0,
  19. TYPE_OPEN, INDEX_NOEXIST, 0, false, false);
  20. op.entry_callback_ = std::move(callback);
  21. op.entry_result_state_ = result_state;
  22. return op;
  23. }
  24. // static
  25. SimpleEntryOperation SimpleEntryOperation::CreateOperation(
  26. SimpleEntryImpl* entry,
  27. EntryResultState result_state,
  28. EntryResultCallback callback) {
  29. SimpleEntryOperation op(entry, nullptr, CompletionOnceCallback(), 0, 0, 0,
  30. TYPE_CREATE, INDEX_NOEXIST, 0, false, false);
  31. op.entry_callback_ = std::move(callback);
  32. op.entry_result_state_ = result_state;
  33. return op;
  34. }
  35. // static
  36. SimpleEntryOperation SimpleEntryOperation::OpenOrCreateOperation(
  37. SimpleEntryImpl* entry,
  38. OpenEntryIndexEnum index_state,
  39. EntryResultState result_state,
  40. EntryResultCallback callback) {
  41. SimpleEntryOperation op(entry, nullptr, CompletionOnceCallback(), 0, 0, 0,
  42. TYPE_OPEN_OR_CREATE, index_state, 0, false, false);
  43. op.entry_callback_ = std::move(callback);
  44. op.entry_result_state_ = result_state;
  45. return op;
  46. }
  47. // static
  48. SimpleEntryOperation SimpleEntryOperation::CloseOperation(
  49. SimpleEntryImpl* entry) {
  50. return SimpleEntryOperation(entry, nullptr, CompletionOnceCallback(), 0, 0, 0,
  51. TYPE_CLOSE, INDEX_NOEXIST, 0, false, false);
  52. }
  53. // static
  54. SimpleEntryOperation SimpleEntryOperation::ReadOperation(
  55. SimpleEntryImpl* entry,
  56. int index,
  57. int offset,
  58. int length,
  59. net::IOBuffer* buf,
  60. CompletionOnceCallback callback) {
  61. return SimpleEntryOperation(entry, buf, std::move(callback), offset, 0,
  62. length, TYPE_READ, INDEX_NOEXIST, index, false,
  63. false);
  64. }
  65. // static
  66. SimpleEntryOperation SimpleEntryOperation::WriteOperation(
  67. SimpleEntryImpl* entry,
  68. int index,
  69. int offset,
  70. int length,
  71. net::IOBuffer* buf,
  72. bool truncate,
  73. bool optimistic,
  74. CompletionOnceCallback callback) {
  75. return SimpleEntryOperation(entry, buf, std::move(callback), offset, 0,
  76. length, TYPE_WRITE, INDEX_NOEXIST, index,
  77. truncate, optimistic);
  78. }
  79. // static
  80. SimpleEntryOperation SimpleEntryOperation::ReadSparseOperation(
  81. SimpleEntryImpl* entry,
  82. int64_t sparse_offset,
  83. int length,
  84. net::IOBuffer* buf,
  85. CompletionOnceCallback callback) {
  86. return SimpleEntryOperation(entry, buf, std::move(callback), 0, sparse_offset,
  87. length, TYPE_READ_SPARSE, INDEX_NOEXIST, 0, false,
  88. false);
  89. }
  90. // static
  91. SimpleEntryOperation SimpleEntryOperation::WriteSparseOperation(
  92. SimpleEntryImpl* entry,
  93. int64_t sparse_offset,
  94. int length,
  95. net::IOBuffer* buf,
  96. CompletionOnceCallback callback) {
  97. return SimpleEntryOperation(entry, buf, std::move(callback), 0, sparse_offset,
  98. length, TYPE_WRITE_SPARSE, INDEX_NOEXIST, 0,
  99. false, false);
  100. }
  101. // static
  102. SimpleEntryOperation SimpleEntryOperation::GetAvailableRangeOperation(
  103. SimpleEntryImpl* entry,
  104. int64_t sparse_offset,
  105. int length,
  106. RangeResultCallback callback) {
  107. SimpleEntryOperation op(entry, nullptr, CompletionOnceCallback(), 0,
  108. sparse_offset, length, TYPE_GET_AVAILABLE_RANGE,
  109. INDEX_NOEXIST, 0, false, false);
  110. op.range_callback_ = std::move(callback);
  111. return op;
  112. }
  113. // static
  114. SimpleEntryOperation SimpleEntryOperation::DoomOperation(
  115. SimpleEntryImpl* entry,
  116. net::CompletionOnceCallback callback) {
  117. net::IOBuffer* const buf = nullptr;
  118. const int offset = 0;
  119. const int64_t sparse_offset = 0;
  120. const int length = 0;
  121. const OpenEntryIndexEnum index_state = INDEX_NOEXIST;
  122. const int index = 0;
  123. const bool truncate = false;
  124. const bool optimistic = false;
  125. return SimpleEntryOperation(entry, buf, std::move(callback), offset,
  126. sparse_offset, length, TYPE_DOOM, index_state,
  127. index, truncate, optimistic);
  128. }
  129. SimpleEntryOperation::SimpleEntryOperation(SimpleEntryImpl* entry,
  130. net::IOBuffer* buf,
  131. net::CompletionOnceCallback callback,
  132. int offset,
  133. int64_t sparse_offset,
  134. int length,
  135. EntryOperationType type,
  136. OpenEntryIndexEnum index_state,
  137. int index,
  138. bool truncate,
  139. bool optimistic)
  140. : entry_(entry),
  141. buf_(buf),
  142. callback_(std::move(callback)),
  143. offset_(offset),
  144. sparse_offset_(sparse_offset),
  145. length_(length),
  146. type_(type),
  147. index_state_(index_state),
  148. index_(index),
  149. truncate_(truncate),
  150. optimistic_(optimistic) {}
  151. } // namespace disk_cache