[WebEditingTester] Add a custom protocol for loading canned content
authortimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Feb 2015 20:02:39 +0000 (20:02 +0000)
committertimothy_horton@apple.com <timothy_horton@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 20 Feb 2015 20:02:39 +0000 (20:02 +0000)
https://bugs.webkit.org/show_bug.cgi?id=141830

Reviewed by Enrica Casucci.

Add a canned:// protocol handler, which can be used to emulate behaviors
in WebKit client apps (like Mail) that use custom protocol handlers,
which can have important effects on editing (mostly, the URLs are only
valid inside of the client application, but not when copied/dragged out).

For now, we only support two canned files:

canned://dice is a PNG of dice (stolen from LayoutTests/)
canned://text is the string "canned text" served as text/html

In the future we could support things like very large (generated) files,
progressive loading, etc. to further simulate conditions in other apps.

* WebEditingTester/AppDelegate.m:
(-[WebEditingAppDelegate applicationDidFinishLaunching:]):
Register the protocol with CFNetwork, and the scheme with WebKit2.

* WebEditingTester/Canned Content/dice.png: Added.
* WebEditingTester/CannedContent.h: Added.
* WebEditingTester/CannedContent.m: Added.
(-[CannedContent initWithRequest:cachedResponse:client:]):
(+[CannedContent canonicalRequestForRequest:]):
(+[CannedContent canInitWithRequest:]):
We only want to handle canned:// URLs.

(-[CannedContent startLoading]):
For now, immediately load the two files we know about.
Otherwise, immediately fail the load.

(-[CannedContent stopLoading]):
* WebEditingTester/WebEditingTester.xcodeproj/project.pbxproj:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@180439 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Tools/ChangeLog
Tools/WebEditingTester/AppDelegate.m
Tools/WebEditingTester/Canned Content/dice.png [new file with mode: 0644]
Tools/WebEditingTester/CannedContent.h [new file with mode: 0644]
Tools/WebEditingTester/CannedContent.m [new file with mode: 0644]
Tools/WebEditingTester/WebEditingTester.xcodeproj/project.pbxproj

index 546fd43..98b4a73 100644 (file)
@@ -1,5 +1,44 @@
 2015-02-20  Timothy Horton  <timothy_horton@apple.com>
 
+        [WebEditingTester] Add a custom protocol for loading canned content
+        https://bugs.webkit.org/show_bug.cgi?id=141830
+
+        Reviewed by Enrica Casucci.
+
+        Add a canned:// protocol handler, which can be used to emulate behaviors
+        in WebKit client apps (like Mail) that use custom protocol handlers,
+        which can have important effects on editing (mostly, the URLs are only
+        valid inside of the client application, but not when copied/dragged out).
+
+        For now, we only support two canned files:
+
+            canned://dice is a PNG of dice (stolen from LayoutTests/)
+            canned://text is the string "canned text" served as text/html
+
+        In the future we could support things like very large (generated) files,
+        progressive loading, etc. to further simulate conditions in other apps.
+
+        * WebEditingTester/AppDelegate.m:
+        (-[WebEditingAppDelegate applicationDidFinishLaunching:]):
+        Register the protocol with CFNetwork, and the scheme with WebKit2.
+
+        * WebEditingTester/Canned Content/dice.png: Added.
+        * WebEditingTester/CannedContent.h: Added.
+        * WebEditingTester/CannedContent.m: Added.
+        (-[CannedContent initWithRequest:cachedResponse:client:]):
+        (+[CannedContent canonicalRequestForRequest:]):
+        (+[CannedContent canInitWithRequest:]):
+        We only want to handle canned:// URLs.
+
+        (-[CannedContent startLoading]):
+        For now, immediately load the two files we know about.
+        Otherwise, immediately fail the load.
+
+        (-[CannedContent stopLoading]):
+        * WebEditingTester/WebEditingTester.xcodeproj/project.pbxproj:
+
+2015-02-20  Timothy Horton  <timothy_horton@apple.com>
+
         [WebEditingTester] Implement an open panel
         https://bugs.webkit.org/show_bug.cgi?id=141829
 
