1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55

fuchsia_web / webengine / browser / web_engine_memory_inspector.h [blame]

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef FUCHSIA_WEB_WEBENGINE_BROWSER_WEB_ENGINE_MEMORY_INSPECTOR_H_
#define FUCHSIA_WEB_WEBENGINE_BROWSER_WEB_ENGINE_MEMORY_INSPECTOR_H_

#include <lib/inspect/cpp/vmo/types.h>
#include <memory>

#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "services/resource_coordinator/public/mojom/memory_instrumentation/memory_instrumentation.mojom.h"

namespace fpromise {
class context;
}

// Integrates with MemoryInstrumentation to publish memory details to Inspect
// on-demand.
class WebEngineMemoryInspector {
 public:
  // Creates a lazily-populated node called "memory" under |parent_node|.
  explicit WebEngineMemoryInspector(inspect::Node& parent_node);
  ~WebEngineMemoryInspector();

 private:
  // Returns the result of attempting to resolve the memory dump promise:
  // - If the dump is available then the contents are serialized and returned.
  // - Otherwise the |context| is suspended, a fresh dump requested, and a
  //   pending result returned. The |context| will be resumed only when the
  //   dump request completes.
  // This is used as a continuation function for promises generated by the
  // LazyNode's callback.
  fpromise::result<inspect::Inspector> ResolveMemoryDumpPromise(
      fpromise::context& context);

  // Handles completion of a memory dump request.
  void OnMemoryDumpComplete(
      base::TimeTicks requested_at,
      fpromise::suspended_task task,
      bool success,
      memory_instrumentation::mojom::GlobalMemoryDumpPtr raw_dump);

  // Node holding memory usage information.
  inspect::LazyNode node_;

  // Holds an Inspector populated with data from the memory dump received by
  // OnMemoryDumpComplete(), until the promise continuation is called.
  std::unique_ptr<inspect::Inspector> dump_results_;

  const base::WeakPtrFactory<WebEngineMemoryInspector> weak_this_{this};
};

#endif  // FUCHSIA_WEB_WEBENGINE_BROWSER_WEB_ENGINE_MEMORY_INSPECTOR_H_