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
;
32 bool Clipboard::IsAvailable() {
33 return has_interface
<PPB_Flash_Clipboard_5_0
>() ||
34 has_interface
<PPB_Flash_Clipboard_4_0
>() ;
38 uint32_t Clipboard::RegisterCustomFormat(const InstanceHandle
& instance
,
39 const std::string
& format_name
) {
40 uint32_t rv
= PP_FLASH_CLIPBOARD_FORMAT_INVALID
;
41 if (has_interface
<PPB_Flash_Clipboard_5_0
>()) {
42 rv
= get_interface
<PPB_Flash_Clipboard_5_0
>()->RegisterCustomFormat(
43 instance
.pp_instance(), format_name
.c_str());
49 bool Clipboard::IsFormatAvailable(const InstanceHandle
& instance
,
50 PP_Flash_Clipboard_Type clipboard_type
,
53 if (has_interface
<PPB_Flash_Clipboard_5_0
>()) {
54 rv
= PP_ToBool(get_interface
<PPB_Flash_Clipboard_5_0
>()->IsFormatAvailable(
55 instance
.pp_instance(), clipboard_type
, format
));
56 } else if (has_interface
<PPB_Flash_Clipboard_4_0
>()) {
57 rv
= PP_ToBool(get_interface
<PPB_Flash_Clipboard_4_0
>()->IsFormatAvailable(
58 instance
.pp_instance(), clipboard_type
,
59 static_cast<PP_Flash_Clipboard_Format
>(format
)));
65 bool Clipboard::ReadData(
66 const InstanceHandle
& instance
,
67 PP_Flash_Clipboard_Type clipboard_type
,
71 if (has_interface
<PPB_Flash_Clipboard_5_0
>()) {
72 PP_Var result
= get_interface
<PPB_Flash_Clipboard_5_0
>()->ReadData(
73 instance
.pp_instance(),
76 *out
= Var(PASS_REF
, result
);
78 } else if (has_interface
<PPB_Flash_Clipboard_4_0
>()) {
79 PP_Var result
= get_interface
<PPB_Flash_Clipboard_4_0
>()->ReadData(
80 instance
.pp_instance(),
82 static_cast<PP_Flash_Clipboard_Format
>(format
));
83 *out
= Var(PASS_REF
, result
);
90 bool Clipboard::WriteData(
91 const InstanceHandle
& instance
,
92 PP_Flash_Clipboard_Type clipboard_type
,
93 const std::vector
<uint32_t>& formats
,
94 const std::vector
<Var
>& data_items
) {
95 if (formats
.size() != data_items
.size())
99 if (has_interface
<PPB_Flash_Clipboard_5_0
>()) {
100 // Convert vector of pp::Var into a vector of PP_Var.
101 std::vector
<PP_Var
> data_items_vector
;
102 for (uint32_t i
= 0; i
< data_items
.size(); ++i
)
103 data_items_vector
.push_back(data_items
[i
].pp_var());
105 // Ensure that we don't dereference the memory in empty vectors. We still
106 // want to call WriteData because it has the effect of clearing the
108 const uint32_t* formats_ptr(NULL
);
109 const PP_Var
* data_items_ptr(NULL
);
110 if (data_items
.size() > 0) {
111 formats_ptr
= &formats
[0];
112 data_items_ptr
= &data_items_vector
[0];
115 rv
= (get_interface
<PPB_Flash_Clipboard_5_0
>()->WriteData(
116 instance
.pp_instance(),
120 data_items_ptr
) == PP_OK
);
121 } else if (has_interface
<PPB_Flash_Clipboard_4_0
>()) {
122 // Convert vector of pp::Var into a vector of PP_Var.
123 std::vector
<PP_Var
> data_items_vector
;
124 std::vector
<PP_Flash_Clipboard_Format
> old_formats
;
125 for (uint32_t i
= 0; i
< data_items
.size(); ++i
) {
126 data_items_vector
.push_back(data_items
[i
].pp_var());
127 old_formats
.push_back(static_cast<PP_Flash_Clipboard_Format
>(formats
[i
]));
130 // Ensure that we don't dereference the memory in empty vectors. We still
131 // want to call WriteData because it has the effect of clearing the
133 const PP_Flash_Clipboard_Format
* formats_ptr(NULL
);
134 const PP_Var
* data_items_ptr(NULL
);
135 if (data_items
.size() > 0) {
136 formats_ptr
= &old_formats
[0];
137 data_items_ptr
= &data_items_vector
[0];
140 rv
= (get_interface
<PPB_Flash_Clipboard_4_0
>()->WriteData(
141 instance
.pp_instance(),
145 data_items_ptr
) == PP_OK
);