| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/cpp/private/flash_clipboard.h" | 5 #include "ppapi/cpp/private/flash_clipboard.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "ppapi/c/pp_bool.h" | 9 #include "ppapi/c/pp_bool.h" |
| 10 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
| 11 #include "ppapi/cpp/instance_handle.h" | 11 #include "ppapi/cpp/instance_handle.h" |
| 12 #include "ppapi/cpp/module_impl.h" | 12 #include "ppapi/cpp/module_impl.h" |
| 13 #include "ppapi/cpp/var.h" | 13 #include "ppapi/cpp/var.h" |
| 14 | 14 |
| 15 namespace pp { | 15 namespace pp { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 template <> const char* interface_name<PPB_Flash_Clipboard_4_0>() { | 19 template <> const char* interface_name<PPB_Flash_Clipboard_4_0>() { |
| 20 return PPB_FLASH_CLIPBOARD_INTERFACE_4_0; | 20 return PPB_FLASH_CLIPBOARD_INTERFACE_4_0; |
| 21 } | 21 } |
| 22 | 22 |
| 23 template <> const char* interface_name<PPB_Flash_Clipboard_5_0>() { |
| 24 return PPB_FLASH_CLIPBOARD_INTERFACE_5_0; |
| 25 } |
| 26 |
| 23 } // namespace | 27 } // namespace |
| 24 | 28 |
| 25 namespace flash { | 29 namespace flash { |
| 26 | 30 |
| 27 // static | 31 // static |
| 28 bool Clipboard::IsAvailable() { | 32 bool Clipboard::IsAvailable() { |
| 29 return has_interface<PPB_Flash_Clipboard_4_0>(); | 33 return has_interface<PPB_Flash_Clipboard_5_0>() || |
| 34 has_interface<PPB_Flash_Clipboard_4_0>() ; |
| 35 } |
| 36 |
| 37 // static |
| 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()); |
| 44 } |
| 45 return rv; |
| 30 } | 46 } |
| 31 | 47 |
| 32 // static | 48 // static |
| 33 bool Clipboard::IsFormatAvailable(const InstanceHandle& instance, | 49 bool Clipboard::IsFormatAvailable(const InstanceHandle& instance, |
| 34 PP_Flash_Clipboard_Type clipboard_type, | 50 PP_Flash_Clipboard_Type clipboard_type, |
| 35 PP_Flash_Clipboard_Format format) { | 51 uint32_t format) { |
| 36 bool rv = false; | 52 bool rv = false; |
| 37 if (has_interface<PPB_Flash_Clipboard_4_0>()) { | 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>()) { |
| 38 rv = PP_ToBool(get_interface<PPB_Flash_Clipboard_4_0>()->IsFormatAvailable( | 57 rv = PP_ToBool(get_interface<PPB_Flash_Clipboard_4_0>()->IsFormatAvailable( |
| 39 instance.pp_instance(), clipboard_type, format)); | 58 instance.pp_instance(), clipboard_type, |
| 59 static_cast<PP_Flash_Clipboard_Format>(format))); |
| 40 } | 60 } |
| 41 return rv; | 61 return rv; |
| 42 } | 62 } |
| 43 | 63 |
| 44 // static | 64 // static |
| 45 bool Clipboard::ReadData( | 65 bool Clipboard::ReadData( |
| 46 const InstanceHandle& instance, | 66 const InstanceHandle& instance, |
| 47 PP_Flash_Clipboard_Type clipboard_type, | 67 PP_Flash_Clipboard_Type clipboard_type, |
| 48 PP_Flash_Clipboard_Format clipboard_format, | 68 uint32_t format, |
| 49 Var* out) { | 69 Var* out) { |
| 50 bool rv = false; | 70 bool rv = false; |
| 51 if (has_interface<PPB_Flash_Clipboard_4_0>()) { | 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(), |
| 74 clipboard_type, |
| 75 format); |
| 76 *out = Var(PASS_REF, result); |
| 77 rv = true; |
| 78 } else if (has_interface<PPB_Flash_Clipboard_4_0>()) { |
| 52 PP_Var result = get_interface<PPB_Flash_Clipboard_4_0>()->ReadData( | 79 PP_Var result = get_interface<PPB_Flash_Clipboard_4_0>()->ReadData( |
| 53 instance.pp_instance(), | 80 instance.pp_instance(), |
| 54 clipboard_type, | 81 clipboard_type, |
| 55 clipboard_format); | 82 static_cast<PP_Flash_Clipboard_Format>(format)); |
| 56 *out = Var(PASS_REF, result); | 83 *out = Var(PASS_REF, result); |
| 57 rv = true; | 84 rv = true; |
| 58 } | 85 } |
| 59 return rv; | 86 return rv; |
| 60 } | 87 } |
| 61 | 88 |
| 62 // static | 89 // static |
| 63 bool Clipboard::WriteData( | 90 bool Clipboard::WriteData( |
| 64 const InstanceHandle& instance, | 91 const InstanceHandle& instance, |
| 65 PP_Flash_Clipboard_Type clipboard_type, | 92 PP_Flash_Clipboard_Type clipboard_type, |
| 66 const std::vector<PP_Flash_Clipboard_Format>& formats, | 93 const std::vector<uint32_t>& formats, |
| 67 const std::vector<Var>& data_items) { | 94 const std::vector<Var>& data_items) { |
| 68 if (formats.size() != data_items.size()) | 95 if (formats.size() != data_items.size()) |
| 69 return false; | 96 return false; |
| 70 | 97 |
| 71 bool rv = false; | 98 bool rv = false; |
| 72 if (has_interface<PPB_Flash_Clipboard_4_0>()) { | 99 if (has_interface<PPB_Flash_Clipboard_5_0>()) { |
| 73 // Convert vector of pp::Var into a vector of PP_Var. | 100 // Convert vector of pp::Var into a vector of PP_Var. |
| 74 std::vector<PP_Var> data_items_vector; | 101 std::vector<PP_Var> data_items_vector; |
| 75 for (uint32_t i = 0; i < data_items.size(); ++i) | 102 for (uint32_t i = 0; i < data_items.size(); ++i) |
| 76 data_items_vector.push_back(data_items[i].pp_var()); | 103 data_items_vector.push_back(data_items[i].pp_var()); |
| 77 | 104 |
| 78 // Ensure that we don't dereference the memory in empty vectors. We still | 105 // Ensure that we don't dereference the memory in empty vectors. We still |
| 79 // want to call WriteData because it has the effect of clearing the | 106 // want to call WriteData because it has the effect of clearing the |
| 80 // clipboard. | 107 // clipboard. |
| 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]; |
| 113 } |
| 114 |
| 115 rv = (get_interface<PPB_Flash_Clipboard_5_0>()->WriteData( |
| 116 instance.pp_instance(), |
| 117 clipboard_type, |
| 118 data_items.size(), |
| 119 formats_ptr, |
| 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])); |
| 128 } |
| 129 |
| 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 |
| 132 // clipboard. |
| 81 const PP_Flash_Clipboard_Format* formats_ptr(NULL); | 133 const PP_Flash_Clipboard_Format* formats_ptr(NULL); |
| 82 const PP_Var* data_items_ptr(NULL); | 134 const PP_Var* data_items_ptr(NULL); |
| 83 if (data_items.size() > 0) { | 135 if (data_items.size() > 0) { |
| 84 formats_ptr = &formats[0]; | 136 formats_ptr = &old_formats[0]; |
| 85 data_items_ptr = &data_items_vector[0]; | 137 data_items_ptr = &data_items_vector[0]; |
| 86 } | 138 } |
| 87 | 139 |
| 88 rv = (get_interface<PPB_Flash_Clipboard_4_0>()->WriteData( | 140 rv = (get_interface<PPB_Flash_Clipboard_4_0>()->WriteData( |
| 89 instance.pp_instance(), | 141 instance.pp_instance(), |
| 90 clipboard_type, | 142 clipboard_type, |
| 91 data_items.size(), | 143 data_items.size(), |
| 92 formats_ptr, | 144 formats_ptr, |
| 93 data_items_ptr) == PP_OK); | 145 data_items_ptr) == PP_OK); |
| 94 } | 146 } |
| 95 | 147 |
| 96 return rv; | 148 return rv; |
| 97 } | 149 } |
| 98 | 150 |
| 99 } // namespace flash | 151 } // namespace flash |
| 100 } // namespace pp | 152 } // namespace pp |
| OLD | NEW |