Home | History | Annotate | Download | only in web
      1 /*
      2  * Copyright (C) 2009 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 
     33 #include "core/clipboard/DataObject.h"
     34 #include "core/clipboard/DataTransferItem.h"
     35 #include "modules/filesystem/DraggedIsolatedFileSystem.h"
     36 #include "platform/clipboard/ClipboardMimeTypes.h"
     37 #include "platform/heap/Handle.h"
     38 #include "public/platform/WebData.h"
     39 #include "public/platform/WebDragData.h"
     40 #include "public/platform/WebString.h"
     41 #include "public/platform/WebURL.h"
     42 #include "public/platform/WebVector.h"
     43 #include "wtf/HashMap.h"
     44 #include "wtf/PassRefPtr.h"
     45 
     46 namespace blink {
     47 
     48 void WebDragData::initialize()
     49 {
     50     m_private = DataObject::create();
     51 }
     52 
     53 void WebDragData::reset()
     54 {
     55     m_private.reset();
     56 }
     57 
     58 void WebDragData::assign(const WebDragData& other)
     59 {
     60     m_private = other.m_private;
     61 }
     62 
     63 WebDragData::WebDragData(const PassRefPtrWillBeRawPtr<DataObject>& object)
     64 {
     65     m_private = object;
     66 }
     67 
     68 WebDragData& WebDragData::operator=(const PassRefPtrWillBeRawPtr<DataObject>& object)
     69 {
     70     m_private = object;
     71     return *this;
     72 }
     73 
     74 DataObject* WebDragData::getValue() const
     75 {
     76     return m_private.get();
     77 }
     78 
     79 void WebDragData::ensureMutable()
     80 {
     81     ASSERT(!isNull());
     82 }
     83 
     84 WebVector<WebDragData::Item> WebDragData::items() const
     85 {
     86     Vector<Item> itemList;
     87     for (size_t i = 0; i < m_private->length(); ++i) {
     88         DataObjectItem* originalItem = m_private->item(i).get();
     89         WebDragData::Item item;
     90         if (originalItem->kind() == DataObjectItem::StringKind) {
     91             item.storageType = Item::StorageTypeString;
     92             item.stringType = originalItem->type();
     93             item.stringData = originalItem->getAsString();
     94         } else if (originalItem->kind() == DataObjectItem::FileKind) {
     95             if (originalItem->sharedBuffer()) {
     96                 item.storageType = Item::StorageTypeBinaryData;
     97                 item.binaryData = originalItem->sharedBuffer();
     98             } else if (originalItem->isFilename()) {
     99                 RefPtrWillBeRawPtr<Blob> blob = originalItem->getAsFile();
    100                 if (blob->isFile()) {
    101                     File* file = toFile(blob.get());
    102                     if (file->hasBackingFile()) {
    103                         if (file->userVisibility() == File::IsUserVisible) {
    104                             item.storageType = Item::StorageTypeFilename;
    105                             item.filenameData = file->path();
    106                             item.displayNameData = file->name();
    107                         } else {
    108                             item.storageType = Item::StorageTypeFileSystemFile;
    109                             item.fileSystemURL = file->fileSystemURL();
    110                             item.fileSystemFileSize = file->size();
    111                         }
    112                     } else {
    113                         // FIXME: support dragging constructed Files across renderers, see http://crbug.com/394955
    114                         item.storageType = Item::StorageTypeString;
    115                         item.stringType = "text/plain";
    116                         item.stringData = file->name();
    117                     }
    118                 } else {
    119                     ASSERT_NOT_REACHED();
    120                 }
    121             } else {
    122                 ASSERT_NOT_REACHED();
    123             }
    124         } else {
    125             ASSERT_NOT_REACHED();
    126         }
    127         item.title = originalItem->title();
    128         item.baseURL = originalItem->baseURL();
    129         itemList.append(item);
    130     }
    131     return itemList;
    132 }
    133 
    134 void WebDragData::setItems(const WebVector<Item>& itemList)
    135 {
    136     m_private->clearAll();
    137     for (size_t i = 0; i < itemList.size(); ++i)
    138         addItem(itemList[i]);
    139 }
    140 
    141 void WebDragData::addItem(const Item& item)
    142 {
    143     ensureMutable();
    144     switch (item.storageType) {
    145     case Item::StorageTypeString:
    146         if (String(item.stringType) == mimeTypeTextURIList)
    147             m_private->setURLAndTitle(item.stringData, item.title);
    148         else if (String(item.stringType) == mimeTypeTextHTML)
    149             m_private->setHTMLAndBaseURL(item.stringData, item.baseURL);
    150         else
    151             m_private->setData(item.stringType, item.stringData);
    152         return;
    153     case Item::StorageTypeFilename:
    154         m_private->addFilename(item.filenameData, item.displayNameData);
    155         return;
    156     case Item::StorageTypeBinaryData:
    157         // This should never happen when dragging in.
    158         ASSERT_NOT_REACHED();
    159         return;
    160     case Item::StorageTypeFileSystemFile:
    161         {
    162             FileMetadata fileMetadata;
    163             fileMetadata.length = item.fileSystemFileSize;
    164             m_private->add(File::createForFileSystemFile(item.fileSystemURL, fileMetadata));
    165         }
    166         return;
    167     }
    168 }
    169 
    170 WebString WebDragData::filesystemId() const
    171 {
    172     ASSERT(!isNull());
    173     return m_private.get()->filesystemId();
    174 }
    175 
    176 void WebDragData::setFilesystemId(const WebString& filesystemId)
    177 {
    178     // The ID is an opaque string, given by and validated by chromium port.
    179     ensureMutable();
    180     DraggedIsolatedFileSystem::provideTo(*m_private.get(), DraggedIsolatedFileSystem::supplementName(), DraggedIsolatedFileSystem::create(*m_private.get(), filesystemId));
    181 }
    182 
    183 } // namespace blink
    184