1// Copyright 2013 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 CHROME_RENDERER_PEPPER_PNACL_TRANSLATION_RESOURCE_HOST_H_
6#define CHROME_RENDERER_PEPPER_PNACL_TRANSLATION_RESOURCE_HOST_H_
7
8#include <map>
9
10#include "ipc/ipc_channel_proxy.h"
11#include "ipc/ipc_platform_file.h"
12#include "ppapi/c/private/pp_file_handle.h"
13#include "ppapi/shared_impl/tracked_callback.h"
14
15namespace nacl {
16struct PnaclCacheInfo;
17}
18
19// A class to keep track of requests made to the browser for resources that the
20// PNaCl translator needs (e.g. descriptors for the translator nexes, temp
21// files, and cached translations).
22
23// "Resource" might not be the best name for the various things that pnacl
24// needs from the browser since "Resource" is a Pepper thing...
25class PnaclTranslationResourceHost : public IPC::ChannelProxy::MessageFilter {
26 public:
27  explicit PnaclTranslationResourceHost(
28      const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
29  void RequestNexeFd(int render_view_id,
30                     PP_Instance instance,
31                     const nacl::PnaclCacheInfo& cache_info,
32                     PP_Bool* is_hit,
33                     PP_FileHandle* file_handle,
34                     scoped_refptr<ppapi::TrackedCallback> callback);
35  void ReportTranslationFinished(PP_Instance instance, PP_Bool success);
36
37 protected:
38  virtual ~PnaclTranslationResourceHost();
39
40 private:
41  class CacheRequestInfo {
42   public:
43    CacheRequestInfo(PP_Bool* hit,
44                     PP_FileHandle* handle,
45                     scoped_refptr<ppapi::TrackedCallback> cb);
46
47    ~CacheRequestInfo();
48
49    PP_Bool* is_hit;
50    PP_FileHandle* file_handle;
51    scoped_refptr<ppapi::TrackedCallback> callback;
52  };
53
54  // Maps the instance with an outstanding cache request to the info
55  // about that request.
56  typedef std::map<PP_Instance, CacheRequestInfo> CacheRequestInfoMap;
57  // IPC::ChannelProxy::MessageFilter implementation.
58  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
59  virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE;
60  virtual void OnFilterRemoved() OVERRIDE;
61  virtual void OnChannelClosing() OVERRIDE;
62
63  void SendRequestNexeFd(int render_view_id,
64                         PP_Instance instance,
65                         const nacl::PnaclCacheInfo& cache_info,
66                         PP_Bool* is_hit,
67                         PP_FileHandle* file_handle,
68                         scoped_refptr<ppapi::TrackedCallback> callback);
69  void SendReportTranslationFinished(PP_Instance instance,
70                                     PP_Bool success);
71  void OnNexeTempFileReply(PP_Instance instance,
72                           bool is_hit,
73                           IPC::PlatformFileForTransit file);
74  void CleanupCacheRequests();
75
76  scoped_refptr<base::MessageLoopProxy> io_message_loop_;
77
78  // Should be accessed on the io thread.
79  IPC::Channel* channel_;
80  CacheRequestInfoMap pending_cache_requests_;
81  DISALLOW_COPY_AND_ASSIGN(PnaclTranslationResourceHost);
82};
83
84#endif  // CHROME_RENDERER_PEPPER_PNACL_TRANSLATION_RESOURCE_HOST_H_
85