webengine/osswebengine/WebKit/WebView/WebHTMLRepresentation.mm
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2005, 2006, 2007 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 
       
    29 #import "WebHTMLRepresentation.h"
       
    30 
       
    31 #import "DOMNodeInternal.h"
       
    32 #import "DOMRangeInternal.h"
       
    33 #import "WebArchive.h"
       
    34 #import "WebBasePluginPackage.h"
       
    35 #import "WebDataSourceInternal.h"
       
    36 #import "WebDocumentPrivate.h"
       
    37 #import "WebFrameBridge.h"
       
    38 #import "WebFrameInternal.h"
       
    39 #import "WebKitNSStringExtras.h"
       
    40 #import "WebKitStatisticsPrivate.h"
       
    41 #import "WebNSAttributedStringExtras.h"
       
    42 #import "WebNSObjectExtras.h"
       
    43 #import "WebResourcePrivate.h"
       
    44 #import "WebView.h"
       
    45 #import <Foundation/NSURLResponse.h>
       
    46 #import <JavaScriptCore/Assertions.h>
       
    47 #import <WebCore/Document.h>
       
    48 #import <WebCore/DocumentLoader.h>
       
    49 #import <WebCore/Frame.h>
       
    50 #import <WebCore/FrameLoader.h>
       
    51 #import <WebCore/MIMETypeRegistry.h>
       
    52 #import <WebCore/Range.h>
       
    53 
       
    54 using namespace WebCore;
       
    55 
       
    56 @interface WebHTMLRepresentationPrivate : NSObject
       
    57 {
       
    58 @public
       
    59     WebDataSource *dataSource;
       
    60     WebFrameBridge *bridge;
       
    61     NSData *parsedArchiveData;
       
    62     
       
    63     BOOL hasSentResponseToPlugin;
       
    64     id <WebPluginManualLoader> manualLoader;
       
    65     NSView *pluginView;
       
    66 }
       
    67 @end
       
    68 
       
    69 @implementation WebHTMLRepresentationPrivate
       
    70 
       
    71 - (void)dealloc
       
    72 {
       
    73     [parsedArchiveData release];
       
    74     [super dealloc];
       
    75 }
       
    76 
       
    77 @end
       
    78 
       
    79 @implementation WebHTMLRepresentation
       
    80 
       
    81 static NSArray *stringArray(const HashSet<String>& set)
       
    82 {
       
    83     NSMutableArray *array = [NSMutableArray arrayWithCapacity:set.size()];
       
    84     HashSet<String>::const_iterator end = set.end();
       
    85     for (HashSet<String>::const_iterator it = set.begin(); it != end; ++it)
       
    86         [array addObject:(NSString *)(*it)];
       
    87     return array;
       
    88 }
       
    89 
       
    90 static NSArray *concatenateArrays(NSArray *first, NSArray *second)
       
    91 {
       
    92     NSMutableArray *result = [[first mutableCopy] autorelease];
       
    93     [result addObjectsFromArray:second];
       
    94     return result;
       
    95 }
       
    96 
       
    97 + (NSArray *)supportedMIMETypes
       
    98 {
       
    99     static RetainPtr<NSArray> staticSupportedMIMETypes =
       
   100         concatenateArrays([self supportedNonImageMIMETypes], [self supportedImageMIMETypes]);
       
   101     return staticSupportedMIMETypes.get();
       
   102 }
       
   103 
       
   104 + (NSArray *)supportedNonImageMIMETypes
       
   105 {
       
   106     static RetainPtr<NSArray> staticSupportedNonImageMIMETypes =
       
   107         stringArray(MIMETypeRegistry::getSupportedNonImageMIMETypes());
       
   108     return staticSupportedNonImageMIMETypes.get();
       
   109 }
       
   110 
       
   111 + (NSArray *)supportedImageMIMETypes
       
   112 {
       
   113     static RetainPtr<NSArray> staticSupportedImageMIMETypes =
       
   114         stringArray(MIMETypeRegistry::getSupportedImageMIMETypes());
       
   115     return staticSupportedImageMIMETypes.get();
       
   116 }
       
   117 
       
   118 - init
       
   119 {
       
   120     self = [super init];
       
   121     if (!self)
       
   122         return nil;
       
   123     
       
   124     _private = [[WebHTMLRepresentationPrivate alloc] init];
       
   125     
       
   126     ++WebHTMLRepresentationCount;
       
   127     
       
   128     return self;
       
   129 }
       
   130 
       
   131 - (void)dealloc
       
   132 {
       
   133     --WebHTMLRepresentationCount;
       
   134     
       
   135     [_private release];
       
   136 
       
   137     [super dealloc];
       
   138 }
       
   139 
       
   140 - (void)finalize
       
   141 {
       
   142     --WebHTMLRepresentationCount;
       
   143 
       
   144     [super finalize];
       
   145 }
       
   146 
       
   147 - (WebFrameBridge *)_bridge
       
   148 {
       
   149     return _private->bridge;
       
   150 }
       
   151 
       
   152 - (void)_redirectDataToManualLoader:(id<WebPluginManualLoader>)manualLoader forPluginView:(NSView *)pluginView;
       
   153 {
       
   154     _private->manualLoader = manualLoader;
       
   155     _private->pluginView = pluginView;
       
   156 }
       
   157 
       
   158 - (void)setDataSource:(WebDataSource *)dataSource
       
   159 {
       
   160     _private->dataSource = dataSource;
       
   161     _private->bridge = [[dataSource webFrame] _bridge];
       
   162 }
       
   163 
       
   164 - (BOOL)_isDisplayingWebArchive
       
   165 {
       
   166     return [[[_private->dataSource response] MIMEType] _webkit_isCaseInsensitiveEqualToString:@"application/x-webarchive"];
       
   167 }
       
   168 
       
   169 - (void)receivedData:(NSData *)data withDataSource:(WebDataSource *)dataSource
       
   170 {
       
   171     if ([dataSource webFrame] && ![self _isDisplayingWebArchive]) {
       
   172         if (!_private->pluginView)
       
   173             [_private->bridge receivedData:data textEncodingName:[[_private->dataSource response] textEncodingName]];
       
   174 
       
   175         if (_private->pluginView) {
       
   176             if (!_private->hasSentResponseToPlugin) {
       
   177                 [_private->manualLoader pluginView:_private->pluginView receivedResponse:[dataSource response]];
       
   178                 _private->hasSentResponseToPlugin = YES;
       
   179             }
       
   180             
       
   181             [_private->manualLoader pluginView:_private->pluginView receivedData:data];
       
   182         }
       
   183     }
       
   184 }
       
   185 
       
   186 - (void)receivedError:(NSError *)error withDataSource:(WebDataSource *)dataSource
       
   187 {
       
   188     if (_private->pluginView) {
       
   189         [_private->manualLoader pluginView:_private->pluginView receivedError:error];
       
   190     }
       
   191 }
       
   192 
       
   193 - (void)_loadDataSourceAsWebArchive
       
   194 {
       
   195     WebArchive *archive = [[WebArchive alloc] initWithData:[_private->dataSource data]];
       
   196     WebResource *mainResource = [archive mainResource];
       
   197     if (!mainResource) {
       
   198         [archive release];
       
   199         return;
       
   200     }
       
   201     
       
   202     NSData *data = [mainResource data];
       
   203     [data retain];
       
   204     [_private->parsedArchiveData release];
       
   205     _private->parsedArchiveData = data;
       
   206     
       
   207     [_private->dataSource _addToUnarchiveState:archive];
       
   208     [archive release];
       
   209     
       
   210     WebFrame *webFrame = [_private->dataSource webFrame];
       
   211     
       
   212     if (!webFrame)
       
   213         return;
       
   214     
       
   215     core(webFrame)->loader()->continueLoadWithData(SharedBuffer::wrapNSData(data).get(), [mainResource MIMEType], [mainResource textEncodingName], [mainResource URL]);
       
   216 }
       
   217 
       
   218 - (void)finishedLoadingWithDataSource:(WebDataSource *)dataSource
       
   219 {
       
   220     WebFrame *frame = [dataSource webFrame];
       
   221 
       
   222     if (_private->pluginView) {
       
   223         [_private->manualLoader pluginViewFinishedLoading:_private->pluginView];
       
   224         return;
       
   225     }
       
   226 
       
   227     if (frame) {
       
   228         if ([self _isDisplayingWebArchive])
       
   229             [self _loadDataSourceAsWebArchive];
       
   230         else
       
   231             // Telling the bridge we received some data and passing nil as the data is our
       
   232             // way to get work done that is normally done when the first bit of data is
       
   233             // received, even for the case of a document with no data (like about:blank).
       
   234             [_private->bridge receivedData:nil textEncodingName:[[_private->dataSource response] textEncodingName]];
       
   235         
       
   236         WebView *webView = [frame webView];
       
   237         if ([webView isEditable])
       
   238             core(frame)->applyEditingStyleToBodyElement();
       
   239     }
       
   240 }
       
   241 
       
   242 - (BOOL)canProvideDocumentSource
       
   243 {
       
   244     return [_private->bridge canProvideDocumentSource];
       
   245 }
       
   246 
       
   247 - (BOOL)canSaveAsWebArchive
       
   248 {
       
   249     return [_private->bridge canSaveAsWebArchive];
       
   250 }
       
   251 
       
   252 - (NSString *)documentSource
       
   253 {
       
   254     if ([self _isDisplayingWebArchive])
       
   255         return [[[NSString alloc] initWithData:_private->parsedArchiveData encoding:NSUTF8StringEncoding] autorelease]; 
       
   256 
       
   257     return [_private->bridge stringWithData:[_private->dataSource data]];
       
   258 }
       
   259 
       
   260 - (NSString *)title
       
   261 {
       
   262     return nsStringNilIfEmpty([_private->dataSource _documentLoader]->title());
       
   263 }
       
   264 
       
   265 - (DOMDocument *)DOMDocument
       
   266 {
       
   267     return [[_private->bridge webFrame] DOMDocument];
       
   268 }
       
   269 
       
   270 - (NSAttributedString *)attributedText
       
   271 {
       
   272     // FIXME: Implement
       
   273     return nil;
       
   274 }
       
   275 
       
   276 - (NSAttributedString *)attributedStringFrom:(DOMNode *)startNode startOffset:(int)startOffset to:(DOMNode *)endNode endOffset:(int)endOffset
       
   277 {
       
   278     Range range([startNode _node]->document(), [startNode _node], startOffset, [endNode _node], endOffset);
       
   279     return [NSAttributedString _web_attributedStringFromRange:&range];
       
   280 }
       
   281 
       
   282 - (DOMElement *)elementWithName:(NSString *)name inForm:(DOMElement *)form
       
   283 {
       
   284     return [_private->bridge elementWithName:name inForm:form];
       
   285 }
       
   286 
       
   287 - (BOOL)elementDoesAutoComplete:(DOMElement *)element
       
   288 {
       
   289     return [_private->bridge elementDoesAutoComplete:element];
       
   290 }
       
   291 
       
   292 - (BOOL)elementIsPassword:(DOMElement *)element
       
   293 {
       
   294     return [_private->bridge elementIsPassword:element];
       
   295 }
       
   296 
       
   297 - (DOMElement *)formForElement:(DOMElement *)element
       
   298 {
       
   299     return [_private->bridge formForElement:element];
       
   300 }
       
   301 
       
   302 - (DOMElement *)currentForm
       
   303 {
       
   304     return [_private->bridge currentForm];
       
   305 }
       
   306 
       
   307 - (NSArray *)controlsInForm:(DOMElement *)form
       
   308 {
       
   309     return [_private->bridge controlsInForm:form];
       
   310 }
       
   311 
       
   312 - (NSString *)searchForLabels:(NSArray *)labels beforeElement:(DOMElement *)element
       
   313 {
       
   314     return [_private->bridge searchForLabels:labels beforeElement:element];
       
   315 }
       
   316 
       
   317 - (NSString *)matchLabels:(NSArray *)labels againstElement:(DOMElement *)element
       
   318 {
       
   319     return [_private->bridge matchLabels:labels againstElement:element];
       
   320 }
       
   321 
       
   322 @end