1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nsTransferable_h__
7 #define nsTransferable_h__
8 
9 #include "nsICookieJarSettings.h"
10 #include "nsIFormatConverter.h"
11 #include "nsITransferable.h"
12 #include "nsCOMPtr.h"
13 #include "nsString.h"
14 #include "nsTArray.h"
15 #include "nsIPrincipal.h"
16 #include "prio.h"
17 #include "mozilla/Maybe.h"
18 
19 class nsIMutableArray;
20 
21 //
22 // DataStruct
23 //
24 // Holds a flavor (a mime type) that describes the data and the associated data.
25 //
26 struct DataStruct {
DataStructDataStruct27   explicit DataStruct(const char* aFlavor)
28       : mCacheFD(nullptr), mFlavor(aFlavor) {}
29   DataStruct(DataStruct&& aRHS);
30   ~DataStruct();
31 
GetFlavorDataStruct32   const nsCString& GetFlavor() const { return mFlavor; }
33   void SetData(nsISupports* aData, bool aIsPrivateData);
34   void GetData(nsISupports** aData);
IsDataAvailableDataStruct35   bool IsDataAvailable() const { return mData || mCacheFD; }
36 
37  protected:
38   enum {
39     // The size of data over which we write the data to disk rather than
40     // keep it around in memory.
41     kLargeDatasetSize = 1000000  // 1 million bytes
42   };
43 
44   nsresult WriteCache(void* aData, uint32_t aDataLen);
45   nsresult ReadCache(nsISupports** aData);
46 
47   // mData OR mCacheFD should be used, not both.
48   nsCOMPtr<nsISupports> mData;  // OWNER - some varient of primitive wrapper
49   PRFileDesc* mCacheFD;
50   const nsCString mFlavor;
51 
52  private:
53   DataStruct(const DataStruct&) = delete;
54   DataStruct& operator=(const DataStruct&) = delete;
55 };
56 
57 /**
58  * XP Transferable wrapper
59  */
60 
61 class nsTransferable : public nsITransferable {
62  public:
63   nsTransferable();
64 
65   // nsISupports
66   NS_DECL_ISUPPORTS
67   NS_DECL_NSITRANSFERABLE
68 
69  protected:
70   virtual ~nsTransferable();
71 
72   // Get flavors w/out converter
73   void GetTransferDataFlavors(nsTArray<nsCString>& aFlavors);
74 
75   // Find index for data with the matching flavor in mDataArray.
76   mozilla::Maybe<size_t> FindDataFlavor(const char* aFlavor);
77 
78   nsTArray<DataStruct> mDataArray;
79   nsCOMPtr<nsIFormatConverter> mFormatConv;
80   bool mPrivateData;
81   nsCOMPtr<nsIPrincipal> mRequestingPrincipal;
82   nsContentPolicyType mContentPolicyType;
83   nsCOMPtr<nsICookieJarSettings> mCookieJarSettings;
84 #if DEBUG
85   bool mInitialized;
86 #endif
87 };
88 
89 #endif  // nsTransferable_h__
90