1// Copyright (c) 2012 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 NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_TEMPORARY_FILE_H_
6#define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_TEMPORARY_FILE_H_
7
8#include "native_client/src/include/nacl_macros.h"
9#include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
10
11#include "ppapi/c/private/pp_file_handle.h"
12
13namespace plugin {
14
15class Plugin;
16
17// Translation creates two temporary files.  The first temporary file holds
18// the object file created by llc.  The second holds the nexe produced by
19// the linker.  Both of these temporary files are used to both write and
20// read according to the following matrix:
21//
22// PnaclCoordinator::obj_file_:
23//     written by: llc     (passed in explicitly through SRPC)
24//     read by:    ld      (returned via lookup service from SRPC)
25// PnaclCoordinator::nexe_file_:
26//     written by: ld      (passed in explicitly through SRPC)
27//     read by:    sel_ldr (passed in explicitly to command channel)
28//
29
30// TempFile represents a file used as a temporary between stages in
31// translation.  It is automatically deleted when all handles are closed
32// (or earlier -- immediately unlinked on POSIX systems).  The file is only
33// opened, once, but because both reading and writing are necessary (and in
34// different processes), the user should reset / seek back to the beginning
35// of the file between sessions.
36class TempFile {
37 public:
38  // Create a TempFile.
39  TempFile(Plugin* plugin, PP_FileHandle handle);
40  ~TempFile();
41
42  // Opens a temporary file object and descriptor wrapper referring to the file.
43  // If |writeable| is true, the descriptor will be opened for writing, and
44  // write_wrapper will return a valid pointer, otherwise it will return NULL.
45  int32_t Open(bool writeable);
46  // Resets file position of the handle, for reuse.
47  bool Reset();
48
49  // Accessors.
50  // The nacl::DescWrapper* for the writeable version of the file.
51  nacl::DescWrapper* write_wrapper() { return write_wrapper_.get(); }
52  nacl::DescWrapper* read_wrapper() { return read_wrapper_.get(); }
53
54  // Returns the handle to the file repesented and resets the internal handle
55  // and all wrappers.
56  PP_FileHandle TakeFileHandle();
57
58 private:
59  NACL_DISALLOW_COPY_AND_ASSIGN(TempFile);
60
61  Plugin* plugin_;
62  nacl::scoped_ptr<nacl::DescWrapper> read_wrapper_;
63  nacl::scoped_ptr<nacl::DescWrapper> write_wrapper_;
64  PP_FileHandle internal_handle_;
65};
66
67}  // namespace plugin
68
69#endif  // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_TEMPORARY_FILE_H_
70