Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: ppapi/proxy/proxy_array_output.h

Issue 9728001: Make the file chooser use PP_ArrayOutput (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 PPAPI_PROXY_PROXY_ARRAY_OUTPUT_H_
6 #define PPAPI_PROXY_PROXY_ARRAY_OUTPUT_H_
7
8 #include <vector>
9
10 #include "base/memory/ref_counted.h"
11 #include "ppapi/c/pp_array_output.h"
12
13 // Like ppapi/cpp/array_output.h file in the C++ wrappers but for use in the
14 // proxy where we can't link to the C++ wrappers. This also adds a refcounted
15 // version.
16 //
17 // Use ArrayOutputAdapter when calling a function that synchronously returns
18 // an array of data. Use RefCountedArrayOutputAdapterWithStorage for
19 // asynchronous returns:
20 //
21 // void OnCallbackComplete(
22 // int32_t result,
23 // scoped_refptr<RefCountedArrayOutputAdapter<PP_Resource> > output) {
24 // // Vector is in output->output().
25 // }
26 //
27 // void ScheduleCallback() {
28 // base::scoped_refptr<RefCountedArrayOutputAdapter<PP_Resource> > output;
29 //
30 // callback = factory.NewOptionalCallback(&OnCallbackComplete, output);
31 // DoSomethingAsynchronously(output->pp_array_output(),
32 // callback.PP_CompletionCallback());
33 // ...
34 namespace ppapi {
35 namespace proxy {
36
37 // Non-templatized base class for the array output conversion. It provides the
38 // C implementation of a PP_ArrayOutput whose callback function is implemented
39 // as a virtual call on a derived class. Do not use directly, use one of the
40 // derived classes below.
41 class ArrayOutputAdapterBase {
42 public:
43 ArrayOutputAdapterBase() {
44 pp_array_output_.GetDataBuffer =
45 &ArrayOutputAdapterBase::GetDataBufferThunk;
46 pp_array_output_.user_data = this;
47 }
48 virtual ~ArrayOutputAdapterBase() {}
49
50 const PP_ArrayOutput& pp_array_output() { return pp_array_output_; }
51
52 protected:
53 virtual void* GetDataBuffer(uint32_t element_count,
54 uint32_t element_size) = 0;
55
56 private:
57 static void* GetDataBufferThunk(void* user_data,
58 uint32_t element_count,
59 uint32_t element_size);
60
61 PP_ArrayOutput pp_array_output_;
62
63 // Disallow copying and assignment. This will do the wrong thing for most
64 // subclasses.
65 ArrayOutputAdapterBase(const ArrayOutputAdapterBase&);
66 ArrayOutputAdapterBase& operator=(const ArrayOutputAdapterBase&);
67 };
68
69 // This adapter provides functionality for implementing a PP_ArrayOutput
70 // structure as writing to a given vector object.
71 //
72 // This is generally used internally in the C++ wrapper objects to
73 // write into an output parameter supplied by the plugin. If the element size
74 // that the browser is writing does not match the size of the type we're using
75 // this will assert and return NULL (which will cause the browser to fail the
76 // call).
77 //
78 // Example that allows the browser to write into a given vector:
79 // void DoFoo(std::vector<int>* results) {
80 // ArrayOutputAdapter<int> adapter(results);
81 // ppb_foo->DoFoo(adapter.pp_array_output());
82 // }
83 template<typename T>
84 class ArrayOutputAdapter : public ArrayOutputAdapterBase {
85 public:
86 ArrayOutputAdapter(std::vector<T>* output) : output_(output) {}
87
88 protected:
89 // Two-step init for the "with storage" version below.
90 ArrayOutputAdapter() : output_(NULL) {}
91 void set_output(std::vector<T>* output) { output_ = output; }
92
93 // ArrayOutputAdapterBase implementation.
94 virtual void* GetDataBuffer(uint32_t element_count, uint32_t element_size) {
95 DCHECK(element_size == sizeof(T));
96 if (element_count == 0 || element_size != sizeof(T))
97 return NULL;
98 output_->resize(element_count);
99 return &(*output_)[0];
100 }
101
102 private:
103 std::vector<T>* output_;
104 };
105
106 template<typename T>
107 class ArrayOutputAdapterWithStorage : public ArrayOutputAdapter<T> {
108 public:
109 ArrayOutputAdapterWithStorage() {
110 set_output(&output_storage_);
111 }
112
113 std::vector<T>& output() { return output_storage_; }
114
115 private:
116 std::vector<T> output_storage_;
117 };
118
119 // A reference counted version of ArrayOutputAdapterWithStorage. Since it
120 // doesn't make much sense to heap-allocate one without storage, we don't
121 // call it "with storage" to keep the name length under control.
122 template<typename T>
123 class RefCountedArrayOutputAdapter
124 : public ArrayOutputAdapterWithStorage<T>,
125 public base::RefCounted<RefCountedArrayOutputAdapter<T> > {
126 public:
127 RefCountedArrayOutputAdapter()
128 : ArrayOutputAdapterWithStorage<T>() {
129 }
130 };
131
132 } // namespace proxy
133 } // namespace ppapi
134
135 #endif // PPAPI_PROXY_PROXY_ARRAY_OUTPUT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698