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> |
| 8 |
7 #include "ppapi/c/pp_bool.h" | 9 #include "ppapi/c/pp_bool.h" |
8 #include "ppapi/c/pp_errors.h" | 10 #include "ppapi/c/pp_errors.h" |
9 #include "ppapi/cpp/instance.h" | 11 #include "ppapi/cpp/instance.h" |
10 #include "ppapi/cpp/module_impl.h" | 12 #include "ppapi/cpp/module_impl.h" |
11 #include "ppapi/cpp/var.h" | 13 #include "ppapi/cpp/var.h" |
12 | 14 |
13 namespace pp { | 15 namespace pp { |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 template <> const char* interface_name<PPB_Flash_Clipboard>() { | 19 template <> const char* interface_name<PPB_Flash_Clipboard>() { |
18 return PPB_FLASH_CLIPBOARD_INTERFACE; | 20 return PPB_FLASH_CLIPBOARD_INTERFACE; |
19 } | 21 } |
20 | 22 |
| 23 template <> const char* interface_name<PPB_Flash_Clipboard_3_0>() { |
| 24 return PPB_FLASH_CLIPBOARD_INTERFACE_3_0; |
| 25 } |
| 26 |
21 } // namespace | 27 } // namespace |
22 | 28 |
23 namespace flash { | 29 namespace flash { |
24 | 30 |
25 // static | 31 // static |
26 bool Clipboard::IsAvailable() { | 32 bool Clipboard::IsAvailable() { |
27 return has_interface<PPB_Flash_Clipboard>(); | 33 return has_interface<PPB_Flash_Clipboard>() || |
| 34 has_interface<PPB_Flash_Clipboard_3_0>(); |
28 } | 35 } |
29 | 36 |
30 // static | 37 // static |
31 bool Clipboard::IsFormatAvailable(Instance* instance, | 38 bool Clipboard::IsFormatAvailable(Instance* instance, |
32 PP_Flash_Clipboard_Type clipboard_type, | 39 PP_Flash_Clipboard_Type clipboard_type, |
33 PP_Flash_Clipboard_Format format) { | 40 PP_Flash_Clipboard_Format format) { |
34 bool rv = false; | 41 bool rv = false; |
35 if (has_interface<PPB_Flash_Clipboard>()) { | 42 if (has_interface<PPB_Flash_Clipboard>()) { |
36 rv = PP_ToBool(get_interface<PPB_Flash_Clipboard>()->IsFormatAvailable( | 43 rv = PP_ToBool(get_interface<PPB_Flash_Clipboard>()->IsFormatAvailable( |
37 instance->pp_instance(), clipboard_type, format)); | 44 instance->pp_instance(), clipboard_type, format)); |
38 } | 45 } |
39 return rv; | 46 return rv; |
40 } | 47 } |
41 | 48 |
42 // static | 49 // static |
43 bool Clipboard::ReadPlainText(Instance* instance, | 50 bool Clipboard::ReadData( |
44 PP_Flash_Clipboard_Type clipboard_type, | 51 Instance* instance, |
45 std::string* text_out) { | 52 PP_Flash_Clipboard_Type clipboard_type, |
| 53 PP_Flash_Clipboard_Format clipboard_format, |
| 54 Var* out) { |
46 bool rv = false; | 55 bool rv = false; |
47 if (has_interface<PPB_Flash_Clipboard>()) { | 56 if (has_interface<PPB_Flash_Clipboard>()) { |
48 Var v(Var::PassRef(), | 57 PP_Var result = get_interface<PPB_Flash_Clipboard>()->ReadData( |
49 get_interface<PPB_Flash_Clipboard>()->ReadPlainText( | 58 instance->pp_instance(), |
50 instance->pp_instance(), | 59 clipboard_type, |
51 clipboard_type)); | 60 clipboard_format); |
52 if (v.is_string()) { | 61 *out = Var(Var::PassRef(), result); |
53 rv = true; | 62 rv = true; |
54 *text_out = v.AsString(); | 63 } else if (has_interface<PPB_Flash_Clipboard_3_0>() && |
55 } | 64 clipboard_format == PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT) { |
| 65 PP_Var result = get_interface<PPB_Flash_Clipboard_3_0>()->ReadPlainText( |
| 66 instance->pp_instance(), |
| 67 clipboard_type); |
| 68 *out = Var(Var::PassRef(), result); |
| 69 rv = true; |
56 } | 70 } |
57 return rv; | 71 return rv; |
58 } | 72 } |
59 | 73 |
60 // static | 74 // static |
61 bool Clipboard::WritePlainText(Instance* instance, | 75 bool Clipboard::WriteData( |
62 PP_Flash_Clipboard_Type clipboard_type, | 76 Instance* instance, |
63 const std::string& text) { | 77 PP_Flash_Clipboard_Type clipboard_type, |
| 78 const std::vector<PP_Flash_Clipboard_Format>& formats, |
| 79 const std::vector<Var>& data_items) { |
| 80 if (formats.size() != data_items.size()) |
| 81 return false; |
| 82 |
64 bool rv = false; | 83 bool rv = false; |
65 if (has_interface<PPB_Flash_Clipboard>()) { | 84 if (has_interface<PPB_Flash_Clipboard>()) { |
66 rv = (get_interface<PPB_Flash_Clipboard>()->WritePlainText( | 85 // Convert vector of pp::Var into a vector of PP_Var. |
67 instance->pp_instance(), | 86 std::vector<PP_Var> data_items_vector; |
68 clipboard_type, | 87 for (uint32_t i = 0; i < data_items.size(); ++i) |
69 Var(text).pp_var()) == PP_OK); | 88 data_items_vector.push_back(data_items[i].pp_var()); |
| 89 |
| 90 // Ensure that we don't dereference the memory in empty vectors. We still |
| 91 // want to call WriteData because it has the effect of clearing the |
| 92 // clipboard. |
| 93 const PP_Flash_Clipboard_Format* formats_ptr(NULL); |
| 94 const PP_Var* data_items_ptr(NULL); |
| 95 if (data_items.size() > 0) { |
| 96 formats_ptr = &formats[0]; |
| 97 data_items_ptr = &data_items_vector[0]; |
| 98 } |
| 99 |
| 100 rv = (get_interface<PPB_Flash_Clipboard>()->WriteData( |
| 101 instance->pp_instance(), |
| 102 clipboard_type, |
| 103 data_items.size(), |
| 104 formats_ptr, |
| 105 data_items_ptr) == PP_OK); |
| 106 } else if (has_interface<PPB_Flash_Clipboard_3_0>()) { |
| 107 // The API specifies that only the last item of each format needs to be |
| 108 // written. Since we are only writing plain text items for the 3_0 |
| 109 // interface, we just need to write the last one in the array. |
| 110 for (int32_t i = formats.size() - 1; i >= 0; --i) { |
| 111 if (formats[i] == PP_FLASH_CLIPBOARD_FORMAT_PLAINTEXT) { |
| 112 rv = (get_interface<PPB_Flash_Clipboard_3_0>()->WritePlainText( |
| 113 instance->pp_instance(), |
| 114 clipboard_type, |
| 115 data_items[i].pp_var()) == PP_OK); |
| 116 break; |
| 117 } |
| 118 } |
70 } | 119 } |
| 120 |
71 return rv; | 121 return rv; |
72 } | 122 } |
73 | 123 |
74 } // namespace flash | 124 } // namespace flash |
75 } // namespace pp | 125 } // namespace pp |
OLD | NEW |