web_memory_impl.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright 2020 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 COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_WEB_MEMORY_IMPL_H_
  5. #define COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_WEB_MEMORY_IMPL_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/sequence_checker.h"
  10. #include "components/performance_manager/public/mojom/web_memory.mojom.h"
  11. #include "components/performance_manager/public/v8_memory/v8_detailed_memory.h"
  12. #include "components/performance_manager/public/v8_memory/web_memory.h"
  13. #include "third_party/blink/public/common/tokens/tokens.h"
  14. namespace performance_manager {
  15. class FrameNode;
  16. class ProcessNode;
  17. namespace v8_memory {
  18. // A helper class for implementing WebMeasureMemory(). This manages a request
  19. // object that sends a V8 detailed memory request to the renderer, and formats
  20. // the result into a mojom::WebMemoryMeasurement.
  21. class WebMemoryMeasurer {
  22. public:
  23. using MeasurementCallback =
  24. base::OnceCallback<void(mojom::WebMemoryMeasurementPtr)>;
  25. // Implements WebMeasureMemory (from public/v8_memory/web_memory.h) by
  26. // instantiating a WebMemoryMeasurer. |frame_node| must be the last parameter
  27. // so it can be used with base::Bind.
  28. static void MeasureMemory(mojom::WebMemoryMeasurement::Mode mode,
  29. MeasurementCallback callback,
  30. base::WeakPtr<FrameNode> frame_node);
  31. ~WebMemoryMeasurer();
  32. WebMemoryMeasurer(const WebMemoryMeasurer& other) = delete;
  33. WebMemoryMeasurer& operator=(const WebMemoryMeasurer& other) = delete;
  34. V8DetailedMemoryRequestOneShot* request() const {
  35. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  36. return request_.get();
  37. }
  38. // A callback for V8DetailedMemoryRequestOneShot.
  39. void MeasurementComplete(const ProcessNode*,
  40. const V8DetailedMemoryProcessData*);
  41. private:
  42. friend class WebMemoryImplTest;
  43. WebMemoryMeasurer(const blink::LocalFrameToken&,
  44. V8DetailedMemoryRequest::MeasurementMode,
  45. MeasurementCallback);
  46. blink::LocalFrameToken frame_token_;
  47. MeasurementCallback callback_;
  48. std::unique_ptr<V8DetailedMemoryRequestOneShot> request_
  49. GUARDED_BY_CONTEXT(sequence_checker_);
  50. SEQUENCE_CHECKER(sequence_checker_);
  51. };
  52. // The default implementation of WebMeasureMemorySecurityChecker.
  53. class WebMeasureMemorySecurityCheckerImpl
  54. : public WebMeasureMemorySecurityChecker {
  55. public:
  56. WebMeasureMemorySecurityCheckerImpl() = default;
  57. ~WebMeasureMemorySecurityCheckerImpl() override = default;
  58. void CheckMeasureMemoryIsAllowed(
  59. const FrameNode* frame,
  60. MeasureMemoryCallback measure_memory_callback,
  61. mojo::ReportBadMessageCallback bad_message_callback) const override;
  62. };
  63. } // namespace v8_memory
  64. } // namespace performance_manager
  65. #endif // COMPONENTS_PERFORMANCE_MANAGER_V8_MEMORY_WEB_MEMORY_IMPL_H_