WebKitTools/MiniBrowser/mac/AppDelegate.m
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 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  * 1. Redistributions of source code must retain the above copyright
       
     8  *    notice, this list of conditions and the following disclaimer.
       
     9  * 2. Redistributions in binary form must reproduce the above copyright
       
    10  *    notice, this list of conditions and the following disclaimer in the
       
    11  *    documentation and/or other materials provided with the distribution.
       
    12  *
       
    13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
       
    14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
       
    17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
       
    18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
       
    19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
       
    20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
       
    21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
       
    22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
       
    23  * THE POSSIBILITY OF SUCH DAMAGE.
       
    24  */
       
    25 
       
    26 #import "AppDelegate.h"
       
    27 
       
    28 #import "BrowserWindowController.h"
       
    29 #import "BrowserStatisticsWindowController.h"
       
    30 
       
    31 #import <WebKit2/WKStringCF.h>
       
    32 #import <WebKit2/WKContextPrivate.h>
       
    33 
       
    34 static NSString *defaultURL = @"http://www.webkit.org/";
       
    35 
       
    36 @implementation BrowserAppDelegate
       
    37 
       
    38 void _didRecieveMessageFromInjectedBundle(WKContextRef context, WKStringRef message, const void *clientInfo)
       
    39 {
       
    40     CFStringRef cfMessage = WKStringCopyCFString(0, message);
       
    41     LOG(@"ContextInjectedBundleClient - didRecieveMessage - message: %@", cfMessage);
       
    42     CFRelease(cfMessage);
       
    43 
       
    44     WKStringRef newMessage = WKStringCreateWithCFString(CFSTR("Roger that!"));
       
    45     WKContextPostMessageToInjectedBundle(context, newMessage);
       
    46     WKStringRelease(newMessage);
       
    47 }
       
    48 
       
    49 - (id)init
       
    50 {
       
    51     self = [super init];
       
    52     if (self) {
       
    53         if ([NSEvent modifierFlags] & NSShiftKeyMask)
       
    54             currentProcessModel = kProcessModelSharedSecondaryThread;
       
    55         else
       
    56             currentProcessModel = kProcessModelSharedSecondaryProcess;
       
    57 
       
    58         WKContextRef threadContext = WKContextGetSharedThreadContext();
       
    59         threadPageNamespace = WKPageNamespaceCreate(threadContext);
       
    60         WKContextRelease(threadContext);
       
    61 
       
    62         CFStringRef bundlePathCF = (CFStringRef)[[NSBundle mainBundle] pathForAuxiliaryExecutable:@"WebBundle.bundle"];
       
    63         WKStringRef bundlePath = WKStringCreateWithCFString(bundlePathCF);
       
    64 
       
    65         WKContextRef processContext = WKContextCreateWithInjectedBundlePath(bundlePath);
       
    66         
       
    67         WKContextInjectedBundleClient bundleClient = {
       
    68             0,      /* version */
       
    69             0,      /* clientInfo */
       
    70             _didRecieveMessageFromInjectedBundle
       
    71         };
       
    72         WKContextSetInjectedBundleClient(processContext, &bundleClient);
       
    73         
       
    74         processPageNamespace = WKPageNamespaceCreate(processContext);
       
    75         WKContextRelease(processContext);
       
    76 
       
    77         WKStringRelease(bundlePath);
       
    78     }
       
    79 
       
    80     return self;
       
    81 }
       
    82 
       
    83 - (IBAction)newWindow:(id)sender
       
    84 {
       
    85     BrowserWindowController *controller = [[BrowserWindowController alloc] initWithPageNamespace:[self getCurrentPageNamespace]];
       
    86     [[controller window] makeKeyAndOrderFront:sender];
       
    87     
       
    88     [controller loadURLString:defaultURL];
       
    89 }
       
    90 
       
    91 - (WKPageNamespaceRef)getCurrentPageNamespace
       
    92 {
       
    93     return (currentProcessModel == kProcessModelSharedSecondaryThread) ? threadPageNamespace : processPageNamespace;
       
    94 }
       
    95 
       
    96 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
       
    97 {
       
    98     if ([menuItem action] == @selector(setSharedProcessProcessModel:))
       
    99         [menuItem setState:currentProcessModel == kProcessModelSharedSecondaryProcess ? NSOnState : NSOffState];
       
   100     else if ([menuItem action] == @selector(setSharedThreadProcessModel:))
       
   101         [menuItem setState:currentProcessModel == kProcessModelSharedSecondaryThread ? NSOnState : NSOffState];
       
   102     return YES;
       
   103 }        
       
   104 
       
   105 - (void)_setProcessModel:(ProcessModel)processModel
       
   106 {
       
   107     if (processModel == currentProcessModel)
       
   108         return;
       
   109  
       
   110     currentProcessModel = processModel;
       
   111 }
       
   112 
       
   113 - (IBAction)setSharedProcessProcessModel:(id)sender
       
   114 {
       
   115     [self _setProcessModel:kProcessModelSharedSecondaryProcess];
       
   116 }
       
   117 
       
   118 - (IBAction)setSharedThreadProcessModel:(id)sender
       
   119 {
       
   120     [self _setProcessModel:kProcessModelSharedSecondaryThread];
       
   121 }
       
   122 
       
   123 - (IBAction)showStatisticsWindow:(id)sender
       
   124 {
       
   125     static BrowserStatisticsWindowController* windowController;
       
   126     if (!windowController)
       
   127         windowController = [[BrowserStatisticsWindowController alloc] initWithThreadedWKContextRef:WKPageNamespaceGetContext(threadPageNamespace) 
       
   128                                                                                processWKContextRef:WKPageNamespaceGetContext(processPageNamespace)];
       
   129 
       
   130     [[windowController window] makeKeyAndOrderFront:self];
       
   131 }
       
   132 
       
   133 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
       
   134 {
       
   135     [self newWindow:self];
       
   136 }
       
   137 
       
   138 - (void)applicationWillTerminate:(NSNotification *)notification
       
   139 {
       
   140     NSArray* windows = [NSApp windows];
       
   141     for (NSWindow* window in windows) {
       
   142         id delegate = [window delegate];
       
   143         if ([delegate isKindOfClass:[BrowserWindowController class]]) {
       
   144             BrowserWindowController *controller = (BrowserWindowController *)delegate;
       
   145             [controller applicationTerminating];
       
   146         }
       
   147     }
       
   148     
       
   149     WKPageNamespaceRelease(threadPageNamespace);
       
   150     threadPageNamespace = 0;
       
   151 
       
   152     WKPageNamespaceRelease(processPageNamespace);
       
   153     processPageNamespace = 0;
       
   154 }
       
   155 
       
   156 - (BrowserWindowController *)frontmostBrowserWindowController
       
   157 {
       
   158     NSArray* windows = [NSApp windows];
       
   159     for (NSWindow* window in windows) {
       
   160         id delegate = [window delegate];
       
   161         if ([delegate isKindOfClass:[BrowserWindowController class]])
       
   162             return (BrowserWindowController *)delegate;
       
   163     }
       
   164 
       
   165     return 0;
       
   166 }
       
   167 
       
   168 - (IBAction)openDocument:(id)sender
       
   169 {
       
   170     NSOpenPanel *openPanel = [[NSOpenPanel openPanel] retain];
       
   171     [openPanel beginForDirectory:nil
       
   172         file:nil
       
   173         types:nil
       
   174         modelessDelegate:self
       
   175         didEndSelector:@selector(openPanelDidEnd:returnCode:contextInfo:)
       
   176         contextInfo:0];
       
   177 }
       
   178 
       
   179 - (void)openPanelDidEnd:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
       
   180 {
       
   181     [sheet autorelease];
       
   182     if (returnCode != NSOKButton || ![[sheet filenames] count])
       
   183         return;
       
   184     
       
   185     NSString* filePath = [[sheet filenames] objectAtIndex:0];
       
   186 
       
   187     BrowserWindowController *controller = [self frontmostBrowserWindowController];
       
   188     if (!controller) {
       
   189         controller = [[BrowserWindowController alloc] initWithPageNamespace:[self getCurrentPageNamespace]];
       
   190         [[controller window] makeKeyAndOrderFront:self];
       
   191     }
       
   192     
       
   193     [controller loadURLString:[[NSURL fileURLWithPath:filePath] absoluteString]];
       
   194 }
       
   195 
       
   196 @end