PageRenderTime 38ms CodeModel.GetById 12ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/toolkit/content/nsDragAndDrop.js

http://github.com/zpao/v8monkey
JavaScript | 635 lines | 363 code | 65 blank | 207 comment | 58 complexity | 1f8334d82e61994cb741c5e2e86ebe89 MD5 | raw file
  1# -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2# ***** BEGIN LICENSE BLOCK *****
  3# Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4#
  5# The contents of this file are subject to the Mozilla Public License Version
  6# 1.1 (the "License"); you may not use this file except in compliance with
  7# the License. You may obtain a copy of the License at
  8# http://www.mozilla.org/MPL/
  9#
 10# Software distributed under the License is distributed on an "AS IS" basis,
 11# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 12# for the specific language governing rights and limitations under the
 13# License.
 14#
 15# The Original Code is mozilla.org code.
 16#
 17# The Initial Developer of the Original Code is
 18# Netscape Communications Corporation.
 19# Portions created by the Initial Developer are Copyright (C) 1998
 20# the Initial Developer. All Rights Reserved.
 21#
 22# Contributor(s):
 23#   Ben Goodger <ben@netscape.com> (Original Author)
 24#   Pierre Chanial <pierrechanial@netscape.net>
 25#
 26# Alternatively, the contents of this file may be used under the terms of
 27# either the GNU General Public License Version 2 or later (the "GPL"), or
 28# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 29# in which case the provisions of the GPL or the LGPL are applicable instead
 30# of those above. If you wish to allow use of your version of this file only
 31# under the terms of either the GPL or the LGPL, and not to allow others to
 32# use your version of this file under the terms of the MPL, indicate your
 33# decision by deleting the provisions above and replace them with the notice
 34# and other provisions required by the GPL or the LGPL. If you do not delete
 35# the provisions above, a recipient may use your version of this file under
 36# the terms of any one of the MPL, the GPL or the LGPL.
 37#
 38# ***** END LICENSE BLOCK *****
 39
 40////////////////////////////////////////////////////////////////////////
 41//
 42// USE OF THIS API FOR DRAG AND DROP IS DEPRECATED!
 43// Do not use this file for new code.
 44//
 45// For documentation about what to use instead, see:
 46//   http://developer.mozilla.org/En/DragDrop/Drag_and_Drop
 47//
 48////////////////////////////////////////////////////////////////////////
 49
 50
 51/** 
 52 *  nsTransferable - a wrapper for nsITransferable that simplifies
 53 *                   javascript clipboard and drag&drop. for use in
 54 *                   these situations you should use the nsClipboard
 55 *                   and nsDragAndDrop wrappers for more convenience
 56 **/ 
 57 
 58var nsTransferable = {
 59  /**
 60   * nsITransferable set (TransferData aTransferData) ;
 61   *
 62   * Creates a transferable with data for a list of supported types ("flavours")
 63   * 
 64   * @param TransferData aTransferData
 65   *        a javascript object in the format described above 
 66   **/ 
 67  set: function (aTransferDataSet)
 68    {
 69      var trans = this.createTransferable();
 70      for (var i = 0; i < aTransferDataSet.dataList.length; ++i) 
 71        {
 72          var currData = aTransferDataSet.dataList[i];
 73          var currFlavour = currData.flavour.contentType;
 74          trans.addDataFlavor(currFlavour);
 75          var supports = null; // nsISupports data
 76          var length = 0;
 77          if (currData.flavour.dataIIDKey == "nsISupportsString")
 78            {
 79              supports = Components.classes["@mozilla.org/supports-string;1"]
 80                                   .createInstance(Components.interfaces.nsISupportsString);
 81
 82              supports.data = currData.supports;
 83              length = supports.data.length;
 84            }
 85          else 
 86            {
 87              // non-string data.
 88              supports = currData.supports;
 89              length = 0; // kFlavorHasDataProvider
 90            }
 91          trans.setTransferData(currFlavour, supports, length * 2);
 92        }
 93      return trans;
 94    },
 95  
 96  /**
 97   * TransferData/TransferDataSet get (FlavourSet aFlavourSet, 
 98   *                                   Function aRetrievalFunc, Boolean aAnyFlag) ;
 99   *
100   * Retrieves data from the transferable provided in aRetrievalFunc, formatted
101   * for more convenient access.
102   *
103   * @param FlavourSet aFlavourSet
104   *        a FlavourSet object that contains a list of supported flavours.
105   * @param Function aRetrievalFunc
106   *        a reference to a function that returns a nsISupportsArray of nsITransferables
107   *        for each item from the specified source (clipboard/drag&drop etc)
108   * @param Boolean aAnyFlag
109   *        a flag specifying whether or not a specific flavour is requested. If false,
110   *        data of the type of the first flavour in the flavourlist parameter is returned,
111   *        otherwise the best flavour supported will be returned.
112   **/
113  get: function (aFlavourSet, aRetrievalFunc, aAnyFlag)
114    {
115      if (!aRetrievalFunc) 
116        throw "No data retrieval handler provided!";
117      
118      var supportsArray = aRetrievalFunc(aFlavourSet);
119      var dataArray = [];
120      var count = supportsArray.Count();
121      
122      // Iterate over the number of items returned from aRetrievalFunc. For
123      // clipboard operations, this is 1, for drag and drop (where multiple
124      // items may have been dragged) this could be >1.
125      for (var i = 0; i < count; i++)
126        {
127          var trans = supportsArray.GetElementAt(i);
128          if (!trans) continue;
129          trans = trans.QueryInterface(Components.interfaces.nsITransferable);
130            
131          var data = { };
132          var length = { };
133          
134          var currData = null;
135          if (aAnyFlag)
136            { 
137              var flavour = { };
138              trans.getAnyTransferData(flavour, data, length);
139              if (data && flavour)
140                {
141                  var selectedFlavour = aFlavourSet.flavourTable[flavour.value];
142                  if (selectedFlavour) 
143                    dataArray[i] = FlavourToXfer(data.value, length.value, selectedFlavour);
144                }
145            }
146          else
147            {
148              var firstFlavour = aFlavourSet.flavours[0];
149              trans.getTransferData(firstFlavour, data, length);
150              if (data && firstFlavour)
151                dataArray[i] = FlavourToXfer(data.value, length.value, firstFlavour);
152            }
153        }
154      return new TransferDataSet(dataArray);
155    },
156
157  /** 
158   * nsITransferable createTransferable (void) ;
159   *
160   * Creates and returns a transferable object.
161   **/    
162  createTransferable: function ()
163    {
164      const kXferableContractID = "@mozilla.org/widget/transferable;1";
165      const kXferableIID = Components.interfaces.nsITransferable;
166      return Components.classes[kXferableContractID].createInstance(kXferableIID);
167    }
168};  
169
170/** 
171 * A FlavourSet is a simple type that represents a collection of Flavour objects.
172 * FlavourSet is constructed from an array of Flavours, and stores this list as
173 * an array and a hashtable. The rationale for the dual storage is as follows:
174 * 
175 * Array: Ordering is important when adding data flavours to a transferable. 
176 *        Flavours added first are deemed to be 'preferred' by the client. 
177 * Hash:  Convenient lookup of flavour data using the content type (MIME type)
178 *        of data as a key. 
179 */
180function FlavourSet(aFlavourList)
181{
182  this.flavours = aFlavourList || [];
183  this.flavourTable = { };
184
185  this._XferID = "FlavourSet";
186  
187  for (var i = 0; i < this.flavours.length; ++i)
188    this.flavourTable[this.flavours[i].contentType] = this.flavours[i];
189}
190
191FlavourSet.prototype = {
192  appendFlavour: function (aFlavour, aFlavourIIDKey)
193  {
194    var flavour = new Flavour (aFlavour, aFlavourIIDKey);
195    this.flavours.push(flavour);
196    this.flavourTable[flavour.contentType] = flavour;
197  }
198};
199
200/** 
201 * A Flavour is a simple type that represents a data type that can be handled. 
202 * It takes a content type (MIME type) which is used when storing data on the
203 * system clipboard/drag and drop, and an IIDKey (string interface name
204 * which is used to QI data to an appropriate form. The default interface is
205 * assumed to be wide-string.
206 */ 
207function Flavour(aContentType, aDataIIDKey)
208{
209  this.contentType = aContentType;
210  this.dataIIDKey = aDataIIDKey || "nsISupportsString";
211
212  this._XferID = "Flavour";
213}
214
215function TransferDataBase() {}
216TransferDataBase.prototype = {
217  push: function (aItems)
218  {
219    this.dataList.push(aItems);
220  },
221
222  get first ()
223  {
224    return "dataList" in this && this.dataList.length ? this.dataList[0] : null;
225  }
226};
227
228/** 
229 * TransferDataSet is a list (array) of TransferData objects, which represents
230 * data dragged from one or more elements. 
231 */
232function TransferDataSet(aTransferDataList)
233{
234  this.dataList = aTransferDataList || [];
235
236  this._XferID = "TransferDataSet";
237}
238TransferDataSet.prototype = TransferDataBase.prototype;
239
240/** 
241 * TransferData is a list (array) of FlavourData for all the applicable content
242 * types associated with a drag from a single item. 
243 */
244function TransferData(aFlavourDataList)
245{
246  this.dataList = aFlavourDataList || [];
247
248  this._XferID = "TransferData";
249}
250TransferData.prototype = {
251  __proto__: TransferDataBase.prototype,
252  
253  addDataForFlavour: function (aFlavourString, aData, aLength, aDataIIDKey)
254  {
255    this.dataList.push(new FlavourData(aData, aLength, 
256                       new Flavour(aFlavourString, aDataIIDKey)));
257  }
258};
259
260/** 
261 * FlavourData is a type that represents data retrieved from the system 
262 * clipboard or drag and drop. It is constructed internally by the Transferable
263 * using the raw (nsISupports) data from the clipboard, the length of the data,
264 * and an object of type Flavour representing the type. Clients implementing
265 * IDragDropObserver receive an object of this type in their implementation of
266 * onDrop. They access the 'data' property to retrieve data, which is either data 
267 * QI'ed to a usable form, or unicode string. 
268 */
269function FlavourData(aData, aLength, aFlavour) 
270{
271  this.supports = aData;
272  this.contentLength = aLength;
273  this.flavour = aFlavour || null;
274  
275  this._XferID = "FlavourData";
276}
277
278FlavourData.prototype = {
279  get data ()
280  {
281    if (this.flavour &&
282        this.flavour.dataIIDKey != "nsISupportsString")
283      return this.supports.QueryInterface(Components.interfaces[this.flavour.dataIIDKey]); 
284
285    var supports = this.supports;
286    if (supports instanceof Components.interfaces.nsISupportsString)
287      return supports.data.substring(0, this.contentLength/2);
288     
289    return supports;
290  }
291}
292
293/** 
294 * Create a TransferData object with a single FlavourData entry. Used when 
295 * unwrapping data of a specific flavour from the drag service. 
296 */
297function FlavourToXfer(aData, aLength, aFlavour) 
298{
299  return new TransferData([new FlavourData(aData, aLength, aFlavour)]);
300}
301
302var transferUtils = {
303
304  retrieveURLFromData: function (aData, flavour)
305  {
306    switch (flavour) {
307      case "text/unicode":
308      case "text/plain":
309      case "text/x-moz-text-internal":
310        return aData.replace(/^\s+|\s+$/g, "");
311      case "text/x-moz-url":
312        return ((aData instanceof Components.interfaces.nsISupportsString) ? aData.toString() : aData).split("\n")[0];
313      case "application/x-moz-file":
314        var ioService = Components.classes["@mozilla.org/network/io-service;1"]
315                                  .getService(Components.interfaces.nsIIOService);
316        var fileHandler = ioService.getProtocolHandler("file")
317                                   .QueryInterface(Components.interfaces.nsIFileProtocolHandler);
318        return fileHandler.getURLSpecFromFile(aData);
319    }
320    return null;                                                   
321  }
322
323}
324
325/**
326 * nsDragAndDrop - a convenience wrapper for nsTransferable, nsITransferable
327 *                 and nsIDragService/nsIDragSession. 
328 *
329 * Use: map the handler functions to the 'ondraggesture', 'ondragover' and
330 *   'ondragdrop' event handlers on your XML element, e.g.                   
331 *   <xmlelement ondraggesture="nsDragAndDrop.startDrag(event, observer);"   
332 *               ondragover="nsDragAndDrop.dragOver(event, observer);"      
333 *               ondragdrop="nsDragAndDrop.drop(event, observer);"/>         
334 *                                                                           
335 *   You need to create an observer js object with the following member      
336 *   functions:                                                              
337 *     Object onDragStart (event)        // called when drag initiated,      
338 *                                       // returns flavour list with data   
339 *                                       // to stuff into transferable      
340 *     void onDragOver (Object flavour)  // called when element is dragged   
341 *                                       // over, so that it can perform     
342 *                                       // any drag-over feedback for provided
343 *                                       // flavour                          
344 *     void onDrop (Object data)         // formatted data object dropped.   
345 *     Object getSupportedFlavours ()    // returns a flavour list so that   
346 *                                       // nsTransferable can determine
347 *                                       // whether or not to accept drop. 
348 **/   
349
350var nsDragAndDrop = {
351  
352  _mDS: null,
353  get mDragService()
354    {
355      if (!this._mDS) 
356        {
357          const kDSContractID = "@mozilla.org/widget/dragservice;1";
358          const kDSIID = Components.interfaces.nsIDragService;
359          this._mDS = Components.classes[kDSContractID].getService(kDSIID);
360        }
361      return this._mDS;
362    },
363
364  /**
365   * void startDrag (DOMEvent aEvent, Object aDragDropObserver) ;
366   *
367   * called when a drag on an element is started.
368   *
369   * @param DOMEvent aEvent
370   *        the DOM event fired by the drag init
371   * @param Object aDragDropObserver
372   *        javascript object of format described above that specifies
373   *        the way in which the element responds to drag events.
374   **/  
375  startDrag: function (aEvent, aDragDropObserver)
376    {
377      if (!("onDragStart" in aDragDropObserver))
378        return;
379
380      const kDSIID = Components.interfaces.nsIDragService;
381      var dragAction = { action: kDSIID.DRAGDROP_ACTION_COPY + kDSIID.DRAGDROP_ACTION_MOVE + kDSIID.DRAGDROP_ACTION_LINK };
382
383      var transferData = { data: null };
384      try 
385        {
386          aDragDropObserver.onDragStart(aEvent, transferData, dragAction);
387        }
388      catch (e) 
389        {
390          return;  // not a draggable item, bail!
391        }
392
393      if (!transferData.data) return;
394      transferData = transferData.data;
395
396      var dt = aEvent.dataTransfer;
397      var count = 0;
398      do {
399        var tds = transferData._XferID == "TransferData" 
400                                         ? transferData 
401                                         : transferData.dataList[count]
402        for (var i = 0; i < tds.dataList.length; ++i) 
403        {
404          var currData = tds.dataList[i];
405          var currFlavour = currData.flavour.contentType;
406          var value = currData.supports;
407          if (value instanceof Components.interfaces.nsISupportsString)
408            value = value.toString();
409          dt.mozSetDataAt(currFlavour, value, count);
410        }
411
412        count++;
413      }
414      while (transferData._XferID == "TransferDataSet" && 
415             count < transferData.dataList.length);
416
417      dt.effectAllowed = "all";
418      // a drag targeted at a tree should instead use the treechildren so that
419      // the current selection is used as the drag feedback
420      dt.addElement(aEvent.originalTarget.localName == "treechildren" ?
421                    aEvent.originalTarget : aEvent.target);
422      aEvent.stopPropagation();
423    },
424
425  /** 
426   * void dragOver (DOMEvent aEvent, Object aDragDropObserver) ;
427   *
428   * called when a drag passes over this element
429   *
430   * @param DOMEvent aEvent
431   *        the DOM event fired by passing over the element
432   * @param Object aDragDropObserver
433   *        javascript object of format described above that specifies
434   *        the way in which the element responds to drag events.
435   **/
436  dragOver: function (aEvent, aDragDropObserver)
437    { 
438      if (!("onDragOver" in aDragDropObserver)) 
439        return;
440      if (!this.checkCanDrop(aEvent, aDragDropObserver))
441        return;
442      var flavourSet = aDragDropObserver.getSupportedFlavours();
443      for (var flavour in flavourSet.flavourTable)
444        {
445          if (this.mDragSession.isDataFlavorSupported(flavour))
446            {
447              aDragDropObserver.onDragOver(aEvent, 
448                                           flavourSet.flavourTable[flavour], 
449                                           this.mDragSession);
450              aEvent.stopPropagation();
451              aEvent.preventDefault();
452              break;
453            }
454        }
455    },
456
457  mDragSession: null,
458
459  /** 
460   * void drop (DOMEvent aEvent, Object aDragDropObserver) ;
461   *
462   * called when the user drops on the element
463   *
464   * @param DOMEvent aEvent
465   *        the DOM event fired by the drop
466   * @param Object aDragDropObserver
467   *        javascript object of format described above that specifies
468   *        the way in which the element responds to drag events.
469   **/
470  drop: function (aEvent, aDragDropObserver)
471    {
472      if (!("onDrop" in aDragDropObserver))
473        return;
474      if (!this.checkCanDrop(aEvent, aDragDropObserver))
475        return;  
476
477      var flavourSet = aDragDropObserver.getSupportedFlavours();
478
479      var dt = aEvent.dataTransfer;
480      var dataArray = [];
481      var count = dt.mozItemCount;
482      for (var i = 0; i < count; ++i) {
483        var types = dt.mozTypesAt(i);
484        for (var j = 0; j < flavourSet.flavours.length; j++) {
485          var type = flavourSet.flavours[j].contentType;
486          // dataTransfer uses text/plain but older code used text/unicode, so
487          // switch this for compatibility
488          var modtype = (type == "text/unicode") ? "text/plain" : type;
489          if (Array.indexOf(types, modtype) >= 0) {
490            var data = dt.mozGetDataAt(modtype, i);
491            if (data) {
492              // Non-strings need some non-zero value used for their data length.
493              const kNonStringDataLength = 4;
494
495              var length = (typeof data == "string") ? data.length : kNonStringDataLength;
496              dataArray[i] = FlavourToXfer(data, length, flavourSet.flavourTable[type]);
497              break;
498            }
499          }
500        }
501      }
502
503      var transferData = new TransferDataSet(dataArray)
504
505      // hand over to the client to respond to dropped data
506      var multiple = "canHandleMultipleItems" in aDragDropObserver && aDragDropObserver.canHandleMultipleItems;
507      var dropData = multiple ? transferData : transferData.first.first;
508      aDragDropObserver.onDrop(aEvent, dropData, this.mDragSession);
509      aEvent.stopPropagation();
510    },
511
512  /** 
513   * void dragExit (DOMEvent aEvent, Object aDragDropObserver) ;
514   *
515   * called when a drag leaves this element
516   *
517   * @param DOMEvent aEvent
518   *        the DOM event fired by leaving the element
519   * @param Object aDragDropObserver
520   *        javascript object of format described above that specifies
521   *        the way in which the element responds to drag events.
522   **/
523  dragExit: function (aEvent, aDragDropObserver)
524    {
525      if (!this.checkCanDrop(aEvent, aDragDropObserver))
526        return;
527      if ("onDragExit" in aDragDropObserver)
528        aDragDropObserver.onDragExit(aEvent, this.mDragSession);
529    },  
530    
531  /** 
532   * void dragEnter (DOMEvent aEvent, Object aDragDropObserver) ;
533   *
534   * called when a drag enters in this element
535   *
536   * @param DOMEvent aEvent
537   *        the DOM event fired by entering in the element
538   * @param Object aDragDropObserver
539   *        javascript object of format described above that specifies
540   *        the way in which the element responds to drag events.
541   **/
542  dragEnter: function (aEvent, aDragDropObserver)
543    {
544      if (!this.checkCanDrop(aEvent, aDragDropObserver))
545        return;
546      if ("onDragEnter" in aDragDropObserver)
547        aDragDropObserver.onDragEnter(aEvent, this.mDragSession);
548    },  
549
550  /** 
551   * Boolean checkCanDrop (DOMEvent aEvent, Object aDragDropObserver) ;
552   *
553   * Sets the canDrop attribute for the drag session.
554   * returns false if there is no current drag session.
555   *
556   * @param DOMEvent aEvent
557   *        the DOM event fired by the drop
558   * @param Object aDragDropObserver
559   *        javascript object of format described above that specifies
560   *        the way in which the element responds to drag events.
561   **/
562  checkCanDrop: function (aEvent, aDragDropObserver)
563    {
564      if (!this.mDragSession) 
565        this.mDragSession = this.mDragService.getCurrentSession();
566      if (!this.mDragSession) 
567        return false;
568      this.mDragSession.canDrop = this.mDragSession.sourceNode != aEvent.target;
569      if ("canDrop" in aDragDropObserver)
570        this.mDragSession.canDrop &= aDragDropObserver.canDrop(aEvent, this.mDragSession);
571      return true;
572    },
573
574  /**
575   * Do a security check for drag n' drop. Make sure the source document
576   * can load the dragged link.
577   *
578   * @param DOMEvent aEvent
579   *        the DOM event fired by leaving the element
580   * @param Object aDragDropObserver
581   *        javascript object of format described above that specifies
582   *        the way in which the element responds to drag events.
583   * @param String aDraggedText
584   *        the text being dragged
585   **/
586  dragDropSecurityCheck: function (aEvent, aDragSession, aDraggedText)
587    {
588      // Strip leading and trailing whitespace, then try to create a
589      // URI from the dropped string. If that succeeds, we're
590      // dropping a URI and we need to do a security check to make
591      // sure the source document can load the dropped URI. We don't
592      // so much care about creating the real URI here
593      // (i.e. encoding differences etc don't matter), we just want
594      // to know if aDraggedText really is a URI.
595
596      aDraggedText = aDraggedText.replace(/^\s*|\s*$/g, '');
597
598      var uri;
599
600      try {
601        uri = Components.classes["@mozilla.org/network/io-service;1"]
602                        .getService(Components.interfaces.nsIIOService)
603                        .newURI(aDraggedText, null, null);
604      } catch (e) {
605      }
606
607      if (!uri)
608        return;
609
610      // aDraggedText is a URI, do the security check.
611      const nsIScriptSecurityManager = Components.interfaces
612                                                 .nsIScriptSecurityManager;
613      var secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
614                             .getService(nsIScriptSecurityManager);
615
616      if (!aDragSession)
617        aDragSession = this.mDragService.getCurrentSession();
618
619      var sourceDoc = aDragSession.sourceDocument;
620      // Use "file:///" as the default sourceURI so that drops of file:// URIs
621      // are always allowed.
622      var sourceURI = sourceDoc ? sourceDoc.documentURI : "file:///";
623
624      try {
625        secMan.checkLoadURIStr(sourceURI, aDraggedText,
626                               nsIScriptSecurityManager.STANDARD);
627      } catch (e) {
628        // Stop event propagation right here.
629        aEvent.stopPropagation();
630
631        throw "Drop of " + aDraggedText + " denied.";
632      }
633    }
634};
635