Home | History | Annotate | Download | only in dom
      1 /*
      2  * Copyright (C) 2006, 2007, 2008 Apple 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
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "Clipboard.h"
     28 
     29 #include "CachedImage.h"
     30 #include "DOMImplementation.h"
     31 #include "Frame.h"
     32 #include "FrameLoader.h"
     33 #include "Image.h"
     34 
     35 namespace WebCore {
     36 
     37 Clipboard::Clipboard(ClipboardAccessPolicy policy, bool isForDragging)
     38     : m_policy(policy)
     39     , m_dropEffect("none")
     40     , m_effectAllowed("uninitialized")
     41     , m_dragStarted(false)
     42     , m_forDragging(isForDragging)
     43     , m_dragImage(0)
     44 {
     45 }
     46 
     47 void Clipboard::setAccessPolicy(ClipboardAccessPolicy policy)
     48 {
     49     // once you go numb, can never go back
     50     ASSERT(m_policy != ClipboardNumb || policy == ClipboardNumb);
     51     m_policy = policy;
     52 }
     53 
     54 // These "conversion" methods are called by both WebCore and WebKit, and never make sense to JS, so we don't
     55 // worry about security for these. They don't allow access to the pasteboard anyway.
     56 
     57 static DragOperation dragOpFromIEOp(const String& op)
     58 {
     59     // yep, it's really just this fixed set
     60     if (op == "uninitialized")
     61         return DragOperationEvery;
     62     if (op == "none")
     63         return DragOperationNone;
     64     if (op == "copy")
     65         return DragOperationCopy;
     66     if (op == "link")
     67         return DragOperationLink;
     68     if (op == "move")
     69         return DragOperationGeneric;    // FIXME: Why is this DragOperationGeneric? <http://webkit.org/b/33697>
     70     if (op == "copyLink")
     71         return (DragOperation)(DragOperationCopy | DragOperationLink);
     72     if (op == "copyMove")
     73         return (DragOperation)(DragOperationCopy | DragOperationGeneric | DragOperationMove);
     74     if (op == "linkMove")
     75         return (DragOperation)(DragOperationLink | DragOperationGeneric | DragOperationMove);
     76     if (op == "all")
     77         return DragOperationEvery;
     78     return DragOperationPrivate;  // really a marker for "no conversion"
     79 }
     80 
     81 static String IEOpFromDragOp(DragOperation op)
     82 {
     83     bool moveSet = !!((DragOperationGeneric | DragOperationMove) & op);
     84 
     85     if ((moveSet && (op & DragOperationCopy) && (op & DragOperationLink))
     86         || (op == DragOperationEvery))
     87         return "all";
     88     if (moveSet && (op & DragOperationCopy))
     89         return "copyMove";
     90     if (moveSet && (op & DragOperationLink))
     91         return "linkMove";
     92     if ((op & DragOperationCopy) && (op & DragOperationLink))
     93         return "copyLink";
     94     if (moveSet)
     95         return "move";
     96     if (op & DragOperationCopy)
     97         return "copy";
     98     if (op & DragOperationLink)
     99         return "link";
    100     return "none";
    101 }
    102 
    103 DragOperation Clipboard::sourceOperation() const
    104 {
    105     DragOperation op = dragOpFromIEOp(m_effectAllowed);
    106     ASSERT(op != DragOperationPrivate);
    107     return op;
    108 }
    109 
    110 DragOperation Clipboard::destinationOperation() const
    111 {
    112     DragOperation op = dragOpFromIEOp(m_dropEffect);
    113     ASSERT(op == DragOperationCopy || op == DragOperationNone || op == DragOperationLink || op == DragOperationGeneric || op == DragOperationMove);
    114     return op;
    115 }
    116 
    117 void Clipboard::setSourceOperation(DragOperation op)
    118 {
    119     ASSERT_ARG(op, op != DragOperationPrivate);
    120     m_effectAllowed = IEOpFromDragOp(op);
    121 }
    122 
    123 void Clipboard::setDestinationOperation(DragOperation op)
    124 {
    125     ASSERT_ARG(op, op == DragOperationCopy || op == DragOperationNone || op == DragOperationLink || op == DragOperationGeneric || op == DragOperationMove);
    126     m_dropEffect = IEOpFromDragOp(op);
    127 }
    128 
    129 void Clipboard::setDropEffect(const String &effect)
    130 {
    131     if (!m_forDragging)
    132         return;
    133 
    134     // The attribute must ignore any attempts to set it to a value other than none, copy, link, and move.
    135     if (effect != "none" && effect != "copy"  && effect != "link" && effect != "move")
    136         return;
    137 
    138     if (m_policy == ClipboardReadable || m_policy == ClipboardTypesReadable)
    139         m_dropEffect = effect;
    140 }
    141 
    142 void Clipboard::setEffectAllowed(const String &effect)
    143 {
    144     if (!m_forDragging)
    145         return;
    146 
    147     if (dragOpFromIEOp(effect) == DragOperationPrivate) {
    148         // This means that there was no conversion, and the effectAllowed that
    149         // we are passed isn't a valid effectAllowed, so we should ignore it,
    150         // and not set m_effectAllowed.
    151 
    152         // The attribute must ignore any attempts to set it to a value other than
    153         // none, copy, copyLink, copyMove, link, linkMove, move, all, and uninitialized.
    154         return;
    155     }
    156 
    157 
    158     if (m_policy == ClipboardWritable)
    159         m_effectAllowed = effect;
    160 }
    161 
    162 } // namespace WebCore
    163