⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 274697 in webkit


Ignore:
Timestamp:
Mar 18, 2021, 7:37:52 PM (6 years ago)
Author:
BJ Burg
Message:

[Cocoa] Web Inspector: load inspector resources using a custom scheme handler
​https://bugs.webkit.org/show_bug.cgi?id=179904
<rdar://problem/10887211>

Reviewed by Geoff Garen.

Load WebInspectorUI resources via the inspector-resource:// scheme.
Moving off of ​file:// URLs will allow for a tighter sandbox and
will fix the longstanding issue that Web Inspector's settings are
part of the ​file:// scheme and cleared when Safari clears website data.

This also has the effect of resetting Web Inspector settings since
they are no longer associated with ​file:///.

Loading via this scheme is exercised by existing inspector tests.

  • SourcesCocoa.txt: Added new file.
  • WebKit.xcodeproj/project.pbxproj:
  • UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.h: Copied from Source/WebKit/WebProcess/Inspector/mac/WebInspectorUIMac.mm.
  • UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm: Added.

(-[WKInspectorResourceURLSchemeHandler webView:startURLSchemeTask:]):
Use a global concurrent queue and NSOperation / NSOperationQueue to support
reading files off the main queue.

(-[WKInspectorResourceURLSchemeHandler webView:stopURLSchemeTask:]):
Translate the request URL to a bundle resource URL, read the data, and send a response
with the file data. Return an error if the file could not be found or read.

  • UIProcess/Inspector/mac/WKInspectorViewController.h:
  • UIProcess/Inspector/mac/WKInspectorViewController.mm:

(-[WKInspectorViewController webViewConfiguration]):
(+[WKInspectorViewController URLForInspectorResource:]):
(-[WKInspectorViewController webView:decidePolicyForNavigationAction:decisionHandler:]):
Allow navigations to URIs with the inspector-resource:// scheme.

  • UIProcess/Inspector/mac/WebInspectorProxyMac.mm:

(WebKit::WebInspectorProxy::inspectorPageURL):
(WebKit::WebInspectorProxy::inspectorTestPageURL):
(WebKit::WebInspectorProxy::inspectorBaseURL):
Updated.

  • WebProcess/Inspector/mac/WebInspectorUIMac.mm:

(WebKit::webInspectorUILocalizedStringsURL):
Updated.

