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