web_engine_memory_inspector.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2021 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 FUCHSIA_WEB_WEBENGINE_BROWSER_WEB_ENGINE_MEMORY_INSPECTOR_H_
  5. #define FUCHSIA_WEB_WEBENGINE_BROWSER_WEB_ENGINE_MEMORY_INSPECTOR_H_
  6. #include <lib/inspect/cpp/vmo/types.h>
  7. #include <memory>
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/time/time.h"
  10. #include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"
  11. namespace fpromise {
  12. class context;
  13. }
  14. // Integrates with MemoryInstrumentation to publish memory details to Inspect
  15. // on-demand.
  16. class WebEngineMemoryInspector {
  17. public:
  18. // Creates a lazily-populated node called "memory" under |parent_node|.
  19. explicit WebEngineMemoryInspector(inspect::Node& parent_node);
  20. ~WebEngineMemoryInspector();
  21. private:
  22. // Returns the result of attempting to resolve the memory dump promise:
  23. // - If the dump is available then the contents are serialized and returned.
  24. // - Otherwise the |context| is suspended, a fresh dump requested, and a
  25. // pending result returned. The |context| will be resumed only when the
  26. // dump request completes.
  27. // This is used as a continuation function for promises generated by the
  28. // LazyNode's callback.
  29. fpromise::result<inspect::Inspector> ResolveMemoryDumpPromise(
  30. fpromise::context& context);
  31. // Handles completion of a memory dump request.
  32. void OnMemoryDumpComplete(
  33. base::TimeTicks requested_at,
  34. fpromise::suspended_task task,
  35. bool success,
  36. memory_instrumentation::mojom::GlobalMemoryDumpPtr raw_dump);
  37. // Node holding memory usage information.
  38. inspect::LazyNode node_;
  39. // Holds an Inspector populated with data from the memory dump received by
  40. // OnMemoryDumpComplete(), until the promise continuation is called.
  41. std::unique_ptr<inspect::Inspector> dump_results_;
  42. const base::WeakPtrFactory<WebEngineMemoryInspector> weak_this_{this};
  43. };
  44. #endif // FUCHSIA_WEB_WEBENGINE_BROWSER_WEB_ENGINE_MEMORY_INSPECTOR_H_