Home | History | Annotate | Download | only in mac
      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 "config.h"
     27 #import "WebInspectorProxy.h"
     28 
     29 #if ENABLE(INSPECTOR)
     30 
     31 #import "WKAPICast.h"
     32 #import "WKView.h"
     33 #import "WebPageProxy.h"
     34 #import "WebProcessProxy.h"
     35 #import <WebKitSystemInterface.h>
     36 #import <WebCore/LocalizedStrings.h>
     37 #import <wtf/text/WTFString.h>
     38 
     39 using namespace WebCore;
     40 using namespace WebKit;
     41 
     42 // The height needed to match a typical NSToolbar.
     43 static const CGFloat windowContentBorderThickness = 55;
     44 
     45 // WebInspectorProxyObjCAdapter is a helper ObjC object used as a delegate or notification observer
     46 // for the sole purpose of getting back into the C++ code from an ObjC caller.
     47 
     48 @interface WebInspectorProxyObjCAdapter : NSObject <NSWindowDelegate> {
     49     WebInspectorProxy* _inspectorProxy; // Not retained to prevent cycles
     50 }
     51 
     52 - (id)initWithWebInspectorProxy:(WebInspectorProxy*)inspectorProxy;
     53 
     54 @end
     55 
     56 @implementation WebInspectorProxyObjCAdapter
     57 
     58 - (id)initWithWebInspectorProxy:(WebInspectorProxy*)inspectorProxy
     59 {
     60     ASSERT_ARG(inspectorProxy, inspectorProxy);
     61 
     62     if (!(self = [super init]))
     63         return nil;
     64 
     65     _inspectorProxy = inspectorProxy; // Not retained to prevent cycles
     66 
     67     return self;
     68 }
     69 
     70 - (void)windowWillClose:(NSNotification *)notification
     71 {
     72     _inspectorProxy->close();
     73 }
     74 
     75 @end
     76 
     77 namespace WebKit {
     78 
     79 WebPageProxy* WebInspectorProxy::platformCreateInspectorPage()
     80 {
     81     ASSERT(m_page);
     82     ASSERT(!m_inspectorView);
     83 
     84     m_inspectorView.adoptNS([[WKView alloc] initWithFrame:NSZeroRect contextRef:toAPI(page()->process()->context()) pageGroupRef:toAPI(inspectorPageGroup())]);
     85     ASSERT(m_inspectorView);
     86 
     87     [m_inspectorView.get() setDrawsBackground:NO];
     88 
     89     return toImpl(m_inspectorView.get().pageRef);
     90 }
     91 
     92 void WebInspectorProxy::platformOpen()
     93 {
     94     ASSERT(!m_inspectorWindow);
     95 
     96     m_inspectorProxyObjCAdapter.adoptNS([[WebInspectorProxyObjCAdapter alloc] initWithWebInspectorProxy:this]);
     97 
     98     // FIXME: support opening in docked mode here.
     99 
    100     NSUInteger styleMask = (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask | NSTexturedBackgroundWindowMask);
    101     NSWindow *window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, initialWindowWidth, initialWindowHeight) styleMask:styleMask backing:NSBackingStoreBuffered defer:NO];
    102     [window setAutorecalculatesContentBorderThickness:NO forEdge:NSMaxYEdge];
    103     [window setContentBorderThickness:windowContentBorderThickness forEdge:NSMaxYEdge];
    104     [window setDelegate:m_inspectorProxyObjCAdapter.get()];
    105     [window setMinSize:NSMakeSize(minimumWindowWidth, minimumWindowHeight)];
    106     [window setReleasedWhenClosed:NO];
    107 
    108     // Center the window initially before setting the frame autosave name so that the window will be in a good
    109     // position if there is no saved frame yet.
    110     [window center];
    111     [window setFrameAutosaveName:@"Web Inspector 2"];
    112 
    113     WKNSWindowMakeBottomCornersSquare(window);
    114 
    115     NSView *contentView = [window contentView];
    116     [m_inspectorView.get() setFrame:[contentView bounds]];
    117     [m_inspectorView.get() setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
    118     [contentView addSubview:m_inspectorView.get()];
    119 
    120     [window makeKeyAndOrderFront:nil];
    121 
    122     m_inspectorWindow.adoptNS(window);
    123 }
    124 
    125 void WebInspectorProxy::platformClose()
    126 {
    127     // FIXME: support closing in docked mode here.
    128 
    129     [m_inspectorWindow.get() setDelegate:nil];
    130     [m_inspectorWindow.get() orderOut:nil];
    131 
    132     m_inspectorWindow = 0;
    133     m_inspectorView = 0;
    134     m_inspectorProxyObjCAdapter = 0;
    135 }
    136 
    137 void WebInspectorProxy::platformInspectedURLChanged(const String& urlString)
    138 {
    139     NSString *title = [NSString stringWithFormat:WEB_UI_STRING("Web Inspector  %@", "Web Inspector window title"), (NSString *)urlString];
    140     [m_inspectorWindow.get() setTitle:title];
    141 }
    142 
    143 String WebInspectorProxy::inspectorPageURL() const
    144 {
    145     NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.WebCore"] pathForResource:@"inspector" ofType:@"html" inDirectory:@"inspector"];
    146     ASSERT(path);
    147 
    148     return [[NSURL fileURLWithPath:path] absoluteString];
    149 }
    150 
    151 } // namespace WebKit
    152 
    153 #endif // ENABLE(INSPECTOR)
    154