Refactoring related to Safe Browsing
[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 "SafeBrowsingResult.h"
35 #import "SafeBrowsingSPI.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             NSArray<SSBServiceLookupResult *> *results = [result serviceLookupResults];
88             Vector<Ref<SafeBrowsingResult>> resultsVector;
89             resultsVector.reserveInitialCapacity([results count]);
90             for (SSBServiceLookupResult *result in results)
91                 resultsVector.uncheckedAppend(SafeBrowsingResult::create(URL(url), result));
92             listener->didReceiveSafeBrowsingResults(WTFMove(resultsVector));
93         });
94     }).get()];
95 #else
96     listener.didReceiveSafeBrowsingResults({ });
97 #endif
98 }
99
100 #if ENABLE(CONTENT_FILTERING)
101 void WebPageProxy::contentFilterDidBlockLoadForFrame(const WebCore::ContentFilterUnblockHandler& unblockHandler, uint64_t frameID)
102 {
103     if (WebFrameProxy* frame = m_process->webFrame(frameID))
104         frame->contentFilterDidBlockLoad(unblockHandler);
105 }
106 #endif
107
108 void WebPageProxy::addPlatformLoadParameters(LoadParameters& loadParameters)
109 {
110     loadParameters.dataDetectionContext = m_uiClient->dataDetectionContext();
111 }
112
113 void WebPageProxy::createSandboxExtensionsIfNeeded(const Vector<String>& files, SandboxExtension::Handle& fileReadHandle, SandboxExtension::HandleArray& fileUploadHandles)
114 {
115     if (!files.size())
116         return;
117
118     if (files.size() == 1) {
119         BOOL isDirectory;
120         if ([[NSFileManager defaultManager] fileExistsAtPath:files[0] isDirectory:&isDirectory] && !isDirectory) {
121             SandboxExtension::createHandle("/", SandboxExtension::Type::ReadOnly, fileReadHandle);
122             process().willAcquireUniversalFileReadSandboxExtension();
123         }
124     }
125
126     fileUploadHandles.allocate(files.size());
127     for (size_t i = 0; i< files.size(); i++) {
128         NSString *file = files[i];
129         if (![[NSFileManager defaultManager] fileExistsAtPath:file])
130             continue;
131         SandboxExtension::createHandle(file, SandboxExtension::Type::ReadOnly, fileUploadHandles[i]);
132     }
133 }
134
135 #if ENABLE(DRAG_SUPPORT)
136
137 void WebPageProxy::startDrag(const DragItem& dragItem, const ShareableBitmap::Handle& dragImageHandle)
138 {
139     pageClient().startDrag(dragItem, dragImageHandle);
140 }
141
142 #if PLATFORM(IOS)
143
144 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)
145 {
146     notImplemented();
147 }
148
149 void WebPageProxy::setDragCaretRect(const IntRect& dragCaretRect)
150 {
151     if (m_currentDragCaretRect == dragCaretRect)
152         return;
153
154     auto previousRect = m_currentDragCaretRect;
155     m_currentDragCaretRect = dragCaretRect;
156     pageClient().didChangeDataInteractionCaretRect(previousRect, dragCaretRect);
157 }
158
159 #endif // PLATFORM(IOS)
160
161 #endif // ENABLE(DRAG_SUPPORT)
162
163 #if ENABLE(ATTACHMENT_ELEMENT)
164
165 void WebPageProxy::platformRegisterAttachment(Ref<API::Attachment>&& attachment, const String& preferredFileName, const IPC::DataReference& dataReference)
166 {
167     auto buffer = SharedBuffer::create(dataReference.data(), dataReference.size());
168     auto fileWrapper = adoptNS([[NSFileWrapper alloc] initRegularFileWithContents:buffer->createNSData().autorelease()]);
169     [fileWrapper setPreferredFilename:preferredFileName];
170     attachment->setFileWrapper(fileWrapper.get());
171 }
172
173 void WebPageProxy::platformRegisterAttachment(Ref<API::Attachment>&& attachment, const String& filePath)
174 {
175     auto fileWrapper = adoptNS([[NSFileWrapper alloc] initWithURL:[NSURL fileURLWithPath:filePath] options:0 error:nil]);
176     attachment->setFileWrapper(fileWrapper.get());
177 }
178
179 void WebPageProxy::platformCloneAttachment(Ref<API::Attachment>&& fromAttachment, Ref<API::Attachment>&& toAttachment)
180 {
181     toAttachment->setFileWrapper(fromAttachment->fileWrapper());
182 }
183
184 #endif // ENABLE(ATTACHMENT_ELEMENT)
185
186 }