1 // Copyright 2017 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 
5 #ifndef COMPONENTS_SERVICES_HEAP_PROFILING_PUBLIC_CPP_PROFILING_CLIENT_H_
6 #define COMPONENTS_SERVICES_HEAP_PROFILING_PUBLIC_CPP_PROFILING_CLIENT_H_
7 
8 #include "components/services/heap_profiling/public/mojom/heap_profiling_client.mojom.h"
9 #include "mojo/public/cpp/bindings/pending_receiver.h"
10 #include "mojo/public/cpp/bindings/receiver_set.h"
11 
12 namespace heap_profiling {
13 
14 // The Client listens on the interface for a StartProfiling message. On
15 // receiving the message, it begins profiling the current process.
16 //
17 // The owner of this object is responsible for binding it to the BinderRegistry.
18 class ProfilingClient : public mojom::ProfilingClient {
19  public:
20   ProfilingClient();
21 
22   // mojom::ProfilingClient overrides:
23   void StartProfiling(mojom::ProfilingParamsPtr params,
24                       StartProfilingCallback callback) override;
25   void RetrieveHeapProfile(RetrieveHeapProfileCallback callback) override;
26 
27   void BindToInterface(mojo::PendingReceiver<mojom::ProfilingClient> receiver);
28 
29  private:
30   ~ProfilingClient() override;
31 
32   void StartProfilingInternal(mojom::ProfilingParamsPtr params,
33                               StartProfilingCallback callback);
34 
35   // Ideally, this would be a mojo::Receiver that would only keep alive one
36   // client receiver. However, the service that makes the client requests
37   // [content_browser] is different from the service that dedupes the client
38   // requests [profiling service]. This means that there may be a brief
39   // intervals where there are two active bindings, until the profiling service
40   // has a chance to figure out which one to keep.
41   mojo::ReceiverSet<mojom::ProfilingClient> receivers_;
42 
43   bool started_profiling_{false};
44 };
45 
46 // Initializes the TLS slot globally. This will be called early in Chrome's
47 // lifecycle to prevent re-entrancy from occurring while trying to set up the
48 // TLS slot, which is the entity that's supposed to prevent re-entrancy.
49 void InitTLSSlot();
50 
51 // Exists for testing only.
52 // A return value of |true| means that the allocator shim was already
53 // initialized and |callback| will never be called. Otherwise, |callback| will
54 // be called on |task_runner| after the allocator shim is initialized.
55 bool SetOnInitAllocatorShimCallbackForTesting(
56     base::OnceClosure callback,
57     scoped_refptr<base::TaskRunner> task_runner);
58 
59 }  // namespace heap_profiling
60 
61 #endif  // COMPONENTS_SERVICES_HEAP_PROFILING_PUBLIC_CPP_PROFILING_CLIENT_H_
62