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