1950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com// Copyright 2013 The Chromium Authors. All rights reserved.
2950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com// Use of this source code is governed by a BSD-style license that can be
3950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com// found in the LICENSE file.
4950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
5950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#ifndef CHROME_RENDERER_PEPPER_PNACL_TRANSLATION_RESOURCE_HOST_H_
6950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#define CHROME_RENDERER_PEPPER_PNACL_TRANSLATION_RESOURCE_HOST_H_
7950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
8950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#include <map>
9950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
10950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#include "base/callback.h"
11950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#include "base/message_loop/message_loop_proxy.h"
12950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#include "ipc/ipc_platform_file.h"
13950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#include "ipc/message_filter.h"
14950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#include "ppapi/c/pp_bool.h"
15950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#include "ppapi/c/pp_instance.h"
16950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#include "ppapi/c/private/pp_file_handle.h"
17950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
18950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.comnamespace nacl {
19950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.comstruct PnaclCacheInfo;
20950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com}
21950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
22950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com// A class to keep track of requests made to the browser for resources that the
23950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com// PNaCl translator needs (e.g. descriptors for the translator nexes, temp
24950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com// files, and cached translations).
25950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
26950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com// "Resource" might not be the best name for the various things that pnacl
27950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com// needs from the browser since "Resource" is a Pepper thing...
28950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.comclass PnaclTranslationResourceHost : public IPC::MessageFilter {
29950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com public:
30950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  typedef base::Callback<void(int32_t, bool, PP_FileHandle)>
31950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com          RequestNexeFdCallback;
32950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
33950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  explicit PnaclTranslationResourceHost(
34950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com      const scoped_refptr<base::MessageLoopProxy>& io_message_loop);
35950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  void RequestNexeFd(int render_view_id,
36950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com                     PP_Instance instance,
37950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com                     const nacl::PnaclCacheInfo& cache_info,
38950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com                     RequestNexeFdCallback callback);
39950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  void ReportTranslationFinished(PP_Instance instance, PP_Bool success);
40950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
41950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com protected:
42950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  virtual ~PnaclTranslationResourceHost();
43950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
44950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com private:
45950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  // Maps the instance with an outstanding cache request to the info
46950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  // about that request.
47950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  typedef std::map<PP_Instance, RequestNexeFdCallback> CacheRequestInfoMap;
48950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
49950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  // IPC::MessageFilter implementation.
50950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
51950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  virtual void OnFilterAdded(IPC::Sender* sender) OVERRIDE;
52950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  virtual void OnFilterRemoved() OVERRIDE;
53950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  virtual void OnChannelClosing() OVERRIDE;
54950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
55950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  void SendRequestNexeFd(int render_view_id,
56950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com                         PP_Instance instance,
57950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com                         const nacl::PnaclCacheInfo& cache_info,
58950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com                         RequestNexeFdCallback callback);
59950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  void SendReportTranslationFinished(PP_Instance instance,
60950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com                                     PP_Bool success);
61950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  void OnNexeTempFileReply(PP_Instance instance,
62950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com                           bool is_hit,
63950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com                           IPC::PlatformFileForTransit file);
64950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  void CleanupCacheRequests();
65950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
66950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  scoped_refptr<base::MessageLoopProxy> io_message_loop_;
67950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
68950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  // Should be accessed on the io thread.
69950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  IPC::Sender* sender_;
70950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  CacheRequestInfoMap pending_cache_requests_;
71950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com  DISALLOW_COPY_AND_ASSIGN(PnaclTranslationResourceHost);
72950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com};
73950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com
74950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com#endif  // CHROME_RENDERER_PEPPER_PNACL_TRANSLATION_RESOURCE_HOST_H_
75950306ccf1af3f07944636df55daf3da878b7fc4fmalita@google.com