Move Lookup Code for better cross platform usage
[WebKit-https.git] / Source / WebKit / UIProcess / Cocoa / WebPageProxyCocoa.mm
1 /*
2  * Copyright (C) 2014 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "WebPageProxy.h"
28
29 #import "APIAttachment.h"
30 #import "APIUIClient.h"
31 #import "DataDetectionResult.h"
32 #import "LoadParameters.h"
33 #import "PageClient.h"
34 #import "SafeBrowsingSPI.h"
35 #import "SafeBrowsingWarning.h"
36 #import "WebPageMessages.h"
37 #import "WebProcessProxy.h"
38 #import <WebCore/DragItem.h>
39 #import <WebCore/NotImplemented.h>
40 #import <WebCore/SearchPopupMenuCocoa.h>
41 #import <WebCore/ValidationBubble.h>
42 #import <wtf/BlockPtr.h>
43 #import <wtf/cf/TypeCastsCF.h>
44
45 namespace WebKit {
46 using namespace WebCore;
47
48 #if ENABLE(DATA_DETECTION)
49 void WebPageProxy::setDataDetectionResult(const DataDetectionResult& dataDetectionResult)
50 {
51     m_dataDetectionResults = dataDetectionResult.results;
52 }
53 #endif
54
55 void WebPageProxy::saveRecentSearches(const String& name, const Vector<WebCore::RecentSearch>& searchItems)
56 {
57     if (!name) {
58         // FIXME: This should be a message check.
59         return;
60     }
61
62     WebCore::saveRecentSearches(name, searchItems);
63 }
64
65 void WebPageProxy::loadRecentSearches(const String& name, Vector<WebCore::RecentSearch>& searchItems)
66 {
67     if (!name) {
68         // FIXME: This should be a message check.
69         return;
70     }
71
72     searchItems = WebCore::loadRecentSearches(name);
73 }
74
75 void WebPageProxy::beginSafeBrowsingCheck(const URL& url, WebFramePolicyListenerProxy& listener)
76 {
77 #if HAVE(SAFE_BROWSING)
78     SSBLookupContext *context = [SSBLookupContext sharedLookupContext];
79     if (!context)
80         return listener.didReceiveSafeBrowsingResults({ });
81     [context lookUpURL:url completionHandler:BlockPtr<void(SSBLookupResult *, NSError *)>::fromCallable([listener = makeRef(listener), url = url] (SSBLookupResult *result, NSError *error) mutable {
82         RunLoop::main().dispatch([listener = WTFMove(listener), result = retainPtr(result), error = retainPtr(error), url = WTFMove(url)] {
83             if (error) {
84                 listener->didReceiveSafeBrowsingResults({ });
85                 return;
86             }
87
88             for (SSBServiceLookupResult *lookupResult in [result serviceLookupResults]) {
89                 if (lookupResult.isPhishing || lookupResult.isMalware || lookupResult.isUnwantedSoftware) {
90                     listener->didReceiveSafeBrowsingResults(SafeBrowsingWarning::create(url, lookupResult));
91                     return;
92                 }
93             }
94             listener->didReceiveSafeBrowsingResults({ });
95         });
96     }).get()];
97 #else
98     listener.didReceiveSafeBrowsingResults({ });
99 #endif
100 }
101
102 #if ENABLE(CONTENT_FILTERING)
103 void WebPageProxy::contentFilterDidBlockLoadForFrame(const WebCore::ContentFilterUnblockHandler& unblockHandler, uint64_t frameID)
104 {
105     if (WebFrameProxy* frame = m_process->webFrame(frameID))
106         frame->contentFilterDidBlockLoad(unblockHandler);
107 }
108 #endif
109
110 void WebPageProxy::addPlatformLoadParameters(LoadParameters& loadParameters)
111 {
112     loadParameters.dataDetectionContext = m_uiClient->dataDetectionContext();
113 }
114
115 void WebPageProxy::createSandboxExtensionsIfNeeded(const Vector<String>& files, SandboxExtension::Handle& fileReadHandle, SandboxExtension::HandleArray& fileUploadHandles)
116 {
117     if (!files.size())
118         return;
119
120     if (files.size() == 1) {
121         BOOL isDirectory;
122         if ([[NSFileManager defaultManager] fileExistsAtPath:files[0] isDirectory:&isDirectory] && !isDirectory) {
123             SandboxExtension::createHandle("/", SandboxExtension::Type::ReadOnly, fileReadHandle);
124             process().willAcquireUniversalFileReadSandboxExtension();
125         }
126     }
127
128     fileUploadHandles.allocate(files.size());
129     for (size_t i = 0; i< files.size(); i++) {
130         NSString *file = files[i];
131         if (![[NSFileManager defaultManager] fileExistsAtPath:file])
132             continue;
133         SandboxExtension::createHandle(file, SandboxExtension::Type::ReadOnly, fileUploadHandles[i]);
134     }
135 }
136
137 #if ENABLE(DRAG_SUPPORT)
138
139 void WebPageProxy::startDrag(const DragItem& dragItem, const ShareableBitmap::Handle& dragImageHandle)
140 {
141     pageClient().startDrag(dragItem, dragImageHandle);
142 }
143
144 #if PLATFORM(IOS_FAMILY)
145
146 void WebPageProxy::setPromisedDataForImage(const String&, const SharedMemory::Handle&, uint64_t, const String&, const String&, const String&, const String&, const String&, const SharedMemory::Handle&, uint64_t)
147 {
148     notImplemented();
149 }
150
151 void WebPageProxy::setDragCaretRect(const IntRect& dragCaretRect)
152 {
153     if (m_currentDragCaretRect == dragCaretRect)
154         return;
155
156     auto previousRect = m_currentDragCaretRect;
157     m_currentDragCaretRect = dragCaretRect;
158     pageClient().didChangeDataInteractionCaretRect(previousRect, dragCaretRect);
159 }
160
161 #endif // PLATFORM(IOS_FAMILY)
162
163 #endif // ENABLE(DRAG_SUPPORT)
164
165 #if ENABLE(ATTACHMENT_ELEMENT)
166
167 void WebPageProxy::platformRegisterAttachment(Ref<API::Attachment>&& attachment, const String& preferredFileName, const IPC::DataReference& dataReference)
168 {
169     if (dataReference.isEmpty())
170         return;
171
172     auto buffer = SharedBuffer::create(dataReference.data(), dataReference.size());
173     auto fileWrapper = adoptNS([pageClient().allocFileWrapperInstance() initRegularFileWithContents:buffer->createNSData().autorelease()]);
174     [fileWrapper setPreferredFilename:preferredFileName];
175     attachment->setFileWrapper(fileWrapper.get());
176 }
177
178 void WebPageProxy::platformRegisterAttachment(Ref<API::Attachment>&& attachment, const String& filePath)
179 {
180     if (!filePath)
181         return;
182
183     auto fileWrapper = adoptNS([pageClient().allocFileWrapperInstance() initWithURL:[NSURL fileURLWithPath:filePath] options:0 error:nil]);
184     attachment->setFileWrapper(fileWrapper.get());
185 }
186
187 void WebPageProxy::platformCloneAttachment(Ref<API::Attachment>&& fromAttachment, Ref<API::Attachment>&& toAttachment)
188 {
189     toAttachment->setFileWrapper(fromAttachment->fileWrapper());
190 }
191
192 #endif // ENABLE(ATTACHMENT_ELEMENT)
193     
194 void WebPageProxy::performDictionaryLookupAtLocation(const WebCore::FloatPoint& point)
195 {
196     if (!isValid())
197         return;
198     
199     process().send(Messages::WebPage::PerformDictionaryLookupAtLocation(point), m_pageID);
200 }
201
202 void WebPageProxy::performDictionaryLookupOfCurrentSelection()
203 {
204     if (!isValid())
205         return;
206     
207     process().send(Messages::WebPage::PerformDictionaryLookupOfCurrentSelection(), m_pageID);
208 }
209
210 } // namespace WebKit