Home | History | Annotate | Download | only in chromium
      1 /*
      2  * Copyright (C) 2011 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include "config.h"
     32 #include "DataTransferItemsChromium.h"
     33 
     34 #if ENABLE(DATA_TRANSFER_ITEMS)
     35 
     36 #include "Clipboard.h"
     37 #include "DataTransferItemChromium.h"
     38 #include "ExceptionCode.h"
     39 
     40 namespace WebCore {
     41 
     42 PassRefPtr<DataTransferItemsChromium> DataTransferItemsChromium::create(PassRefPtr<Clipboard> owner, ScriptExecutionContext* context)
     43 {
     44     return adoptRef(new DataTransferItemsChromium(owner, context));
     45 }
     46 
     47 DataTransferItemsChromium::DataTransferItemsChromium(PassRefPtr<Clipboard> owner, ScriptExecutionContext* context)
     48     : m_owner(owner)
     49     , m_context(context)
     50 {
     51 }
     52 
     53 unsigned long DataTransferItemsChromium::length() const
     54 {
     55     if (m_owner->policy() == ClipboardNumb)
     56         return 0;
     57     return m_items.size();
     58 }
     59 
     60 PassRefPtr<DataTransferItem> DataTransferItemsChromium::item(unsigned long index) const
     61 {
     62     if (m_owner->policy() == ClipboardNumb || index >= length())
     63         return 0;
     64     return m_items[index];
     65 }
     66 
     67 void DataTransferItemsChromium::deleteItem(unsigned long index, ExceptionCode& ec)
     68 {
     69     if (m_owner->policy() != ClipboardWritable) {
     70         ec = INVALID_STATE_ERR;
     71         return;
     72     }
     73 
     74     if (index >= length())
     75         return;
     76 
     77     m_items.remove(index);
     78 }
     79 
     80 void DataTransferItemsChromium::clear()
     81 {
     82     if (m_owner->policy() != ClipboardWritable)
     83         return;
     84 
     85     m_items.clear();
     86 }
     87 
     88 void DataTransferItemsChromium::add(const String& data, const String& type, ExceptionCode& ec)
     89 {
     90     if (m_owner->policy() != ClipboardWritable)
     91         return;
     92 
     93     // Only one 'string' item with a given type is allowed in the collection.
     94     for (size_t i = 0; i < m_items.size(); ++i) {
     95         if (m_items[i]->type() == type && m_items[i]->kind() == DataTransferItem::kindString) {
     96             ec = INVALID_STATE_ERR;
     97             return;
     98         }
     99     }
    100 
    101     m_items.append(DataTransferItemChromium::create(m_owner, m_context, data, type));
    102 }
    103 
    104 void DataTransferItemsChromium::addPasteboardItem(const String& type)
    105 {
    106     m_items.append(DataTransferItemChromium::createFromPasteboard(m_owner, m_context, type));
    107 }
    108 
    109 } // namespace WebCore
    110 
    111 #endif // ENABLE(DATA_TRANSFER_ITEMS)
    112