f56875dff3a1cc45ebee617215ce95723f53de4e
[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 "WebProcessProxy.h"
37 #import <WebCore/DragItem.h>
38 #import <WebCore/NotImplemented.h>
39 #import <WebCore/SearchPopupMenuCocoa.h>
40 #import <WebCore/ValidationBubble.h>
41 #import <wtf/BlockPtr.h>
42 #import <wtf/cf/TypeCastsCF.h>
43
44 namespace WebKit {
45 using namespace WebCore;
46
47 #if ENABLE(DATA_DETECTION)
48 void WebPageProxy::setDataDetectionResult(const DataDetectionResult& dataDetectionResult)
49 {
50     m_dataDetectionResults = dataDetectionResult.results;
51 }
52 #endif
53
54 void WebPageProxy::saveRecentSearches(const String& name, const Vector<WebCore::RecentSearch>& searchItems)
55 {
56     if (!name) {
57         // FIXME: This should be a message check.
58         return;
59     }
60
61     WebCore::saveRecentSearches(name, searchItems);
62 }
63
64 void WebPageProxy::loadRecentSearches(const String& name, Vector<WebCore::RecentSearch>& searchItems)
65 {
66     if (!name) {
67         // FIXME: This should be a message check.
68         return;
69     }
70
71     searchItems = WebCore::loadRecentSearches(name);
72 }
73
74 void WebPageProxy::beginSafeBrowsingCheck(const URL& url, WebFramePolicyListenerProxy& listener)
75 {
76 #if HAVE(SAFE_BROWSING)
77     SSBLookupContext *context = [SSBLookupContext sharedLookupContext];
78     if (!context)
79         return listener.didReceiveSafeBrowsingResults({ });
80     [context lookUpURL:url completionHandler:BlockPtr<void(SSBLookupResult *, NSError *)>::fromCallable([listener = makeRef(listener), url = url] (SSBLookupResult *result, NSError *error) mutable {
81         RunLoop::main().dispatch([listener = WTFMove(listener), result = retainPtr(result), error = retainPtr(error), url = WTFMove(url)] {
82             if (error) {
83                 listener->didReceiveSafeBrowsingResults({ });
84                 return;
85             }
86
87             for (SSBServiceLookupResult *lookupResult in [result serviceLookupResults]) {
88                 if (lookupResult.isPhishing || lookupResult.isMalware || lookupResult.isUnwantedSoftware) {
89                     listener->didReceiveSafeBrowsingResults(SafeBrowsingWarning::create(url, lookupResult));
90                     return;
91                 }
92             }
93             listener->didReceiveSafeBrowsingResults({ });
94         });
95     }).get()];
96 #else
97     listener.didReceiveSafeBrowsingResults({ });
98 #endif
99 }
100
101 #if ENABLE(CONTENT_FILTERING)
102 void WebPageProxy::contentFilterDidBlockLoadForFrame(const WebCore::ContentFilterUnblockHandler& unblockHandler, uint64_t frameID)
103 {
104     if (WebFrameProxy* frame = m_process->webFrame(frameID))
105         frame->contentFilterDidBlockLoad(unblockHandler);
106 }
107 #endif
108
109 void WebPageProxy::addPlatformLoadParameters(LoadParameters& loadParameters)
110 {
111     loadParameters.dataDetectionContext = m_uiClient->dataDetectionContext();
112 }
113
114 void WebPageProxy::createSandboxExtensionsIfNeeded(const Vector<String>& files, SandboxExtension::Handle& fileReadHandle, SandboxExtension::HandleArray& fileUploadHandles)
115 {
116     if (!files.size())
117         return;
118
119     if (files.size() == 1) {
120         BOOL isDirectory;
121         if ([[NSFileManager defaultManager] fileExistsAtPath:files[0] isDirectory:&isDirectory] && !isDirectory) {
122             SandboxExtension::createHandle("/", SandboxExtension::Type::ReadOnly, fileReadHandle);
123             process().willAcquireUniversalFileReadSandboxExtension();
124         }
125     }
126
127     fileUploadHandles.allocate(files.size());
128     for (size_t i = 0; i< files.size(); i++) {
129         NSString *file = files[i];
130         if (![[NSFileManager defaultManager] fileExistsAtPath:file])
131             continue;
132         SandboxExtension::createHandle(file, SandboxExtension::Type::ReadOnly, fileUploadHandles[i]);
133     }
134 }
135
136 #if ENABLE(DRAG_SUPPORT)
137
138 void WebPageProxy::startDrag(const DragItem& dragItem, const ShareableBitmap::Handle& dragImageHandle)
139 {
140     pageClient().startDrag(dragItem, dragImageHandle);
141 }
142
143 #if PLATFORM(IOS_FAMILY)
144
145 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)
146 {
147     notImplemented();
148 }
149
150 void WebPageProxy::setDragCaretRect(const IntRect& dragCaretRect)
151 {
152     if (m_currentDragCaretRect == dragCaretRect)
153         return;
154
155     auto previousRect = m_currentDragCaretRect;
156     m_currentDragCaretRect = dragCaretRect;
157     pageClient().didChangeDataInteractionCaretRect(previousRect, dragCaretRect);
158 }
159
160 #endif // PLATFORM(IOS_FAMILY)
161
162 #endif // ENABLE(DRAG_SUPPORT)
163
164 #if ENABLE(ATTACHMENT_ELEMENT)
165
166 void WebPageProxy::platformRegisterAttachment(Ref<API::Attachment>&& attachment, const String& preferredFileName, const IPC::DataReference& dataReference)
167 {
168     if (dataReference.isEmpty())
169         return;
170
171     auto buffer = SharedBuffer::create(dataReference.data(), dataReference.size());
172     auto fileWrapper = adoptNS([pageClient().allocFileWrapperInstance() initRegularFileWithContents:buffer->createNSData().autorelease()]);
173     [fileWrapper setPreferredFilename:preferredFileName];
174     attachment->setFileWrapper(fileWrapper.get());
175 }
176
177 void WebPageProxy::platformRegisterAttachment(Ref<API::Attachment>&& attachment, const String& filePath)
178 {
179     if (!filePath)
180         return;
181
182     auto fileWrapper = adoptNS([pageClient().allocFileWrapperInstance() initWithURL:[NSURL fileURLWithPath:filePath] options:0 error:nil]);
183     attachment->setFileWrapper(fileWrapper.get());
184 }
185
186 void WebPageProxy::platformCloneAttachment(Ref<API::Attachment>&& fromAttachment, Ref<API::Attachment>&& toAttachment)
187 {
188     toAttachment->setFileWrapper(fromAttachment->fileWrapper());
189 }
190
191 #endif // ENABLE(ATTACHMENT_ELEMENT)
192
193 }