index bc75bcf..c64c1a5 100644 (file)
 
 #import "AppDelegate.h"
 
+#import "CannedContent.h"
 #import "EditingOperations.h"
 #import "WK1WebDocumentController.h"
 #import "WK2WebDocumentController.h"
+#import <WebKit/WKBrowsingContextController.h>
 
 static NSString * const UseWebKit2ByDefaultPreferenceKey = @"UseWebKit2ByDefault";
 
@@ -46,6 +48,9 @@ static NSString * const UseWebKit2ByDefaultPreferenceKey = @"UseWebKit2ByDefault
 
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
 {
+    [NSURLProtocol registerClass:[CannedContent class]];
+    [WKBrowsingContextController registerSchemeForCustomProtocol:@"canned"];
+
     [self newEditor:self];
     [self _updateNewWindowKeyEquivalents];
 }
diff --git a/Tools/WebEditingTester/Canned Content/dice.png b/Tools/WebEditingTester/Canned Content/dice.png
new file mode 100644 (file)
index 0000000..c82d015
Binary files /dev/null and b/Tools/WebEditingTester/Canned Content/dice.png differ
diff --git a/Tools/WebEditingTester/CannedContent.h b/Tools/WebEditingTester/CannedContent.h
new file mode 100644 (file)
index 0000000..16fe88d
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Foundation/Foundation.h>
+
+@interface CannedContent : NSURLProtocol
+
+@end
diff --git a/Tools/WebEditingTester/CannedContent.m b/Tools/WebEditingTester/CannedContent.m
new file mode 100644 (file)
index 0000000..b82d4ba
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright (C) 2015 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "CannedContent.h"
+
+@implementation CannedContent
+
+- (instancetype)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client
+{
+    if (!(self = [super initWithRequest:request cachedResponse:cachedResponse client:client]))
+        return nil;
+
+    return self;
+}
+
++ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
+{
+    return request;
+}
+
++ (BOOL)canInitWithRequest:(NSURLRequest *)request
+{
+    return [request.URL.scheme isEqualToString:@"canned"];
+}
+
+- (void)startLoading
+{
+    NSData *data = nil;
+    NSString *MIMEType = nil;
+
+    if ([self.request.URL.host isEqualToString:@"dice"]) {
+        data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dice" ofType:@"png"]];
+        MIMEType = @"image/png";
+    } else if ([self.request.URL.host isEqualToString:@"text"]) {
+        data = [@"canned text" dataUsingEncoding:NSUTF8StringEncoding];
+        MIMEType = @"text/html";
+    }
+
+    if (!data) {
+        [self.client URLProtocol:self didFailWithError:[NSError errorWithDomain:@"WebEditingTester" code:1 userInfo:nil]];
+        return;
+    }
+
+    [self.client URLProtocol:self didReceiveResponse:[[NSURLResponse alloc] initWithURL:self.request.URL MIMEType:MIMEType expectedContentLength:data.length textEncodingName:nil] cacheStoragePolicy:NSURLCacheStorageNotAllowed];
+    [self.client URLProtocol:self didLoadData:data];
+    [self.client URLProtocolDidFinishLoading:self];
+}
+
+- (void)stopLoading
+{
+
+}
+
+@end
index 08b3adf..d801a42 100644 (file)
@@ -8,6 +8,8 @@
 
 /* Begin PBXBuildFile section */
                2D4FFA111A97079D0006360B /* EditingOperations.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D4FFA101A97079D0006360B /* EditingOperations.m */; };
