WebKitTools/WebKitTestRunner/InjectedBundle/InjectedBundle.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 "InjectedBundle.h"
       
    27 
       
    28 #include "ActivateFonts.h"
       
    29 #include "InjectedBundlePage.h"
       
    30 #include <WebKit2/WKBundle.h>
       
    31 #include <WebKit2/WKBundlePage.h>
       
    32 #include <WebKit2/WKRetainPtr.h>
       
    33 #include <WebKit2/WKStringCF.h>
       
    34 #include <WebKit2/WebKit2.h>
       
    35 #include <wtf/RetainPtr.h>
       
    36 
       
    37 namespace WTR {
       
    38 
       
    39 InjectedBundle& InjectedBundle::shared()
       
    40 {
       
    41     static InjectedBundle& shared = *new InjectedBundle;
       
    42     return shared;
       
    43 }
       
    44 
       
    45 InjectedBundle::InjectedBundle()
       
    46     : m_bundle(0)
       
    47 {
       
    48 }
       
    49 
       
    50 void InjectedBundle::_didCreatePage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
       
    51 {
       
    52     static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didCreatePage(page);
       
    53 }
       
    54 
       
    55 void InjectedBundle::_willDestroyPage(WKBundleRef bundle, WKBundlePageRef page, const void* clientInfo)
       
    56 {
       
    57     static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->willDestroyPage(page);
       
    58 }
       
    59 
       
    60 void InjectedBundle::_didRecieveMessage(WKBundleRef bundle, WKStringRef message, const void *clientInfo)
       
    61 {
       
    62     static_cast<InjectedBundle*>(const_cast<void*>(clientInfo))->didRecieveMessage(message);
       
    63 }
       
    64 
       
    65 void InjectedBundle::initialize(WKBundleRef bundle)
       
    66 {
       
    67     m_bundle = bundle;
       
    68 
       
    69     WKBundleClient client = {
       
    70         0,
       
    71         this,
       
    72         _didCreatePage,
       
    73         _willDestroyPage,
       
    74         _didRecieveMessage
       
    75     };
       
    76     WKBundleSetClient(m_bundle, &client);
       
    77 
       
    78     activateFonts();
       
    79 }
       
    80 
       
    81 void InjectedBundle::done()
       
    82 {
       
    83     std::string output = m_outputStream.str();
       
    84     RetainPtr<CFStringRef> outputCFString(AdoptCF, CFStringCreateWithCString(0, output.c_str(), kCFStringEncodingUTF8));
       
    85     WKRetainPtr<WKStringRef> doneMessage(AdoptWK, WKStringCreateWithCFString(outputCFString.get()));
       
    86     WKBundlePostMessage(m_bundle, doneMessage.get());
       
    87 }
       
    88 
       
    89 void InjectedBundle::didCreatePage(WKBundlePageRef page)
       
    90 {
       
    91     // FIXME: we really need the main page ref to be sent over from the ui process
       
    92     m_mainPage = new InjectedBundlePage(page);
       
    93     m_pages.add(page, m_mainPage);
       
    94 }
       
    95 
       
    96 void InjectedBundle::willDestroyPage(WKBundlePageRef page)
       
    97 {
       
    98     delete m_pages.take(page);
       
    99 }
       
   100 
       
   101 void InjectedBundle::didRecieveMessage(WKStringRef message)
       
   102 {
       
   103     CFStringRef cfMessage = WKStringCopyCFString(0, message);
       
   104     if (CFEqual(cfMessage, CFSTR("BeginTest"))) {
       
   105         WKRetainPtr<WKStringRef> ackMessage(AdoptWK, WKStringCreateWithCFString(CFSTR("BeginTestAck")));
       
   106         WKBundlePostMessage(m_bundle, ackMessage.get());
       
   107 
       
   108         reset();
       
   109         return;
       
   110     }
       
   111 
       
   112     WKRetainPtr<WKStringRef> errorMessage(AdoptWK, WKStringCreateWithCFString(CFSTR("Error: Unknown.")));
       
   113     WKBundlePostMessage(m_bundle, errorMessage.get());
       
   114 }
       
   115 
       
   116 void InjectedBundle::reset()
       
   117 {
       
   118     m_outputStream.str("");
       
   119     m_layoutTestController = LayoutTestController::create(std::string(""));
       
   120 }
       
   121 
       
   122 } // namespace WTR