WebKitTools/MiniBrowser/win/BrowserView.cpp
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 #include "StdAfx.h"
       
    27 #include "BrowserView.h"
       
    28 
       
    29 #include "BrowserWindow.h"
       
    30 #include <WebKit2/WKContextPrivate.h>
       
    31 #include <WebKit2/WKURLCF.h>
       
    32 
       
    33 static const unsigned short HIGH_BIT_MASK_SHORT = 0x8000;
       
    34 
       
    35 BrowserView::BrowserView()
       
    36     : m_webView(0)
       
    37 {
       
    38 }
       
    39 
       
    40 // UI Client Callbacks
       
    41 
       
    42 static WKPageRef createNewPage(WKPageRef page, const void* clientInfo)
       
    43 {
       
    44     BrowserWindow* browserWindow = BrowserWindow::create();
       
    45     browserWindow->createWindow(0, 0, 800, 600);
       
    46 
       
    47     return WKViewGetPage(browserWindow->view().webView());
       
    48 }
       
    49 
       
    50 static void showPage(WKPageRef page, const void *clientInfo)
       
    51 {
       
    52     static_cast<BrowserWindow*>(const_cast<void*>(clientInfo))->showWindow();
       
    53 }
       
    54 
       
    55 static void closePage(WKPageRef page, const void *clientInfo)
       
    56 {
       
    57 }
       
    58 
       
    59 static void runJavaScriptAlert(WKPageRef page, WKStringRef alertText, WKFrameRef frame, const void* clientInfo)
       
    60 {
       
    61 }
       
    62 
       
    63 
       
    64 void BrowserView::create(RECT webViewRect, BrowserWindow* parentWindow)
       
    65 {
       
    66     assert(!m_webView);
       
    67 
       
    68     bool isShiftKeyDown = ::GetKeyState(VK_SHIFT) & HIGH_BIT_MASK_SHORT;
       
    69 
       
    70     WKContextRef context;
       
    71     if (isShiftKeyDown)
       
    72         context = WKContextGetSharedThreadContext();
       
    73     else
       
    74         context = WKContextGetSharedProcessContext();
       
    75 
       
    76     WKPageNamespaceRef pageNamespace = WKPageNamespaceCreate(context);
       
    77 
       
    78     m_webView = WKViewCreate(webViewRect, pageNamespace, parentWindow->window());
       
    79 
       
    80     WKPageUIClient uiClient = {
       
    81         0,              /* version */
       
    82         parentWindow,   /* clientInfo */
       
    83         createNewPage,
       
    84         showPage,
       
    85         closePage,
       
    86         runJavaScriptAlert
       
    87     };
       
    88     WKPageSetPageUIClient(WKViewGetPage(m_webView), &uiClient);
       
    89 }
       
    90 
       
    91 void BrowserView::setFrame(RECT rect)
       
    92 {
       
    93     HWND webViewWindow = WKViewGetWindow(m_webView);
       
    94     ::SetWindowPos(webViewWindow, 0, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE | SWP_NOCOPYBITS);
       
    95 }
       
    96 
       
    97 void BrowserView::goToURL(const std::wstring& urlString)
       
    98 {
       
    99     CFStringRef string = CFStringCreateWithCharacters(0, (const UniChar*)urlString.data(), urlString.size());
       
   100     CFURLRef cfURL = CFURLCreateWithString(0, string, 0);
       
   101     CFRelease(string);
       
   102 
       
   103     WKURLRef url = WKURLCreateWithCFURL(cfURL);
       
   104     CFRelease(cfURL); 
       
   105 
       
   106     WKPageRef page = WKViewGetPage(m_webView);
       
   107     WKPageLoadURL(page, url);
       
   108     WKURLRelease(url);
       
   109 }