webengine/osswebengine/WebKit/Plugins/WebPluginContainerCheck.mm
changeset 0 dd21522fd290
equal deleted inserted replaced
-1:000000000000 0:dd21522fd290
       
     1 /*
       
     2  * Copyright (C) 2005 Apple Computer, 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 "WebPluginContainerCheck.h"
       
    30 
       
    31 #import "WebFrame.h"
       
    32 #import "WebFrameBridge.h"
       
    33 #import "WebPluginContainer.h"
       
    34 #import "WebPluginContainerPrivate.h"
       
    35 #import "WebPluginController.h"
       
    36 #import "WebPolicyDelegate.h"
       
    37 #import "WebPolicyDelegatePrivate.h"
       
    38 #import "WebView.h"
       
    39 #import "WebViewInternal.h"
       
    40 #import <Foundation/NSDictionary.h>
       
    41 #import <Foundation/NSURL.h>
       
    42 #import <Foundation/NSURLRequest.h>
       
    43 #import <JavaScriptCore/Assertions.h>
       
    44 #import <WebCore/Frame.h>
       
    45 #import <WebCore/FrameLoader.h>
       
    46 #import <WebCore/FrameLoaderTypes.h>
       
    47 #import <objc/objc-runtime.h>
       
    48 
       
    49 using namespace WebCore;
       
    50 
       
    51 @implementation WebPluginContainerCheck
       
    52 
       
    53 + (id)checkWithRequest:(NSURLRequest *)request target:(NSString *)target resultObject:(id)obj selector:(SEL)selector controller:(WebPluginController *)controller
       
    54 {
       
    55     return [[[self alloc] initWithRequest:request target:target resultObject:obj selector:selector controller:controller] autorelease];
       
    56 }
       
    57 
       
    58 - (id)initWithRequest:(NSURLRequest *)request target:(NSString *)target resultObject:(id)obj selector:(SEL)selector controller:(WebPluginController *)controller
       
    59 {
       
    60     if (!(self = [super init]))
       
    61         return nil;
       
    62 
       
    63     _request = [request copy];
       
    64     _target = [target copy];
       
    65     _resultObject = [obj retain];
       
    66     _resultSelector = selector;
       
    67 
       
    68     // controller owns us so don't retain, to avoid cycle
       
    69     _controller = controller;
       
    70 
       
    71     return self;
       
    72 }
       
    73 
       
    74 - (void)finalize
       
    75 {
       
    76     // mandatory to complete or cancel before releasing this object
       
    77     ASSERT(_done);
       
    78     [super finalize];
       
    79 }
       
    80 
       
    81 - (void)dealloc
       
    82 {
       
    83     // mandatory to complete or cancel before releasing this object
       
    84     ASSERT(_done);
       
    85     [super dealloc];
       
    86 }
       
    87 
       
    88 - (void)_continueWithPolicy:(PolicyAction)policy
       
    89 {
       
    90     ((void (*)(id, SEL, BOOL))objc_msgSend)(_resultObject, _resultSelector, (policy == PolicyUse));
       
    91 
       
    92     // this will call indirectly call cancel
       
    93     [_controller _webPluginContainerCancelCheckIfAllowedToLoadRequest:self];
       
    94 }
       
    95 
       
    96 - (BOOL)_isForbiddenFileLoad
       
    97 {
       
    98    WebFrameBridge *bridge = [_controller bridge];
       
    99    ASSERT(bridge);
       
   100    if (![bridge _frame]->loader()->canLoad([_request URL], [bridge _frame]->document())) {
       
   101        [self _continueWithPolicy:PolicyIgnore];
       
   102        return YES;
       
   103    }
       
   104 
       
   105    return NO;
       
   106 }
       
   107 
       
   108 - (NSDictionary *)_actionInformationWithURL:(NSURL *)URL
       
   109 {
       
   110     return [NSDictionary dictionaryWithObjectsAndKeys:
       
   111                [NSNumber numberWithInt:WebNavigationTypePlugInRequest], WebActionNavigationTypeKey,
       
   112                [NSNumber numberWithInt:0], WebActionModifierFlagsKey,
       
   113                URL, WebActionOriginalURLKey,
       
   114                nil];
       
   115 }
       
   116 
       
   117 - (void)_askPolicyDelegate
       
   118 {
       
   119     WebView *webView = [_controller webView];
       
   120 
       
   121     WebFrame *targetFrame;
       
   122     if ([_target length] > 0) {
       
   123         targetFrame = [[_controller webFrame] findFrameNamed:_target];
       
   124     } else {
       
   125         targetFrame = [_controller webFrame];
       
   126     }
       
   127 
       
   128     NSDictionary *action = [self _actionInformationWithURL:[_request URL]];
       
   129 
       
   130     _listener = [[WebPolicyDecisionListener alloc] _initWithTarget:self action:@selector(_continueWithPolicy:)];
       
   131 
       
   132     if (targetFrame == nil) {
       
   133         // would open new window
       
   134         [[webView _policyDelegateForwarder] webView:webView
       
   135                      decidePolicyForNewWindowAction:action
       
   136                                             request:_request
       
   137                                        newFrameName:_target
       
   138                                    decisionListener:_listener];
       
   139     } else {
       
   140         // would target existing frame
       
   141         [[webView _policyDelegateForwarder] webView:webView
       
   142                     decidePolicyForNavigationAction:action
       
   143                                             request:_request
       
   144                                               frame:targetFrame
       
   145                                    decisionListener:_listener];        
       
   146     }
       
   147 }
       
   148 
       
   149 - (void)start
       
   150 {
       
   151     ASSERT(!_listener);
       
   152     ASSERT(!_done);
       
   153 
       
   154     if ([self _isForbiddenFileLoad])
       
   155         return;
       
   156 
       
   157     [self _askPolicyDelegate];
       
   158 }
       
   159 
       
   160 - (void)cancel
       
   161 {
       
   162     if (_done)
       
   163         return;
       
   164 
       
   165     [_request release];
       
   166     _request = nil;
       
   167     
       
   168     [_target release];
       
   169     _target = nil;
       
   170 
       
   171     [_listener _invalidate];
       
   172     [_listener release];
       
   173     _listener = nil;
       
   174 
       
   175     [_resultObject autorelease];
       
   176     _resultObject = nil;
       
   177 
       
   178     _controller = nil;
       
   179 
       
   180     _done = YES;
       
   181 }
       
   182 
       
   183 @end