15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_TEMPORARY_FILE_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_TEMPORARY_FILE_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "native_client/src/include/nacl_macros.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "native_client/src/trusted/desc/nacl_desc_wrapper.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "ppapi/c/private/pp_file_handle.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace plugin {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Plugin;
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Translation creates two temporary files.  The first temporary file holds
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the object file created by llc.  The second holds the nexe produced by
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the linker.  Both of these temporary files are used to both write and
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// read according to the following matrix:
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PnaclCoordinator::obj_file_:
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     written by: llc     (passed in explicitly through SRPC)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     read by:    ld      (returned via lookup service from SRPC)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// PnaclCoordinator::nexe_file_:
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     written by: ld      (passed in explicitly through SRPC)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     read by:    sel_ldr (passed in explicitly to command channel)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TempFile represents a file used as a temporary between stages in
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// translation.  It is automatically deleted when all handles are closed
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// (or earlier -- immediately unlinked on POSIX systems).  The file is only
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// opened, once, but because both reading and writing are necessary (and in
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// different processes), the user should reset / seek back to the beginning
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of the file between sessions.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TempFile {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Create a TempFile.
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  TempFile(Plugin* plugin, PP_FileHandle handle);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~TempFile();
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Opens a temporary file object and descriptor wrapper referring to the file.
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // If |writeable| is true, the descriptor will be opened for writing, and
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // write_wrapper will return a valid pointer, otherwise it will return NULL.
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  int32_t Open(bool writeable);
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets file position of the handle, for reuse.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Reset();
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Accessors.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The nacl::DescWrapper* for the writeable version of the file.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  nacl::DescWrapper* write_wrapper() { return write_wrapper_.get(); }
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  nacl::DescWrapper* read_wrapper() { return read_wrapper_.get(); }
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Returns the handle to the file repesented and resets the internal handle
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // and all wrappers.
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PP_FileHandle TakeFileHandle();
5758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NACL_DISALLOW_COPY_AND_ASSIGN(TempFile);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Plugin* plugin_;
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  nacl::scoped_ptr<nacl::DescWrapper> read_wrapper_;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  nacl::scoped_ptr<nacl::DescWrapper> write_wrapper_;
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  PP_FileHandle internal_handle_;
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace plugin
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_TEMPORARY_FILE_H_
70