[M120 Migration]Fix for crash during chrome exit
[platform/framework/web/chromium-efl.git] / chrome / browser / process_resource_usage.h
1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_
6 #define CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_
7
8 #include <stddef.h>
9
10 #include "base/containers/circular_deque.h"
11 #include "base/functional/callback.h"
12 #include "base/threading/thread_checker.h"
13 #include "content/public/common/resource_usage_reporter.mojom.h"
14 #include "mojo/public/cpp/bindings/pending_remote.h"
15 #include "mojo/public/cpp/bindings/remote.h"
16 #include "third_party/blink/public/common/web_cache/web_cache_resource_type_stats.h"
17
18 // Provides resource usage information about a child process.
19 //
20 // This is a wrapper around the content::mojom::ResourceUsageReporter Mojo
21 // service that exposes
22 // information about resources used by a child process. Currently, this is only
23 // V8 memory and Blink resource cache usage, but could be expanded to include
24 // other resources.  This is intended for status viewers such as the task
25 // manager.
26 //
27 // To create:
28 // 1. Create a mojo::PendingRemote<content::mojom::ResourceUsageReporter> and
29 //    obtain a mojo::PendingReceiver<> using InitWithNewPipeAndPassReceiver().
30 // 2. Use the child process's service registry to connect to the service using
31 //    the mojo::PendingReceiver<>. Note, ServiceRegistry is thread hostile and
32 //    must always be accessed from the same thread. However, PendingReceiver<>
33 //    can be passed safely between threads, and therefore a task can be posted
34 //    to the ServiceRegistry thread to connect to the remote service.
35 // 3. Pass the mojo::PendingRemote<content::mojom::ResourceUsageReporter> to the
36 //    constructor.
37 //
38 // Example:
39 //   void Foo::ConnectToService(
40 //       mojo::PendingReceiver<content::mojom::ResourceUsageReporter>
41 //           receiver) {
42 //     content::ServiceRegistry* registry = host_->GetServiceRegistry();
43 //     registry->ConnectToRemoteService(std::move(req));
44 //   }
45 //
46 //   ...
47 //     mojo::PendingRemote<content::mojom::ResourceUsageReporter> service;
48 //     mojo::PendingReceiver<content::mojom::ResourceUsageReporter> receiver =
49 //         service.InitWithNewPipeAndPassReceiver();
50 //     content::GetIOThreadTaskRunner({})->PostTask(
51 //         FROM_HERE,
52 //         base::BindOnce(&Foo::ConnectToService, this,
53 //         base::Passed(&receiver)));
54 //     resource_usage_.reset(new ProcessResourceUsage(std::move(service)));
55 //   ...
56 //
57 // Note: ProcessResourceUsage is thread-hostile and must live on a single
58 // thread.
59 class ProcessResourceUsage {
60  public:
61   // Must be called from the same thread that created |service|.
62   explicit ProcessResourceUsage(
63       mojo::PendingRemote<content::mojom::ResourceUsageReporter> service);
64
65   ProcessResourceUsage(const ProcessResourceUsage&) = delete;
66   ProcessResourceUsage& operator=(const ProcessResourceUsage&) = delete;
67
68   ~ProcessResourceUsage();
69
70   // Refresh the resource usage information. |callback| is invoked when the
71   // usage data is updated, or when the IPC connection is lost.
72   void Refresh(base::OnceClosure callback);
73
74   // Get V8 memory usage information.
75   bool ReportsV8MemoryStats() const;
76   size_t GetV8MemoryAllocated() const;
77   size_t GetV8MemoryUsed() const;
78
79   // Get Blink resource cache information.
80   blink::WebCacheResourceTypeStats GetBlinkMemoryCacheStats() const;
81
82  private:
83   // Mojo IPC callback.
84   void OnRefreshDone(content::mojom::ResourceUsageDataPtr data);
85
86   void RunPendingRefreshCallbacks();
87
88   mojo::Remote<content::mojom::ResourceUsageReporter> service_;
89   bool update_in_progress_;
90   base::circular_deque<base::OnceClosure> refresh_callbacks_;
91
92   content::mojom::ResourceUsageDataPtr stats_;
93
94   base::ThreadChecker thread_checker_;
95 };
96
97 #endif  // CHROME_BROWSER_PROCESS_RESOURCE_USAGE_H_