Changeset 197004 in webkit


Ignore:
Timestamp:
Feb 23, 2016 4:20:01 PM (8 years ago)
Author:
andersca@apple.com
Message:

WKWebView should implement NSCoding
https://bugs.webkit.org/show_bug.cgi?id=137160
Source/WebKit2:

rdar://problem/17380562

Reviewed by Dan Bernstein.

  • UIProcess/API/Cocoa/WKUserContentController.mm:

(-[WKUserContentController initWithCoder:]):
We need to call [self init] here, so that the wrapper will be initialized.

  • UIProcess/API/Cocoa/WKWebView.h:

-initWithCoder: shouldn't be unavailable, it should be a designated initializer.

  • UIProcess/API/Cocoa/WKWebView.mm:

(-[WKWebView _initializeWithConfiguration:]):
Move initialization out into a common method.

(-[WKWebView initWithFrame:configuration:]):
Call -initializeWithConfiguration: here.

(-[WKWebView initWithCoder:]):
Decode everything.

(-[WKWebView encodeWithCoder:]):
Encode everything.

Tools:

Reviewed by Dan Bernstein.

Add tests.

  • TestWebKitAPI/Tests/WebKit2Cocoa/Coding.mm:

(TEST):

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r197003 r197004  
     12016-02-23  Anders Carlsson  <andersca@apple.com>
     2
     3        WKWebView should implement NSCoding
     4        https://bugs.webkit.org/show_bug.cgi?id=137160
     5        rdar://problem/17380562
     6
     7        Reviewed by Dan Bernstein.
     8
     9        * UIProcess/API/Cocoa/WKUserContentController.mm:
     10        (-[WKUserContentController initWithCoder:]):
     11        We need to call [self init] here, so that the wrapper will be initialized.
     12
     13        * UIProcess/API/Cocoa/WKWebView.h:
     14        -initWithCoder: shouldn't be unavailable, it should be a designated initializer.
     15
     16        * UIProcess/API/Cocoa/WKWebView.mm:
     17        (-[WKWebView _initializeWithConfiguration:]):
     18        Move initialization out into a common method.
     19
     20        (-[WKWebView initWithFrame:configuration:]):
     21        Call -initializeWithConfiguration: here.
     22
     23        (-[WKWebView initWithCoder:]):
     24        Decode everything.
     25
     26        (-[WKWebView encodeWithCoder:]):
     27        Encode everything.
     28
    1292016-02-23  Anders Carlsson  <andersca@apple.com>
    230
  • trunk/Source/WebKit2/UIProcess/API/Cocoa/WKUserContentController.mm

    r197000 r197004  
    6969- (instancetype)initWithCoder:(NSCoder *)coder
    7070{
    71     if (!(self = [super init]))
     71    if (!(self = [self init]))
    7272        return nil;
    7373
  • trunk/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.h

    r195963 r197004  
    8484- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration NS_DESIGNATED_INITIALIZER;
    8585
    86 - (instancetype)initWithCoder:(NSCoder *)coder NS_UNAVAILABLE;
     86- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
    8787
    8888/*! @abstract Navigates to a requested URL.
  • trunk/Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm

    r196024 r197004  
    339339#endif
    340340
    341 - (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
    342 {
    343     if (!(self = [super initWithFrame:frame]))
    344         return nil;
    345 
     341- (void)_initializeWithConfiguration:(WKWebViewConfiguration *)configuration
     342{
    346343    if (!configuration)
    347344        [NSException raise:NSInvalidArgumentException format:@"Configuration cannot be nil"];
     
    359356
    360357    [_configuration _validate];
    361 
    362358
    363359    WebKit::WebProcessPool& processPool = *[_configuration processPool]->_processPool;
     
    470466
    471467    pageToViewMap().add(_page.get(), self);
     468}
     469
     470- (instancetype)initWithFrame:(CGRect)frame configuration:(WKWebViewConfiguration *)configuration
     471{
     472    if (!(self = [super initWithFrame:frame]))
     473        return nil;
     474
     475    [self _initializeWithConfiguration:configuration];
    472476
    473477    return self;
     
    476480- (instancetype)initWithCoder:(NSCoder *)coder
    477481{
    478     [self release];
    479     return nil;
     482    if (!(self = [super initWithCoder:coder]))
     483        return nil;
     484
     485    WKWebViewConfiguration *configuration = [coder decodeObjectForKey:@"configuration"];
     486    [self _initializeWithConfiguration:configuration];
     487
     488    self.allowsBackForwardNavigationGestures = [coder decodeBoolForKey:@"allowsBackForwardNavigationGestures"];
     489    self.customUserAgent = [coder decodeObjectForKey:@"customUserAgent"];
     490    self.allowsLinkPreview = [coder decodeBoolForKey:@"allowsLinkPreview"];
     491
     492#if PLATFORM(MAC)
     493    self.allowsMagnification = [coder decodeBoolForKey:@"allowsMagnification"];
     494    self.magnification = [coder decodeDoubleForKey:@"magnification"];
     495#endif
     496
     497    return self;
     498}
     499
     500- (void)encodeWithCoder:(NSCoder *)coder
     501{
     502    [coder encodeObject:_configuration.get() forKey:@"configuration"];
     503
     504    [coder encodeBool:self.allowsBackForwardNavigationGestures forKey:@"allowsBackForwardNavigationGestures"];
     505    [coder encodeObject:self.customUserAgent forKey:@"customUserAgent"];
     506    [coder encodeBool:self.allowsLinkPreview forKey:@"allowsLinkPreview"];
     507
     508#if PLATFORM(MAC)
     509    [coder encodeBool:self.allowsMagnification forKey:@"allowsMagnification"];
     510    [coder encodeDouble:self.magnification forKey:@"magnification"];
     511#endif
    480512}
    481513
  • trunk/Tools/ChangeLog

    r196997 r197004  
     12016-02-23  Anders Carlsson  <andersca@apple.com>
     2
     3        WKWebView should implement NSCoding
     4        https://bugs.webkit.org/show_bug.cgi?id=137160
     5
     6        Reviewed by Dan Bernstein.
     7
     8        Add tests.
     9
     10        * TestWebKitAPI/Tests/WebKit2Cocoa/Coding.mm:
     11        (TEST):
     12
    1132016-02-23  Anders Carlsson  <andersca@apple.com>
    214
  • trunk/Tools/TestWebKitAPI/Tests/WebKit2Cocoa/Coding.mm

    r196997 r197004  
    121121#endif
    122122}
     123
     124TEST(Coding, WKWebView)
     125{
     126    auto a = adoptNS([[WKWebView alloc] init]);
     127
     128    // Change all defaults to something else.
     129    [a setAllowsBackForwardNavigationGestures:YES];
     130    [a setCustomUserAgent:@"CustomUserAgent"];
     131
     132#if PLATFORM(IOS)
     133    [a setAllowsLinkPreview:YES];
     134#else
     135    [a setAllowsLinkPreview:NO];
     136    [a setAllowsMagnification:YES];
     137    [a setMagnification:2];
    123138#endif
     139
     140    auto b = encodeAndDecode(a.get());
     141
     142    EXPECT_EQ([a allowsBackForwardNavigationGestures], [b allowsBackForwardNavigationGestures]);
     143    EXPECT_TRUE([[a customUserAgent] isEqualToString:[b customUserAgent]]);
     144    EXPECT_EQ([a allowsLinkPreview], [b allowsLinkPreview]);
     145
     146#if PLATFORM(MAC)
     147    EXPECT_EQ([a allowsMagnification], [b allowsMagnification]);
     148    EXPECT_EQ([a magnification], [b magnification]);
     149#endif
     150}
     151
     152TEST(Coding, WKWebView_SameConfiguration)
     153{
     154    // First, encode two WKWebViews sharing the same configuration.
     155    RetainPtr<NSData> data;
     156    {
     157        auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     158
     159        auto a = adoptNS([[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration.get()]);
     160        auto b = adoptNS([[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration.get()]);
     161
     162        data = [NSKeyedArchiver archivedDataWithRootObject:@[a.get(), b.get()]];
     163    }
     164
     165    // Then, decode and verify that the important configuration properties are the same.
     166    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:data.get()];
     167
     168    WKWebView *aView = array[0];
     169    WKWebView *bView = array[1];
     170
     171    WKWebViewConfiguration *a = aView.configuration;
     172    WKWebViewConfiguration *b = bView.configuration;
     173
     174    EXPECT_EQ(a.processPool, b.processPool);
     175    EXPECT_EQ(a.preferences, b.preferences);
     176    EXPECT_EQ(a.userContentController, b.userContentController);
     177    EXPECT_EQ(a.websiteDataStore, b.websiteDataStore);
     178}
     179
     180#endif
Note: See TracChangeset for help on using the changeset viewer.