xref: /aosp_15_r20/external/cronet/components/nacl/renderer/manifest_service_channel.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2014 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 COMPONENTS_NACL_RENDERER_MANIFEST_SERVICE_CHANNEL_H_
6 #define COMPONENTS_NACL_RENDERER_MANIFEST_SERVICE_CHANNEL_H_
7 
8 #include <stdint.h>
9 
10 #include <memory>
11 
12 #include "base/files/file.h"
13 #include "base/functional/callback.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/process/process.h"
16 #include "base/synchronization/lock.h"
17 #include "ipc/ipc_listener.h"
18 
19 namespace base {
20 class WaitableEvent;
21 }  // namespace base
22 
23 namespace IPC {
24 struct ChannelHandle;
25 class Message;
26 class SyncChannel;
27 }  // namespace IPC
28 
29 namespace nacl {
30 
31 class ManifestServiceChannel : public IPC::Listener {
32  public:
33   typedef base::OnceCallback<void(base::File, uint64_t, uint64_t)>
34       OpenResourceCallback;
35 
36   class Delegate {
37    public:
~Delegate()38     virtual ~Delegate() {}
39 
40     // Called when PPAPI initialization in the NaCl plugin is finished.
41     virtual void StartupInitializationComplete() = 0;
42 
43     // Called when irt_open_resource() is invoked in the NaCl plugin.
44     // Upon completion, callback is invoked with the file.
45     virtual void OpenResource(const std::string& key,
46                               OpenResourceCallback callback) = 0;
47   };
48 
49   ManifestServiceChannel(const IPC::ChannelHandle& handle,
50                          base::OnceCallback<void(int32_t)> connected_callback,
51                          std::unique_ptr<Delegate> delegate,
52                          base::WaitableEvent* waitable_event);
53 
54   ManifestServiceChannel(const ManifestServiceChannel&) = delete;
55   ManifestServiceChannel& operator=(const ManifestServiceChannel&) = delete;
56 
57   ~ManifestServiceChannel() override;
58 
59   void Send(IPC::Message* message);
60 
61   // Listener implementation.
62   bool OnMessageReceived(const IPC::Message& message) override;
63   void OnChannelConnected(int32_t peer_pid) override;
64   void OnChannelError() override;
65 
66  private:
67   void OnStartupInitializationComplete();
68   void OnOpenResource(const std::string& key, IPC::Message* reply);
69   void DidOpenResource(IPC::Message* reply,
70                        base::File file,
71                        uint64_t token_lo,
72                        uint64_t token_hi);
73   base::OnceCallback<void(int32_t)> connected_callback_;
74   std::unique_ptr<Delegate> delegate_;
75   std::unique_ptr<IPC::SyncChannel> channel_;
76 
77   base::ProcessId peer_pid_;
78 
79   // Note: This should remain the last member so it'll be destroyed and
80   // invalidate the weak pointers before any other members are destroyed.
81   base::WeakPtrFactory<ManifestServiceChannel> weak_ptr_factory_{this};
82 };
83 
84 }  // namespace nacl
85 
86 #endif  // COMPONENTS_NACL_RENDERER_MANIFEST_SERVICE_CHANNEL_H_
87