+               2D6B05BF1A972E8C00613256 /* CannedContent.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D6B05BE1A972E8C00613256 /* CannedContent.m */; };
+               2D6B05C21A97365300613256 /* dice.png in Resources */ = {isa = PBXBuildFile; fileRef = 2D6B05C11A97365300613256 /* dice.png */; };
                C53E7D0D1A9546E600818F19 /* MainMenu.xib in Resources */ = {isa = PBXBuildFile; fileRef = C53E7D0B1A9546E600818F19 /* MainMenu.xib */; };
                C53E7D0E1A9546E600818F19 /* WebDocument.xib in Resources */ = {isa = PBXBuildFile; fileRef = C53E7D0C1A9546E600818F19 /* WebDocument.xib */; };
                C558CDB31A92D6900059A907 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = C558CDB21A92D6900059A907 /* AppDelegate.m */; };
@@ -22,6 +24,9 @@
 /* Begin PBXFileReference section */
                2D4FFA0F1A97079D0006360B /* EditingOperations.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EditingOperations.h; sourceTree = "<group>"; };
                2D4FFA101A97079D0006360B /* EditingOperations.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EditingOperations.m; sourceTree = "<group>"; };
+               2D6B05BD1A972E8C00613256 /* CannedContent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CannedContent.h; sourceTree = "<group>"; };
+               2D6B05BE1A972E8C00613256 /* CannedContent.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CannedContent.m; sourceTree = "<group>"; };
+               2D6B05C11A97365300613256 /* dice.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = dice.png; sourceTree = "<group>"; };
                C53E7D0B1A9546E600818F19 /* MainMenu.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = MainMenu.xib; sourceTree = "<group>"; };
                C53E7D0C1A9546E600818F19 /* WebDocument.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = WebDocument.xib; sourceTree = "<group>"; };
                C558CDAC1A92D6900059A907 /* WebEditingTester.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = WebEditingTester.app; sourceTree = BUILT_PRODUCTS_DIR; };
 /* End PBXFrameworksBuildPhase section */
 
 /* Begin PBXGroup section */
+               2D6B05C01A9734D700613256 /* Canned Content */ = {
+                       isa = PBXGroup;
+                       children = (
+                               2D6B05C11A97365300613256 /* dice.png */,
+                       );
+                       path = "Canned Content";
+                       sourceTree = "<group>";
+               };
                C558CDA31A92D6900059A907 = {
                        isa = PBXGroup;
                        children = (
                                C558CDAE1A92D6900059A907 /* WebEditingTester */,
                                C59F9B9A1A93C9030041A959 /* Resources */,
+                               2D6B05C01A9734D700613256 /* Canned Content */,
                                C558CDAF1A92D6900059A907 /* Supporting Files */,
                                C558CDD51A93C8830059A907 /* Frameworks */,
                                C558CDAD1A92D6900059A907 /* Products */,
@@ -84,6 +98,8 @@
                                C558CDB71A92D6900059A907 /* WebDocumentController.m */,
                                2D4FFA0F1A97079D0006360B /* EditingOperations.h */,
                                2D4FFA101A97079D0006360B /* EditingOperations.m */,
+                               2D6B05BD1A972E8C00613256 /* CannedContent.h */,
+                               2D6B05BE1A972E8C00613256 /* CannedContent.m */,
                        );
                        name = WebEditingTester;
                        sourceTree = "<group>";
                        files = (
                                C53E7D0E1A9546E600818F19 /* WebDocument.xib in Resources */,
                                C53E7D0D1A9546E600818F19 /* MainMenu.xib in Resources */,
+                               2D6B05C21A97365300613256 /* dice.png in Resources */,
                        );
                        runOnlyForDeploymentPostprocessing = 0;
                };
                        isa = PBXSourcesBuildPhase;
                        buildActionMask = 2147483647;
                        files = (
+                               2D6B05BF1A972E8C00613256 /* CannedContent.m in Sources */,
                                C558CDB81A92D6900059A907 /* WebDocumentController.m in Sources */,
                                C59F9BA01A93CB630041A959 /* WK2WebDocumentController.m in Sources */,
                                2D4FFA111A97079D0006360B /* EditingOperations.m in Sources */,