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