1// Copyright (c) 2011 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 PPAPI_CPP_DIRECTORY_ENTRY_H_
6#define PPAPI_CPP_DIRECTORY_ENTRY_H_
7
8#include <vector>
9
10#include "ppapi/c/pp_array_output.h"
11#include "ppapi/c/pp_directory_entry.h"
12#include "ppapi/cpp/array_output.h"
13#include "ppapi/cpp/file_ref.h"
14#include "ppapi/cpp/output_traits.h"
15#include "ppapi/cpp/pass_ref.h"
16
17/// @file
18/// This file defines the API used to handle a directory entry.
19
20namespace pp {
21
22/// The <code>DirectoryEntry</code> class represents information about
23/// a directory entry.
24class DirectoryEntry {
25 public:
26  /// Default constructor for creating an is_null() <code>DirectoryEntry</code>
27  /// object.
28  DirectoryEntry();
29
30  /// A constructor used when you have a <code>PP_DirectoryEntry</code> which
31  /// contains a <code>FileRef</code> that has already been reference counted
32  /// as a return value.
33  ///
34  /// @param[in] data A <code>PP_DirectoryEntry</code> to be copied.
35  DirectoryEntry(PassRef, const PP_DirectoryEntry& data);
36
37  /// A copy constructor for <code>DirectoryEntry</code>. This constructor
38  /// increments a reference count of the <code>FileRef</code> held by this
39  /// DirectoryEntry.
40  ///
41  /// @param[in] other A pointer to a <code>DirectoryEntry</code>.
42  DirectoryEntry(const DirectoryEntry& other);
43
44  /// A destructor that decrements a reference count of the <code>FileRef</code>
45  /// held by this <code>DirectoryEntry</code>.
46  ~DirectoryEntry();
47
48  /// This function assigns one <code>DirectoryEntry</code> object to this
49  /// <code>DirectoryEntry</code> object. This function increases the reference
50  /// count of the <code>FileRef</code> of the other DirectoryEntry while
51  /// decrementing the reference count of the FileRef of this DirectoryEntry.
52  ///
53  /// @param[in] other A pointer to a <code>DirectoryEntry</code>.
54  ///
55  /// @return A new <code>DirectoryEntry</code> object.
56  DirectoryEntry& operator=(const DirectoryEntry& other);
57
58  /// This function determines if this <code>DirectoryEntry</code> is a null
59  /// value.
60  ///
61  /// @return true if this <code>DirectoryEntry</code> is null, otherwise false.
62  bool is_null() const { return !data_.file_ref; }
63
64  /// This function returns the <code>FileRef</code> held by this
65  /// <code>DirectoryEntry</code>.
66  ///
67  /// @return A <code>FileRef</code> of the file.
68  FileRef file_ref() const { return FileRef(data_.file_ref); }
69
70  /// This function returns the <code>PP_FileType</code> of the file referenced
71  /// by this <code>DirectoryEntry</code>.
72  ///
73  /// @return A <code>PP_FileType</code> of the file.
74  PP_FileType file_type() const { return data_.file_type; }
75
76 private:
77  PP_DirectoryEntry data_;
78};
79
80namespace internal {
81
82class DirectoryEntryArrayOutputAdapterWithStorage
83    : public ArrayOutputAdapter<PP_DirectoryEntry> {
84 public:
85  DirectoryEntryArrayOutputAdapterWithStorage();
86  virtual ~DirectoryEntryArrayOutputAdapterWithStorage();
87
88  // Returns the final array of resource objects, converting the
89  // PP_DirectoryEntry written by the browser to pp::DirectoryEntry
90  // objects.
91  //
92  // This function should only be called once or we would end up converting
93  // the array more than once, which would mess up the refcounting.
94  std::vector<DirectoryEntry>& output();
95
96 private:
97  // The browser will write the PP_DirectoryEntrys into this array.
98  std::vector<PP_DirectoryEntry> temp_storage_;
99
100  // When asked for the output, the PP_DirectoryEntrys above will be
101  // converted to the pp::DirectoryEntrys in this array for passing to the
102  // calling code.
103  std::vector<DirectoryEntry> output_storage_;
104};
105
106// A specialization of CallbackOutputTraits to provide the callback system the
107// information on how to handle vectors of pp::DirectoryEntry. This converts
108// PP_DirectoryEntry to pp::DirectoryEntry when passing to the plugin.
109template <>
110struct CallbackOutputTraits< std::vector<DirectoryEntry> > {
111  typedef PP_ArrayOutput APIArgType;
112  typedef DirectoryEntryArrayOutputAdapterWithStorage StorageType;
113
114  static inline APIArgType StorageToAPIArg(StorageType& t) {
115    return t.pp_array_output();
116  }
117
118  static inline std::vector<DirectoryEntry>& StorageToPluginArg(
119      StorageType& t) {
120    return t.output();
121  }
122
123  static inline void Initialize(StorageType* /* t */) {}
124};
125
126}  // namespace internal
127}  // namespace pp
128
129#endif  // PPAPI_CPP_DIRECTORY_ENTRY_H_
130