|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/ |
| 4 | + |
| 5 | +import Common |
| 6 | +import WebKit |
| 7 | +import Photos |
| 8 | +import Shared |
| 9 | + |
| 10 | +class ActionProviderBuilder { |
| 11 | + private var actions = [UIAction]() |
| 12 | + |
| 13 | + func build() -> [UIAction] { |
| 14 | + return actions |
| 15 | + } |
| 16 | + |
| 17 | + func addOpenInNewTab(url: URL, currentTab: Tab, addTab: @escaping (URL, Bool, Tab) -> Void) { |
| 18 | + actions.append( |
| 19 | + UIAction( |
| 20 | + title: .ContextMenuOpenInNewTab, |
| 21 | + image: UIImage.templateImageNamed(StandardImageIdentifiers.Large.plus), |
| 22 | + identifier: UIAction.Identifier(rawValue: "linkContextMenu.openInNewTab") |
| 23 | + ) { _ in |
| 24 | + addTab(url, false, currentTab) |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + func addOpenInNewPrivateTab(url: URL, currentTab: Tab, addTab: @escaping (URL, Bool, Tab) -> Void) { |
| 29 | + actions.append( |
| 30 | + UIAction( |
| 31 | + title: .ContextMenuOpenInNewPrivateTab, |
| 32 | + image: UIImage.templateImageNamed(StandardImageIdentifiers.Large.privateMode), |
| 33 | + identifier: UIAction.Identifier("linkContextMenu.openInNewPrivateTab") |
| 34 | + ) { _ in |
| 35 | + addTab(url, true, currentTab) |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + func addBookmarkLink(url: URL, title: String?, addBookmark: @escaping (String, String?) -> Void) { |
| 40 | + actions.append( |
| 41 | + UIAction( |
| 42 | + title: .ContextMenuBookmarkLink, |
| 43 | + image: UIImage.templateImageNamed(StandardImageIdentifiers.Large.bookmark), |
| 44 | + identifier: UIAction.Identifier("linkContextMenu.bookmarkLink") |
| 45 | + ) { _ in |
| 46 | + addBookmark(url.absoluteString, title) |
| 47 | + TelemetryWrapper.recordEvent(category: .action, |
| 48 | + method: .add, |
| 49 | + object: .bookmark, |
| 50 | + value: .contextMenu) |
| 51 | + } |
| 52 | + ) |
| 53 | + } |
| 54 | + |
| 55 | + func addRemoveBookmarkLink(url: URL, title: String?, removeBookmark: @escaping (URL, String?) -> Void) { |
| 56 | + actions.append( |
| 57 | + UIAction( |
| 58 | + title: .RemoveBookmarkContextMenuTitle, |
| 59 | + image: UIImage.templateImageNamed(StandardImageIdentifiers.Large.cross), |
| 60 | + identifier: UIAction.Identifier("linkContextMenu.removeBookmarkLink") |
| 61 | + ) { _ in |
| 62 | + removeBookmark(url, title) |
| 63 | + TelemetryWrapper.recordEvent(category: .action, |
| 64 | + method: .delete, |
| 65 | + object: .bookmark, |
| 66 | + value: .contextMenu) |
| 67 | + } |
| 68 | + ) |
| 69 | + } |
| 70 | + |
| 71 | + func addDownload(url: URL, currentTab: Tab, assignWebView: @escaping (WKWebView?) -> Void) { |
| 72 | + actions.append(UIAction( |
| 73 | + title: .ContextMenuDownloadLink, |
| 74 | + image: UIImage.templateImageNamed( |
| 75 | + StandardImageIdentifiers.Large.download |
| 76 | + ), |
| 77 | + identifier: UIAction.Identifier("linkContextMenu.download") |
| 78 | + ) { _ in |
| 79 | + // This checks if download is a blob, if yes, begin blob download process |
| 80 | + if !DownloadContentScript.requestBlobDownload(url: url, tab: currentTab) { |
| 81 | + // if not a blob, set pendingDownloadWebView and load the request in |
| 82 | + // the webview, which will trigger the WKWebView navigationResponse |
| 83 | + // delegate function and eventually downloadHelper.open() |
| 84 | + assignWebView(currentTab.webView) |
| 85 | + let request = URLRequest(url: url) |
| 86 | + currentTab.webView?.load(request) |
| 87 | + } |
| 88 | + }) |
| 89 | + } |
| 90 | + |
| 91 | + func addCopyLink(url: URL) { |
| 92 | + actions.append(UIAction( |
| 93 | + title: .ContextMenuCopyLink, |
| 94 | + image: UIImage.templateImageNamed(StandardImageIdentifiers.Large.link), |
| 95 | + identifier: UIAction.Identifier("linkContextMenu.copyLink") |
| 96 | + ) { _ in |
| 97 | + UIPasteboard.general.url = url |
| 98 | + }) |
| 99 | + } |
| 100 | + |
| 101 | + func addShare(url: URL, |
| 102 | + tabManager: TabManager, |
| 103 | + webView: WKWebView, |
| 104 | + view: UIView, |
| 105 | + navigationHandler: BrowserNavigationHandler?, |
| 106 | + contentContainer: ContentContainer) { |
| 107 | + actions.append(UIAction( |
| 108 | + title: .ContextMenuShareLink, |
| 109 | + image: UIImage.templateImageNamed(StandardImageIdentifiers.Large.shareApple), |
| 110 | + identifier: UIAction.Identifier("linkContextMenu.share") |
| 111 | + ) { _ in |
| 112 | + guard let tab = tabManager[webView], |
| 113 | + let helper = tab.getContentScript(name: ContextMenuHelper.name()) as? ContextMenuHelper |
| 114 | + else { return } |
| 115 | + |
| 116 | + // This is only used on ipad for positioning the popover. On iPhone it is an action sheet. |
| 117 | + let point = webView.convert(helper.touchPoint, to: view) |
| 118 | + navigationHandler?.showShareExtension( |
| 119 | + url: url, |
| 120 | + sourceView: view, |
| 121 | + sourceRect: CGRect(origin: point, size: CGSize(width: 10.0, height: 10.0)), |
| 122 | + toastContainer: contentContainer, |
| 123 | + popoverArrowDirection: .unknown |
| 124 | + ) |
| 125 | + }) |
| 126 | + } |
| 127 | + |
| 128 | + func addSaveImage(url: URL, |
| 129 | + getImageData: @escaping (URL, @escaping (Data) -> Void) -> Void, |
| 130 | + writeToPhotoAlbum: @escaping (UIImage) -> Void) { |
| 131 | + actions.append(UIAction( |
| 132 | + title: .ContextMenuSaveImage, |
| 133 | + identifier: UIAction.Identifier("linkContextMenu.saveImage") |
| 134 | + ) { _ in |
| 135 | + getImageData(url) { data in |
| 136 | + if url.pathExtension.lowercased() == "gif" { |
| 137 | + PHPhotoLibrary.shared().performChanges { |
| 138 | + let creationRequest = PHAssetCreationRequest.forAsset() |
| 139 | + creationRequest.addResource(with: .photo, data: data, options: nil) |
| 140 | + } |
| 141 | + } else { |
| 142 | + guard let image = UIImage(data: data) else { return } |
| 143 | + writeToPhotoAlbum(image) |
| 144 | + } |
| 145 | + } |
| 146 | + }) |
| 147 | + } |
| 148 | + |
| 149 | + func addCopyImage(url: URL) { |
| 150 | + actions.append(UIAction( |
| 151 | + title: .ContextMenuCopyImage, |
| 152 | + identifier: UIAction.Identifier("linkContextMenu.copyImage") |
| 153 | + ) { _ in |
| 154 | + // put the actual image on the clipboard |
| 155 | + // do this asynchronously just in case we're in a low bandwidth situation |
| 156 | + let pasteboard = UIPasteboard.general |
| 157 | + pasteboard.url = url as URL |
| 158 | + let changeCount = pasteboard.changeCount |
| 159 | + let application = UIApplication.shared |
| 160 | + var taskId = UIBackgroundTaskIdentifier(rawValue: 0) |
| 161 | + taskId = application.beginBackgroundTask(expirationHandler: { |
| 162 | + application.endBackgroundTask(taskId) |
| 163 | + }) |
| 164 | + |
| 165 | + makeURLSession( |
| 166 | + userAgent: UserAgent.fxaUserAgent, |
| 167 | + configuration: URLSessionConfiguration.default |
| 168 | + ).dataTask(with: url) { (data, response, error) in |
| 169 | + guard validatedHTTPResponse(response, statusCode: 200..<300) != nil else { |
| 170 | + application.endBackgroundTask(taskId) |
| 171 | + return |
| 172 | + } |
| 173 | + |
| 174 | + // Only set the image onto the pasteboard if the pasteboard hasn't changed since |
| 175 | + // fetching the image; otherwise, in low-bandwidth situations, |
| 176 | + // we might be overwriting something that the user has subsequently added. |
| 177 | + if changeCount == pasteboard.changeCount, |
| 178 | + let imageData = data, |
| 179 | + error == nil { |
| 180 | + pasteboard.addImageWithData(imageData, forURL: url) |
| 181 | + } |
| 182 | + |
| 183 | + application.endBackgroundTask(taskId) |
| 184 | + }.resume() |
| 185 | + }) |
| 186 | + } |
| 187 | + |
| 188 | + func addCopyImageLink(url: URL) { |
| 189 | + actions.append(UIAction( |
| 190 | + title: .ContextMenuCopyImageLink, |
| 191 | + identifier: UIAction.Identifier("linkContextMenu.copyImageLink") |
| 192 | + ) { _ in |
| 193 | + UIPasteboard.general.url = url as URL |
| 194 | + }) |
| 195 | + } |
| 196 | +} |
0 commit comments