1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // A struct for managing data being dropped on a WebContents.  This represents
6 // a union of all the types of data that can be dropped in a platform neutral
7 // way.
8 
9 #ifndef CONTENT_PUBLIC_COMMON_DROP_DATA_H_
10 #define CONTENT_PUBLIC_COMMON_DROP_DATA_H_
11 
12 #include <stdint.h>
13 
14 #include <string>
15 #include <unordered_map>
16 #include <vector>
17 
18 #include "base/files/file_path.h"
19 #include "base/optional.h"
20 #include "base/strings/string16.h"
21 #include "content/common/content_export.h"
22 #include "ipc/ipc_message.h"
23 #include "services/network/public/mojom/referrer_policy.mojom.h"
24 #include "ui/base/dragdrop/file_info/file_info.h"
25 #include "url/gurl.h"
26 
27 namespace content {
28 
29 struct CONTENT_EXPORT DropData {
30   struct CONTENT_EXPORT FileSystemFileInfo {
31     // Writes file system files to the pickle.
32     static void WriteFileSystemFilesToPickle(
33         const std::vector<FileSystemFileInfo>& file_system_files,
34         base::Pickle* pickle);
35 
36     // Reads file system files from the pickle.
37     static bool ReadFileSystemFilesFromPickle(
38         const base::Pickle& pickle,
39         std::vector<FileSystemFileInfo>* file_system_files);
40 
41     GURL url;
42     int64_t size = 0;
43     std::string filesystem_id;
44   };
45 
46   enum class Kind {
47     STRING = 0,
48     FILENAME,
49     FILESYSTEMFILE,
50     LAST = FILESYSTEMFILE
51   };
52 
53   struct Metadata {
54     Metadata();
55     static Metadata CreateForMimeType(const Kind& kind,
56                                       const base::string16& mime_type);
57     static Metadata CreateForFilePath(const base::FilePath& filename);
58     static Metadata CreateForFileSystemUrl(const GURL& file_system_url);
59     Metadata(const Metadata& other);
60     ~Metadata();
61 
62     Kind kind;
63     base::string16 mime_type;
64     base::FilePath filename;
65     GURL file_system_url;
66   };
67 
68   DropData();
69   DropData(const DropData& other);
70   ~DropData();
71 
72   // Returns a sanitized filename to use for the dragged image, or base::nullopt
73   // if no sanitized name could be synthesized.
74   base::Optional<base::FilePath> GetSafeFilenameForImageFileContents() const;
75 
76   int view_id = MSG_ROUTING_NONE;
77 
78   // Whether this drag originated from a renderer.
79   bool did_originate_from_renderer;
80 
81   // User is dragging a link or image.
82   GURL url;
83   base::string16 url_title;  // The title associated with |url|.
84 
85   // User is dragging a link out-of the webview.
86   base::string16 download_metadata;
87 
88   // Referrer policy to use when dragging a link out of the webview results in
89   // a download.
90   network::mojom::ReferrerPolicy referrer_policy;
91 
92   // User is dropping one or more files on the webview. This field is only
93   // populated if the drag is not renderer tainted, as this allows File access
94   // from web content.
95   std::vector<ui::FileInfo> filenames;
96   // The mime types of dragged files.
97   std::vector<base::string16> file_mime_types;
98 
99   // Isolated filesystem ID for the files being dragged on the webview.
100   base::string16 filesystem_id;
101 
102   // User is dragging files specified with filesystem: URLs.
103   std::vector<FileSystemFileInfo> file_system_files;
104 
105   // User is dragging plain text into the webview.
106   base::Optional<base::string16> text;
107 
108   // User is dragging text/html into the webview (e.g., out of Firefox).
109   // |html_base_url| is the URL that the html fragment is taken from (used to
110   // resolve relative links).  It's ok for |html_base_url| to be empty.
111   base::Optional<base::string16> html;
112   GURL html_base_url;
113 
114   // User is dragging an image out of the WebView.
115   std::string file_contents;
116   GURL file_contents_source_url;
117   base::FilePath::StringType file_contents_filename_extension;
118   std::string file_contents_content_disposition;
119 
120   std::unordered_map<base::string16, base::string16> custom_data;
121 };
122 
123 }  // namespace content
124 
125 #endif  // CONTENT_PUBLIC_COMMON_DROP_DATA_H_
126