📄 resourcehandlemac.mm
字号:
[[d->m_currentMacChallenge sender] continueWithoutCredentialForAuthenticationChallenge:d->m_currentMacChallenge]; clearAuthentication();}void ResourceHandle::receivedCancellation(const AuthenticationChallenge& challenge){ if (challenge != d->m_currentWebChallenge) return; if (client()) client()->receivedCancellation(this, challenge);}} // namespace WebCore@implementation WebCoreResourceHandleAsDelegate- (id)initWithHandle:(ResourceHandle*)handle{ self = [self init]; if (!self) return nil; m_handle = handle; return self;}#ifndef BUILDING_ON_TIGER- (void)dealloc{ [m_url release]; [super dealloc];}#endif- (void)detachHandle{ m_handle = 0;}- (NSURLRequest *)connection:(NSURLConnection *)unusedConnection willSendRequest:(NSURLRequest *)newRequest redirectResponse:(NSURLResponse *)redirectResponse{ UNUSED_PARAM(unusedConnection); // the willSendRequest call may cancel this load, in which case self could be deallocated RetainPtr<WebCoreResourceHandleAsDelegate> protect(self); if (!m_handle || !m_handle->client()) return nil; // See <rdar://problem/5380697> . This is a workaround for a behavior change in CFNetwork where willSendRequest gets called more often. if (!redirectResponse) return newRequest; CallbackGuard guard; ResourceRequest request = newRequest; m_handle->client()->willSendRequest(m_handle, request, redirectResponse);#ifndef BUILDING_ON_TIGER NSURL *copy = [[request.nsURLRequest() URL] copy]; [m_url release]; m_url = copy;#endif if (!ResourceHandle::didSendBodyDataDelegateExists()) { // The client may change the request's body stream, in which case we have to re-associate // the handle with the new stream so upload progress callbacks continue to work correctly. NSInputStream* oldBodyStream = [newRequest HTTPBodyStream]; NSInputStream* newBodyStream = [request.nsURLRequest() HTTPBodyStream]; if (oldBodyStream != newBodyStream) { disassociateStreamWithResourceHandle(oldBodyStream); associateStreamWithResourceHandle(newBodyStream, m_handle); } } return request.nsURLRequest();}- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)unusedConnection{ UNUSED_PARAM(unusedConnection); if (!m_handle) return NO; CallbackGuard guard; return m_handle->shouldUseCredentialStorage();}- (void)connection:(NSURLConnection *)unusedConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ UNUSED_PARAM(unusedConnection);#ifndef BUILDING_ON_TIGER if ([challenge previousFailureCount] == 0) { NSString *user = [m_url user]; NSString *password = [m_url password]; if (user && password) { NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:user password:password persistence:NSURLCredentialPersistenceForSession]; [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; [credential release]; return; } }#endif if (!m_handle) return; CallbackGuard guard; m_handle->didReceiveAuthenticationChallenge(core(challenge));}- (void)connection:(NSURLConnection *)unusedConnection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ UNUSED_PARAM(unusedConnection); if (!m_handle) return; CallbackGuard guard; m_handle->didCancelAuthenticationChallenge(core(challenge));}- (void)connection:(NSURLConnection *)unusedConnection didReceiveResponse:(NSURLResponse *)r{ UNUSED_PARAM(unusedConnection); if (!m_handle || !m_handle->client()) return; CallbackGuard guard; m_handle->client()->didReceiveResponse(m_handle, r);}- (void)connection:(NSURLConnection *)unusedConnection didReceiveData:(NSData *)data lengthReceived:(long long)lengthReceived{ UNUSED_PARAM(unusedConnection); if (!m_handle || !m_handle->client()) return; // FIXME: If we get more than 2B bytes in a single chunk, this code won't do the right thing. // However, with today's computers and networking speeds, this won't happen in practice. // Could be an issue with a giant local file. CallbackGuard guard; m_handle->client()->didReceiveData(m_handle, (const char*)[data bytes], [data length], static_cast<int>(lengthReceived));}- (void)connection:(NSURLConnection *)unusedConnection willStopBufferingData:(NSData *)data{ UNUSED_PARAM(unusedConnection); if (!m_handle || !m_handle->client()) return; // FIXME: If we get a resource with more than 2B bytes, this code won't do the right thing. // However, with today's computers and networking speeds, this won't happen in practice. // Could be an issue with a giant local file. CallbackGuard guard; m_handle->client()->willStopBufferingData(m_handle, (const char*)[data bytes], static_cast<int>([data length]));}- (void)connection:(NSURLConnection *)unusedConnection didSendBodyData:(NSInteger)unusedBytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite{ UNUSED_PARAM(unusedConnection); UNUSED_PARAM(unusedBytesWritten); if (!m_handle || !m_handle->client()) return; CallbackGuard guard; m_handle->client()->didSendData(m_handle, totalBytesWritten, totalBytesExpectedToWrite);}- (void)connectionDidFinishLoading:(NSURLConnection *)unusedConnection{ UNUSED_PARAM(unusedConnection); if (!m_handle || !m_handle->client()) return; CallbackGuard guard; if (!ResourceHandle::didSendBodyDataDelegateExists()) disassociateStreamWithResourceHandle([m_handle->request().nsURLRequest() HTTPBodyStream]); m_handle->client()->didFinishLoading(m_handle);}- (void)connection:(NSURLConnection *)unusedConnection didFailWithError:(NSError *)error{ UNUSED_PARAM(unusedConnection); if (!m_handle || !m_handle->client()) return; CallbackGuard guard; if (!ResourceHandle::didSendBodyDataDelegateExists()) disassociateStreamWithResourceHandle([m_handle->request().nsURLRequest() HTTPBodyStream]); m_handle->client()->didFail(m_handle, error);}#ifdef BUILDING_ON_TIGER- (void)_callConnectionWillCacheResponseWithInfo:(NSMutableDictionary *)info{ NSURLConnection *connection = [info objectForKey:@"connection"]; NSCachedURLResponse *cachedResponse = [info objectForKey:@"cachedResponse"]; NSCachedURLResponse *result = [self connection:connection willCacheResponse:cachedResponse]; if (result) [info setObject:result forKey:@"result"];}#endif- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse{#ifdef BUILDING_ON_TIGER // On Tiger CFURLConnection can sometimes call the connection:willCacheResponse: delegate method on // a secondary thread instead of the main thread. If this happens perform the work on the main thread. if (!pthread_main_np()) { NSMutableDictionary *info = [[NSMutableDictionary alloc] init]; if (connection) [info setObject:connection forKey:@"connection"]; if (cachedResponse) [info setObject:cachedResponse forKey:@"cachedResponse"]; // Include synchronous url connection's mode as an acceptable run loopmode // <rdar://problem/5511842> NSArray *modes = [[NSArray alloc] initWithObjects:(NSString *)kCFRunLoopCommonModes, @"NSSynchronousURLConnection_PrivateMode", nil]; [self performSelectorOnMainThread:@selector(_callConnectionWillCacheResponseWithInfo:) withObject:info waitUntilDone:YES modes:modes]; [modes release]; NSCachedURLResponse *result = [[info valueForKey:@"result"] retain]; [info release]; return [result autorelease]; }#else UNUSED_PARAM(connection);#endif#ifndef NDEBUG if (isInitializingConnection) LOG_ERROR("connection:willCacheResponse: was called inside of [NSURLConnection initWithRequest:delegate:] (4067625)");#endif if (!m_handle || !m_handle->client()) return nil; CallbackGuard guard; NSCachedURLResponse *newResponse = m_handle->client()->willCacheResponse(m_handle, cachedResponse); if (newResponse != cachedResponse) return newResponse; CacheStoragePolicy policy = static_cast<CacheStoragePolicy>([newResponse storagePolicy]); m_handle->client()->willCacheResponse(m_handle, policy); if (static_cast<NSURLCacheStoragePolicy>(policy) != [newResponse storagePolicy]) newResponse = [[[NSCachedURLResponse alloc] initWithResponse:[newResponse response] data:[newResponse data] userInfo:[newResponse userInfo] storagePolicy:static_cast<NSURLCacheStoragePolicy>(policy)] autorelease]; return newResponse;}- (void)useCredential:(NSURLCredential *)credential forAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ if (!m_handle) return; m_handle->receivedCredential(core(challenge), core(credential));}- (void)continueWithoutCredentialForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ if (!m_handle) return; m_handle->receivedRequestToContinueWithoutCredential(core(challenge));}- (void)cancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ if (!m_handle) return; m_handle->receivedCancellation(core(challenge));}@end#ifndef BUILDING_ON_TIGER@implementation WebCoreSynchronousLoader- (BOOL)_isDone{ return m_isDone;}- (void)dealloc{ [m_url release]; [m_response release]; [m_data release]; [m_error release]; [super dealloc];}- (NSURLRequest *)connection:(NSURLConnection *)unusedConnection willSendRequest:(NSURLRequest *)newRequest redirectResponse:(NSURLResponse *)unusedRedirectResponse{ UNUSED_PARAM(unusedConnection); UNUSED_PARAM(unusedRedirectResponse); NSURL *copy = [[newRequest URL] copy]; [m_url release]; m_url = copy; return newRequest;}- (void)connection:(NSURLConnection *)unusedConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{ UNUSED_PARAM(unusedConnection); if ([challenge previousFailureCount] == 0) { NSString *user = [m_url user]; NSString *password = [m_url password]; if (user && password) { NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:user password:password persistence:NSURLCredentialPersistenceForSession]; [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; [credential release]; return; } } [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];}- (void)connection:(NSURLConnection *)unusedConnection didReceiveResponse:(NSURLResponse *)response{ UNUSED_PARAM(unusedConnection); NSURLResponse *r = [response copy]; [m_response release]; m_response = r;}- (void)connection:(NSURLConnection *)unusedConnection didReceiveData:(NSData *)data{ UNUSED_PARAM(unusedConnection); if (!m_data) m_data = [[NSMutableData alloc] init]; [m_data appendData:data];}- (void)connectionDidFinishLoading:(NSURLConnection *)unusedConnection{ UNUSED_PARAM(unusedConnection); m_isDone = YES;}- (void)connection:(NSURLConnection *)unusedConnection didFailWithError:(NSError *)error{ UNUSED_PARAM(unusedConnection); ASSERT(!m_error); m_error = [error retain]; m_isDone = YES;}- (NSData *)_data{ return [[m_data retain] autorelease];}- (NSURLResponse *)_response{ return [[m_response retain] autorelease];}- (NSError *)_error{ return [[m_error retain] autorelease];}+ (NSData *)loadRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error{ WebCoreSynchronousLoader *delegate = [[WebCoreSynchronousLoader alloc] init]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate startImmediately:NO]; [connection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:WebCoreSynchronousLoaderRunLoopMode]; [connection start]; while (![delegate _isDone]) [[NSRunLoop currentRunLoop] runMode:WebCoreSynchronousLoaderRunLoopMode beforeDate:[NSDate distantFuture]]; NSData *data = [delegate _data]; *response = [delegate _response]; *error = [delegate _error]; [connection cancel]; [connection release]; [delegate release]; return data;}@end#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -