network_service_memory_cache_writer.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2022 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 SERVICES_NETWORK_NETWORK_SERVICE_MEMORY_CACHE_WRITER_H_
  5. #define SERVICES_NETWORK_NETWORK_SERVICE_MEMORY_CACHE_WRITER_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "net/base/transport_info.h"
  11. #include "services/network/public/mojom/url_loader_completion_status.mojom.h"
  12. #include "services/network/public/mojom/url_response_head.mojom.h"
  13. namespace net {
  14. class URLRequest;
  15. } // namespace net
  16. namespace network {
  17. class NetworkServiceMemoryCache;
  18. // Duplicates an HTTP response that comes from the underlying layer, i.e., a
  19. // URLLoader. The URLLoader owns an instance of this class.
  20. //
  21. // When the URLLoader completed successfully, the duplicated response is stored
  22. // into the in-memory cache so that it can be served without disk access until
  23. // it gets evicted.
  24. class COMPONENT_EXPORT(NETWORK_SERVICE) NetworkServiceMemoryCacheWriter {
  25. public:
  26. NetworkServiceMemoryCacheWriter(
  27. base::WeakPtr<NetworkServiceMemoryCache> cache,
  28. uint64_t trace_id,
  29. size_t max_bytes,
  30. std::string cache_key,
  31. net::URLRequest* request,
  32. mojom::RequestDestination request_destination,
  33. const net::TransportInfo& transport_info,
  34. const mojom::URLResponseHeadPtr& response_head);
  35. ~NetworkServiceMemoryCacheWriter();
  36. // Called when the owner received response content. Returns false when `this`
  37. // doesn't want to duplicate the response any longer (e.g. the content is too
  38. // large).
  39. bool OnDataRead(const char* buf, int result);
  40. // Called when the owner completed.
  41. void OnCompleted(const URLLoaderCompletionStatus& status);
  42. private:
  43. base::WeakPtr<NetworkServiceMemoryCache> cache_;
  44. // Used for tracing.
  45. const uint64_t trace_id_;
  46. // The maximum size of `received_data_`.
  47. const size_t max_bytes_;
  48. std::string cache_key_;
  49. // `url_request_` must outlive `this`. The owner owns `url_request_`.
  50. const raw_ptr<net::URLRequest> url_request_;
  51. mojom::RequestDestination request_destination_;
  52. const net::TransportInfo transport_info_;
  53. mojom::URLResponseHeadPtr response_head_;
  54. std::vector<unsigned char> received_data_;
  55. };
  56. } // namespace network
  57. #endif // SERVICES_NETWORK_NETWORK_SERVICE_MEMORY_CACHE_WRITER_H_