Home | History | Annotate | Download | only in private
      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.
      4 
      5 #include "ppapi/cpp/private/flash_clipboard.h"
      6 
      7 #include <vector>
      8 
      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"
     14 
     15 namespace pp {
     16 
     17 namespace {
     18 
     19 template <> const char* interface_name<PPB_Flash_Clipboard_4_0>() {
     20   return PPB_FLASH_CLIPBOARD_INTERFACE_4_0;
     21 }
     22 
     23 template <> const char* interface_name<PPB_Flash_Clipboard_5_0>() {
     24   return PPB_FLASH_CLIPBOARD_INTERFACE_5_0;
     25 }
     26 
     27 }  // namespace
     28 
     29 namespace flash {
     30 
     31 // static
     32 bool Clipboard::IsAvailable() {
     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;
     46 }
     47 
     48 // static
     49 bool Clipboard::IsFormatAvailable(const InstanceHandle& instance,
     50                                   PP_Flash_Clipboard_Type clipboard_type,
     51                                   uint32_t format) {
     52   bool rv = false;
     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)));
     60   }
     61   return rv;
     62 }
     63 
     64 // static
     65 bool Clipboard::ReadData(
     66     const InstanceHandle& instance,
     67     PP_Flash_Clipboard_Type clipboard_type,
     68     uint32_t format,
     69     Var* out) {
     70   bool rv = false;
     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>()) {
     79     PP_Var result = get_interface<PPB_Flash_Clipboard_4_0>()->ReadData(
     80         instance.pp_instance(),
     81         clipboard_type,
     82         static_cast<PP_Flash_Clipboard_Format>(format));
     83     *out = Var(PASS_REF, result);
     84     rv = true;
     85   }
     86   return rv;
     87 }
     88 
     89 // static
     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())
     96     return false;
     97 
     98   bool rv = false;
     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());
    104 
    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
    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.
    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];
    138     }
    139 
    140     rv = (get_interface<PPB_Flash_Clipboard_4_0>()->WriteData(
    141         instance.pp_instance(),
    142         clipboard_type,
    143         data_items.size(),
    144         formats_ptr,
    145         data_items_ptr) == PP_OK);
    146   }
    147 
    148   return rv;
    149 }
    150 
    151 }  // namespace flash
    152 }  // namespace pp
    153