PolicyDelegate.mm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2007, 2009 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. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  18. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  21. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. #import "config.h"
  29. #import "PolicyDelegate.h"
  30. #import "DumpRenderTree.h"
  31. #import "TestRunner.h"
  32. #import <WebKit/DOMElement.h>
  33. #import <WebKit/WebFrame.h>
  34. #import <WebKit/WebPolicyDelegate.h>
  35. #import <WebKit/WebView.h>
  36. @interface NSURL (DRTExtras)
  37. - (NSString *)_drt_descriptionSuitableForTestResult;
  38. @end
  39. @interface DOMNode (dumpPath)
  40. - (NSString *)dumpPath;
  41. @end
  42. @implementation PolicyDelegate
  43. - (void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation
  44. request:(NSURLRequest *)request
  45. frame:(WebFrame *)frame
  46. decisionListener:(id<WebPolicyDecisionListener>)listener
  47. {
  48. WebNavigationType navType = (WebNavigationType)[[actionInformation objectForKey:WebActionNavigationTypeKey] intValue];
  49. const char* typeDescription;
  50. switch (navType) {
  51. case WebNavigationTypeLinkClicked:
  52. typeDescription = "link clicked";
  53. break;
  54. case WebNavigationTypeFormSubmitted:
  55. typeDescription = "form submitted";
  56. break;
  57. case WebNavigationTypeBackForward:
  58. typeDescription = "back/forward";
  59. break;
  60. case WebNavigationTypeReload:
  61. typeDescription = "reload";
  62. break;
  63. case WebNavigationTypeFormResubmitted:
  64. typeDescription = "form resubmitted";
  65. break;
  66. case WebNavigationTypeOther:
  67. typeDescription = "other";
  68. break;
  69. default:
  70. typeDescription = "illegal value";
  71. }
  72. NSString *message = [NSString stringWithFormat:@"Policy delegate: attempt to load %@ with navigation type '%s'", [[request URL] _drt_descriptionSuitableForTestResult], typeDescription];
  73. if (DOMElement *originatingNode = [[actionInformation objectForKey:WebActionElementKey] objectForKey:WebElementDOMNodeKey])
  74. message = [message stringByAppendingFormat:@" originating from %@", [originatingNode dumpPath]];
  75. printf("%s\n", [message UTF8String]);
  76. if (permissiveDelegate)
  77. [listener use];
  78. else
  79. [listener ignore];
  80. if (controllerToNotifyDone) {
  81. controllerToNotifyDone->notifyDone();
  82. controllerToNotifyDone = 0;
  83. }
  84. }
  85. - (void)webView:(WebView *)webView unableToImplementPolicyWithError:(NSError *)error frame:(WebFrame *)frame
  86. {
  87. NSString *message = [NSString stringWithFormat:@"Policy delegate: unable to implement policy with error domain '%@', error code %ld, in frame '%@'", [error domain], static_cast<long>([error code]), [frame name]];
  88. printf("%s\n", [message UTF8String]);
  89. }
  90. static NSString *dispositionTypeFromContentDispositionHeader(NSString *header)
  91. {
  92. NSMutableString *result = [[[[header componentsSeparatedByString:@";"] objectAtIndex:0] mutableCopy] autorelease];
  93. if (result)
  94. CFStringTrimWhitespace((CFMutableStringRef)result);
  95. return result;
  96. }
  97. - (void)webView:(WebView *)c decidePolicyForMIMEType:(NSString *)type
  98. request:(NSURLRequest *)request
  99. frame:(WebFrame *)frame
  100. decisionListener:(id<WebPolicyDecisionListener>)listener
  101. {
  102. NSHTTPURLResponse *HTTPResponse = (NSHTTPURLResponse *)[[frame provisionalDataSource] response];
  103. if (![HTTPResponse isKindOfClass:[NSHTTPURLResponse class]])
  104. HTTPResponse = nil;
  105. NSString *dispositionType = dispositionTypeFromContentDispositionHeader([[HTTPResponse allHeaderFields] objectForKey:@"Content-Disposition"]);
  106. if (dispositionType && [dispositionType compare:@"attachment" options:NSCaseInsensitiveSearch] == NSOrderedSame) {
  107. printf("Policy delegate: resource is an attachment, suggested file name '%s'\n", [[HTTPResponse suggestedFilename] UTF8String]);
  108. [listener ignore];
  109. return;
  110. }
  111. [listener use];
  112. }
  113. - (void)setPermissive:(BOOL)permissive
  114. {
  115. permissiveDelegate = permissive;
  116. }
  117. - (void)setControllerToNotifyDone:(TestRunner*)controller
  118. {
  119. controllerToNotifyDone = controller;
  120. }
  121. @end