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.
5 #include "ppapi/cpp/private/flash_clipboard.h"
9 #include "ppapi/c/pp_bool.h"
10 #include "ppapi/c/pp_errors.h"
11 #include "ppapi/cpp/instance_handle.h"
12 #include "ppapi/cpp/module_impl.h"
13 #include "ppapi/cpp/var.h"
19 template <> const char* interface_name
<PPB_Flash_Clipboard_4_0
>() {
20 return PPB_FLASH_CLIPBOARD_INTERFACE_4_0
;
23 template <> const char* interface_name
<PPB_Flash_Clipboard_5_0
>() {
24 return PPB_FLASH_CLIPBOARD_INTERFACE_5_0
;
27 template <> const char* interface_name
<PPB_Flash_Clipboard_5_1
>() {
28 return PPB_FLASH_CLIPBOARD_INTERFACE_5_1
;
36 bool Clipboard::IsAvailable() {
37 return has_interface
<PPB_Flash_Clipboard_5_1
>() ||
38 has_interface
<PPB_Flash_Clipboard_5_0
>() ||
39 has_interface
<PPB_Flash_Clipboard_4_0
>() ;
43 uint32_t Clipboard::RegisterCustomFormat(const InstanceHandle
& instance
,
44 const std::string
& format_name
) {
45 uint32_t rv
= PP_FLASH_CLIPBOARD_FORMAT_INVALID
;
46 if (has_interface
<PPB_Flash_Clipboard_5_1
>()) {
47 rv
= get_interface
<PPB_Flash_Clipboard_5_1
>()->RegisterCustomFormat(
48 instance
.pp_instance(), format_name
.c_str());
49 } else if (has_interface
<PPB_Flash_Clipboard_5_0
>()) {
50 rv
= get_interface
<PPB_Flash_Clipboard_5_0
>()->RegisterCustomFormat(
51 instance
.pp_instance(), format_name
.c_str());
57 bool Clipboard::IsFormatAvailable(const InstanceHandle
& instance
,
58 PP_Flash_Clipboard_Type clipboard_type
,
61 if (has_interface
<PPB_Flash_Clipboard_5_1
>()) {
62 rv
= PP_ToBool(get_interface
<PPB_Flash_Clipboard_5_1
>()->IsFormatAvailable(
63 instance
.pp_instance(), clipboard_type
, format
));
64 } else if (has_interface
<PPB_Flash_Clipboard_5_0
>()) {
65 rv
= PP_ToBool(get_interface
<PPB_Flash_Clipboard_5_0
>()->IsFormatAvailable(
66 instance
.pp_instance(), clipboard_type
, format
));
67 } else if (has_interface
<PPB_Flash_Clipboard_4_0
>()) {
68 rv
= PP_ToBool(get_interface
<PPB_Flash_Clipboard_4_0
>()->IsFormatAvailable(
69 instance
.pp_instance(), clipboard_type
,
70 static_cast<PP_Flash_Clipboard_Format
>(format
)));
76 bool Clipboard::ReadData(
77 const InstanceHandle
& instance
,
78 PP_Flash_Clipboard_Type clipboard_type
,
82 if (has_interface
<PPB_Flash_Clipboard_5_1
>()) {
83 PP_Var result
= get_interface
<PPB_Flash_Clipboard_5_1
>()->ReadData(
84 instance
.pp_instance(),
87 *out
= Var(PASS_REF
, result
);
89 } else if (has_interface
<PPB_Flash_Clipboard_5_0
>()) {
90 PP_Var result
= get_interface
<PPB_Flash_Clipboard_5_0
>()->ReadData(
91 instance
.pp_instance(),
94 *out
= Var(PASS_REF
, result
);
96 } else if (has_interface
<PPB_Flash_Clipboard_4_0
>()) {
97 PP_Var result
= get_interface
<PPB_Flash_Clipboard_4_0
>()->ReadData(
98 instance
.pp_instance(),
100 static_cast<PP_Flash_Clipboard_Format
>(format
));
101 *out
= Var(PASS_REF
, result
);
108 bool Clipboard::WriteData(
109 const InstanceHandle
& instance
,
110 PP_Flash_Clipboard_Type clipboard_type
,
111 const std::vector
<uint32_t>& formats
,
112 const std::vector
<Var
>& data_items
) {
113 if (formats
.size() != data_items
.size())
117 if (has_interface
<PPB_Flash_Clipboard_5_1
>()) {
118 // Convert vector of pp::Var into a vector of PP_Var.
119 std::vector
<PP_Var
> data_items_vector
;
120 for (uint32_t i
= 0; i
< data_items
.size(); ++i
)
121 data_items_vector
.push_back(data_items
[i
].pp_var());
123 // Ensure that we don't dereference the memory in empty vectors. We still
124 // want to call WriteData because it has the effect of clearing the
126 const uint32_t* formats_ptr(NULL
);
127 const PP_Var
* data_items_ptr(NULL
);
128 if (data_items
.size() > 0) {
129 formats_ptr
= &formats
[0];
130 data_items_ptr
= &data_items_vector
[0];
133 rv
= (get_interface
<PPB_Flash_Clipboard_5_1
>()->WriteData(
134 instance
.pp_instance(),
136 static_cast<uint32_t>(data_items
.size()),
138 data_items_ptr
) == PP_OK
);
139 } else if (has_interface
<PPB_Flash_Clipboard_5_0
>()) {
140 // Convert vector of pp::Var into a vector of PP_Var.
141 std::vector
<PP_Var
> data_items_vector
;
142 for (uint32_t i
= 0; i
< data_items
.size(); ++i
)
143 data_items_vector
.push_back(data_items
[i
].pp_var());
145 // Ensure that we don't dereference the memory in empty vectors. We still
146 // want to call WriteData because it has the effect of clearing the
148 const uint32_t* formats_ptr(NULL
);
149 const PP_Var
* data_items_ptr(NULL
);
150 if (data_items
.size() > 0) {
151 formats_ptr
= &formats
[0];
152 data_items_ptr
= &data_items_vector
[0];
155 rv
= (get_interface
<PPB_Flash_Clipboard_5_0
>()->WriteData(
156 instance
.pp_instance(),
158 static_cast<uint32_t>(data_items
.size()),
160 data_items_ptr
) == PP_OK
);
161 } else if (has_interface
<PPB_Flash_Clipboard_4_0
>()) {
162 // Convert vector of pp::Var into a vector of PP_Var.
163 std::vector
<PP_Var
> data_items_vector
;
164 std::vector
<PP_Flash_Clipboard_Format
> old_formats
;
165 for (uint32_t i
= 0; i
< data_items
.size(); ++i
) {
166 data_items_vector
.push_back(data_items
[i
].pp_var());
167 old_formats
.push_back(static_cast<PP_Flash_Clipboard_Format
>(formats
[i
]));
170 // Ensure that we don't dereference the memory in empty vectors. We still
171 // want to call WriteData because it has the effect of clearing the
173 const PP_Flash_Clipboard_Format
* formats_ptr(NULL
);
174 const PP_Var
* data_items_ptr(NULL
);
175 if (data_items
.size() > 0) {
176 formats_ptr
= &old_formats
[0];
177 data_items_ptr
= &data_items_vector
[0];
180 rv
= (get_interface
<PPB_Flash_Clipboard_4_0
>()->WriteData(
181 instance
.pp_instance(),
183 static_cast<uint32_t>(data_items
.size()),
185 data_items_ptr
) == PP_OK
);
192 bool Clipboard::GetSequenceNumber(const InstanceHandle
& instance
,
193 PP_Flash_Clipboard_Type clipboard_type
,
194 uint64_t* sequence_number
) {
195 if (has_interface
<PPB_Flash_Clipboard_5_1
>()) {
197 get_interface
<PPB_Flash_Clipboard_5_1
>()->GetSequenceNumber(
198 instance
.pp_instance(), clipboard_type
, sequence_number
));