Location:
trunk/Source/WebKit
Files:
1 added
7 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r274692 r274697  
     12021-03-18  BJ Burg  <bburg@apple.com>
     2
     3        [Cocoa] Web Inspector: load inspector resources using a custom scheme handler
     4        https://bugs.webkit.org/show_bug.cgi?id=179904
     5        <rdar://problem/10887211>
     6
     7        Reviewed by Geoff Garen.
     8
     9        Load WebInspectorUI resources via the inspector-resource:// scheme.
     10        Moving off of file:// URLs will allow for a tighter sandbox and
     11        will fix the longstanding issue that Web Inspector's settings are
     12        part of the file:// scheme and cleared when Safari clears website data.
     13
     14        This also has the effect of resetting Web Inspector settings since
     15        they are no longer associated with file:///.
     16
     17        Loading via this scheme is exercised by existing inspector tests.
     18
     19        * SourcesCocoa.txt: Added new file.
     20        * WebKit.xcodeproj/project.pbxproj:
     21        * UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.h: Copied from Source/WebKit/WebProcess/Inspector/mac/WebInspectorUIMac.mm.
     22        * UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm: Added.
     23        (-[WKInspectorResourceURLSchemeHandler webView:startURLSchemeTask:]):
     24        Use a global concurrent queue and NSOperation / NSOperationQueue to support
     25        reading files off the main queue.
     26
     27        (-[WKInspectorResourceURLSchemeHandler webView:stopURLSchemeTask:]):
     28        Translate the request URL to a bundle resource URL, read the data, and send a response
     29        with the file data. Return an error if the file could not be found or read.
     30
     31        * UIProcess/Inspector/mac/WKInspectorViewController.h:
     32        * UIProcess/Inspector/mac/WKInspectorViewController.mm:
     33        (-[WKInspectorViewController webViewConfiguration]):
     34        (+[WKInspectorViewController URLForInspectorResource:]):
     35        (-[WKInspectorViewController webView:decidePolicyForNavigationAction:decisionHandler:]):
     36        Allow navigations to URIs with the inspector-resource:// scheme.
     37
     38        * UIProcess/Inspector/mac/WebInspectorProxyMac.mm:
     39        (WebKit::WebInspectorProxy::inspectorPageURL):
     40        (WebKit::WebInspectorProxy::inspectorTestPageURL):
     41        (WebKit::WebInspectorProxy::inspectorBaseURL):
     42        Updated.
     43
     44        * WebProcess/Inspector/mac/WebInspectorUIMac.mm:
     45        (WebKit::webInspectorUILocalizedStringsURL):
     46        Updated.
     47
    1482021-03-18  Sihui Liu  <sihui_liu@appe.com>
    249
  • trunk/Source/WebKit/SourcesCocoa.txt

    r274462 r274697  
    430430UIProcess/Inspector/mac/RemoteWebInspectorProxyMac.mm
    431431UIProcess/Inspector/mac/WebInspectorProxyMac.mm
     432UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.mm
    432433UIProcess/Inspector/mac/WKInspectorViewController.mm
    433434UIProcess/Inspector/mac/WKInspectorWKWebView.mm
  • trunk/Source/WebKit/UIProcess/Inspector/mac/WKInspectorResourceURLSchemeHandler.h

    r274696 r274697  
    11/*
    2  * Copyright (C) 2010-2019 Apple Inc. All rights reserved.
     2 * Copyright (C) 2021 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
    … …  
    2424 */
    2525
    26 #import "config.h"
    27 #import "WebInspectorUI.h"
    28 #import "RemoteWebInspectorUI.h"
    29 
    3026#if PLATFORM(MAC)
    3127
    32 namespace WebKit {
     28#import <WebKit/WKFoundation.h>
     29#import <WebKit/WKURLSchemeHandler.h>
    3330
    34 bool WebInspectorUI::canSave()
    35 {
    36     return true;
    37 }
     31@protocol WKInspectorViewControllerDelegate;
    3832
    39 static String webInspectorUILocalizedStringsURL()
    40 {
    41     NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebInspectorUI"];
    42     if (!bundle)
    43         return String();
     33NS_ASSUME_NONNULL_BEGIN
    4434
    45     NSString *path = [bundle pathForResource:@"localizedStrings" ofType:@"js"];
    46     if (!path)
    47         return String();
    48    
    49     return [NSURL fileURLWithPath:path isDirectory:NO].absoluteString;
    50 }
     35@interface WKInspectorResourceURLSchemeHandler : NSObject <WKURLSchemeHandler>
     36@end
    5137
    52 String WebInspectorUI::localizedStringsURL() const
    53 {
    54     return webInspectorUILocalizedStringsURL();
    55 }
    56 
    57 String RemoteWebInspectorUI::localizedStringsURL() const
    58 {
    59     return webInspectorUILocalizedStringsURL();
    60 }
    61 
    62 } // namespace WebKit
     38NS_ASSUME_NONNULL_END
    6339
    6440#endif // PLATFORM(MAC)
  • trunk/Source/WebKit/UIProcess/Inspector/mac/WKInspectorViewController.h

    r269068 r274697  
    4848
    4949+ (BOOL)viewIsInspectorWebView:(NSView *)view;
     50+ (NSURL * _Nullable)URLForInspectorResource:(NSString *)resource;
    5051
    5152@end
  • trunk/Source/WebKit/UIProcess/Inspector/mac/WKInspectorViewController.mm

    r273109 r274697  
    11/*
    2  * Copyright (C) 2017-2020 Apple Inc. All rights reserved.
     2 * Copyright (C) 2017-2021 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
    … …  
    3131#import "APINavigation.h"
    3232#import "WKFrameInfo.h"
     33#import "WKInspectorResourceURLSchemeHandler.h"
    3334#import "WKInspectorWKWebView.h"
    3435#import "WKNavigationAction.h"
    … …  
    4748#import <wtf/WeakObjCPtr.h>
    4849
     50static NSString * const WKInspectorResourceScheme = @"inspector-resource";
     51
    4952@interface WKInspectorViewController () <WKUIDelegate, WKNavigationDelegate, WKInspectorWKWebViewDelegate>
    5053@end
    … …  
    111114- (WKWebViewConfiguration *)webViewConfiguration
    112115{
    113     auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     116    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     117    RetainPtr<WKInspectorResourceURLSchemeHandler> inspectorSchemeHandler = adoptNS([[WKInspectorResourceURLSchemeHandler alloc] init]);
     118    [configuration setURLSchemeHandler:inspectorSchemeHandler.autorelease() forURLScheme:WKInspectorResourceScheme];
    114119
    115120    WKPreferences *preferences = configuration.get().preferences;
    … …  
    159164{
    160165    return [view isKindOfClass:[WKInspectorWKWebView class]];
     166}
     167
     168+ (NSURL *)URLForInspectorResource:(NSString *)resource
     169{
     170    return [NSURL URLWithString:[NSString stringWithFormat:@"%@:///%@", WKInspectorResourceScheme, resource]].URLByStandardizingPath;
    161171}
    162172
    … …  
    240250
    241251    // Allow loading of the main inspector file.
    242     if (WebKit::WebInspectorProxy::isMainOrTestInspectorPage(navigationAction.request.URL)) {
     252    if ([navigationAction.request.URL.scheme isEqualToString:WKInspectorResourceScheme]) {
    243253        decisionHandler(WKNavigationActionPolicyAllow);
    244254        return;
  • trunk/Source/WebKit/UIProcess/Inspector/mac/WebInspectorProxyMac.mm

    r274106 r274697  
    780780String WebInspectorProxy::inspectorPageURL()
    781781{
    782     NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebInspectorUI"];
    783     if (!bundle)
    784         return String();
    785 
    786     NSString *path = [bundle pathForResource:@"Main" ofType:@"html"];
    787     ASSERT(path && path.length);
    788     if (!path)
    789         return String();
    790 
    791     return [NSURL fileURLWithPath:path isDirectory:NO].absoluteString;
     782    return [WKInspectorViewController URLForInspectorResource:@"Main.html"].absoluteString;
    792783}
    793784
    794785String WebInspectorProxy::inspectorTestPageURL()
    795786{
    796     NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebInspectorUI"];
    797     if (!bundle)
    798         return String();
    799 
    800     // We might not have a Test.html in Production builds.
    801     NSString *path = [bundle pathForResource:@"Test" ofType:@"html"];
    802     if (!path)
    803         return String();
    804 
    805     return [NSURL fileURLWithPath:path isDirectory:NO].absoluteString;
     787    return [WKInspectorViewController URLForInspectorResource:@"Test.html"].absoluteString;
    806788}
    807789
    808790String WebInspectorProxy::inspectorBaseURL()
    809791{
    810     NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebInspectorUI"];
    811     if (!bundle)
    812         return String();
    813 
    814     NSString *path = bundle.resourcePath;
    815     ASSERT(path && path.length);
    816 
    817     return [NSURL fileURLWithPath:path isDirectory:YES].absoluteString;
     792    return [WKInspectorViewController URLForInspectorResource:@""].absoluteString;
    818793}
    819794
  • trunk/Source/WebKit/WebKit.xcodeproj/project.pbxproj

    r274558 r274697  
    15231523                9979CA58237F49F10039EC05 /* _WKInspectorPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 9979CA57237F49F00039EC05 /* _WKInspectorPrivate.h */; settings = {ATTRIBUTES = (Private, ); }; };
    15241524                99996A9F25004BCC004F7559 /* _WKInspectorPrivateForTesting.h in Headers */ = {isa = PBXBuildFile; fileRef = 99996A9D25004BCB004F7559 /* _WKInspectorPrivateForTesting.h */; settings = {ATTRIBUTES = (Private, ); }; };
     1525                99A7ACE526012922006D57FD /* WKInspectorResourceURLSchemeHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 99A7ACE326012919006D57FD /* WKInspectorResourceURLSchemeHandler.h */; };
    15251526                99B16757252BB7E10073140E /* _WKInspectorExtension.h in Headers */ = {isa = PBXBuildFile; fileRef = 99B16754252BB7E00073140E /* _WKInspectorExtension.h */; settings = {ATTRIBUTES = (Private, ); }; };
    15261527                99B16758252BB7E10073140E /* _WKInspectorExtensionInternal.h in Headers */ = {isa = PBXBuildFile; fileRef = 99B16755252BB7E10073140E /* _WKInspectorExtensionInternal.h */; };
    … …  
    48954896                999B7ED82550E4A800F450A4 /* InspectorExtensionTypes.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = InspectorExtensionTypes.cpp; sourceTree = "<group>"; };
    48964897                999B7F4F2554BA3F00F450A4 /* APIInspectorExtension.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = APIInspectorExtension.cpp; sourceTree = "<group>"; };
     4898                99A7ACE326012919006D57FD /* WKInspectorResourceURLSchemeHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WKInspectorResourceURLSchemeHandler.h; sourceTree = "<group>"; };
     4899                99A7ACE42601291A006D57FD /* WKInspectorResourceURLSchemeHandler.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = WKInspectorResourceURLSchemeHandler.mm; sourceTree = "<group>"; };
    48974900                99B16754252BB7E00073140E /* _WKInspectorExtension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _WKInspectorExtension.h; sourceTree = "<group>"; };
    48984901                99B16755252BB7E10073140E /* _WKInspectorExtensionInternal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = _WKInspectorExtensionInternal.h; sourceTree = "<group>"; };
    … …  
    92609263                                A5D3504D1D78F0D2005124A9 /* RemoteWebInspectorProxyMac.mm */,
    92619264                                1CA8B935127C774E00576C2B /* WebInspectorProxyMac.mm */,
     9265                                99A7ACE326012919006D57FD /* WKInspectorResourceURLSchemeHandler.h */,
     9266                                99A7ACE42601291A006D57FD /* WKInspectorResourceURLSchemeHandler.mm */,
    92629267                                994BADF11F7D77EA00B571E7 /* WKInspectorViewController.h */,
    92639268                                994BADF21F7D77EB00B571E7 /* WKInspectorViewController.mm */,
    … …  
    1259112596                                A54293A4195A43DA002782C7 /* WKInspectorNodeSearchGestureRecognizer.h in Headers */,
    1259212597                                6EE849C81368D9390038D481 /* WKInspectorPrivateMac.h in Headers */,
     12598                                99A7ACE526012922006D57FD /* WKInspectorResourceURLSchemeHandler.h in Headers */,
    1259312599                                994BADF41F7D781400B571E7 /* WKInspectorViewController.h in Headers */,
    1259412600                                A518B5D21FE1D55B00F9FA28 /* WKInspectorWKWebView.h in Headers */,
  • trunk/Source/WebKit/WebProcess/Inspector/mac/WebInspectorUIMac.mm

    r255214 r274697  
    11/*
    2  * Copyright (C) 2010-2019 Apple Inc. All rights reserved.
     2 * Copyright (C) 2010-2021 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
    … …  
    2727#import "WebInspectorUI.h"
    2828#import "RemoteWebInspectorUI.h"
     29#import "WKInspectorViewController.h"
    2930
    3031#if PLATFORM(MAC)
    … …  
    3940static String webInspectorUILocalizedStringsURL()
    4041{
    41     NSBundle *bundle = [NSBundle bundleWithIdentifier:@"com.apple.WebInspectorUI"];
    42     if (!bundle)
    43         return String();
    44 
    45     NSString *path = [bundle pathForResource:@"localizedStrings" ofType:@"js"];
    46     if (!path)
    47         return String();
    48    
    49     return [NSURL fileURLWithPath:path isDirectory:NO].absoluteString;
     42    return [WKInspectorViewController URLForInspectorResource:@"localizedStrings.js"].absoluteString;
    5043}
    5144
Note: See TracChangeset for help on using the changeset viewer.