Move resumeAnimations to use Internals interface
[WebKit-https.git] / Tools / DumpRenderTree / win / LayoutTestControllerWin.cpp
1 /*
2  * Copyright (C) 2006, 2007, 2008, 2009, 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  *
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 #include "config.h"
30 #include "LayoutTestController.h"
31
32 #include "DumpRenderTree.h"
33 #include "EditingDelegate.h"
34 #include "PolicyDelegate.h"
35 #include "WorkQueue.h"
36 #include "WorkQueueItem.h"
37 #include <CoreFoundation/CoreFoundation.h>
38 #include <JavaScriptCore/JSRetainPtr.h>
39 #include <JavaScriptCore/JSStringRefBSTR.h>
40 #include <JavaScriptCore/JavaScriptCore.h>
41 #include <WebCore/COMPtr.h>
42 #include <WebKit/WebKit.h>
43 #include <WebKit/WebKitCOMAPI.h>
44 #include <comutil.h>
45 #include <shlwapi.h>
46 #include <shlguid.h>
47 #include <shobjidl.h>
48 #include <string>
49 #include <wtf/Assertions.h>
50 #include <wtf/Platform.h>
51 #include <wtf/RetainPtr.h>
52 #include <wtf/Vector.h>
53
54 using std::string;
55 using std::wstring;
56
57 static bool resolveCygwinPath(const wstring& cygwinPath, wstring& windowsPath);
58
59 LayoutTestController::~LayoutTestController()
60 {
61     COMPtr<IWebView> webView;
62     if (FAILED(frame->webView(&webView)))
63         return;
64
65     // reset webview-related states back to default values in preparation for next test
66
67     COMPtr<IWebViewPrivate> viewPrivate;
68     if (SUCCEEDED(webView->QueryInterface(&viewPrivate)))
69         viewPrivate->setTabKeyCyclesThroughElements(TRUE);
70
71     COMPtr<IWebViewEditing> viewEditing;
72     if (FAILED(webView->QueryInterface(&viewEditing)))
73         return;
74     COMPtr<IWebEditingDelegate> delegate;
75     if (FAILED(viewEditing->editingDelegate(&delegate)))
76         return;
77     COMPtr<EditingDelegate> editingDelegate(Query, viewEditing.get());
78     if (editingDelegate)
79         editingDelegate->setAcceptsEditing(TRUE);
80 }
81
82 void LayoutTestController::addDisallowedURL(JSStringRef url)
83 {
84     // FIXME: Implement!
85 }
86
87 void LayoutTestController::clearBackForwardList()
88 {
89     COMPtr<IWebView> webView;
90     if (FAILED(frame->webView(&webView)))
91         return;
92
93     COMPtr<IWebBackForwardList> backForwardList;
94     if (FAILED(webView->backForwardList(&backForwardList)))
95         return;
96
97     COMPtr<IWebHistoryItem> item;
98     if (FAILED(backForwardList->currentItem(&item)))
99         return;
100
101     // We clear the history by setting the back/forward list's capacity to 0
102     // then restoring it back and adding back the current item.
103     int capacity;
104     if (FAILED(backForwardList->capacity(&capacity)))
105         return;
106
107     backForwardList->setCapacity(0);
108     backForwardList->setCapacity(capacity);
109     backForwardList->addItem(item.get());
110     backForwardList->goToItem(item.get());
111 }
112
113 bool LayoutTestController::callShouldCloseOnWebView()
114 {
115     COMPtr<IWebView> webView;
116     if (FAILED(frame->webView(&webView)))
117         return false;
118
119     COMPtr<IWebViewPrivate> viewPrivate;
120     if (FAILED(webView->QueryInterface(&viewPrivate)))
121         return false;
122
123     BOOL result;
124     viewPrivate->shouldClose(&result);
125     return result;
126 }
127
128 JSStringRef LayoutTestController::copyDecodedHostName(JSStringRef name)
129 {
130     // FIXME: Implement!
131     return 0;
132 }
133
134 JSStringRef LayoutTestController::copyEncodedHostName(JSStringRef name)
135 {
136     // FIXME: Implement!
137     return 0;
138 }
139
140 void LayoutTestController::disableImageLoading()
141 {
142     COMPtr<IWebView> webView;
143     if (FAILED(frame->webView(&webView)))
144         return;
145     
146     COMPtr<IWebPreferences> preferences;
147     if (FAILED(webView->preferences(&preferences)))
148         return;
149     
150     preferences->setLoadsImagesAutomatically(FALSE);
151 }
152
153 void LayoutTestController::dispatchPendingLoadRequests()
154 {
155     // FIXME: Implement for testing fix for 6727495
156 }
157
158 void LayoutTestController::display()
159 {
160     displayWebView();
161 }
162
163 void LayoutTestController::keepWebHistory()
164 {
165     COMPtr<IWebHistory> history;
166     if (FAILED(WebKitCreateInstance(CLSID_WebHistory, 0, __uuidof(history), reinterpret_cast<void**>(&history))))
167         return;
168
169     COMPtr<IWebHistory> sharedHistory;
170     if (FAILED(WebKitCreateInstance(CLSID_WebHistory, 0, __uuidof(sharedHistory), reinterpret_cast<void**>(&sharedHistory))))
171         return;
172
173     history->setOptionalSharedHistory(sharedHistory.get());
174 }
175
176 JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSContextRef context, JSValueRef value)
177 {
178     // FIXME: Implement this.
179     return JSValueMakeUndefined(context);
180 }
181
182 JSRetainPtr<JSStringRef> LayoutTestController::layerTreeAsText() const
183 {
184     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
185     if (!framePrivate)
186         return false;
187
188     BSTR textBSTR = 0;
189     HRESULT hr = framePrivate->layerTreeAsText(&textBSTR);
190
191     wstring text(textBSTR, SysStringLen(textBSTR));
192     SysFreeString(textBSTR);
193     JSRetainPtr<JSStringRef> textValueJS(Adopt, JSStringCreateWithCharacters(text.data(), text.length()));
194     return textValueJS;
195 }
196
197 JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSContextRef context, JSValueRef nodeObject) const
198 {
199     COMPtr<IWebView> webView;
200     if (FAILED(frame->webView(&webView)))
201         return 0;
202
203     COMPtr<IWebViewPrivate> webViewPrivate(Query, webView);
204     if (!webViewPrivate)
205         return 0;
206
207     COMPtr<IDOMElement> element;
208     if (FAILED(webViewPrivate->elementFromJS(context, nodeObject, &element)))
209         return 0;
210
211     COMPtr<IDOMElementPrivate> elementPrivate(Query, element);
212     if (!elementPrivate)
213         return 0;
214
215     BSTR textBSTR = 0;
216     if (FAILED(elementPrivate->markerTextForListItem(&textBSTR)))
217         return 0;
218
219     JSRetainPtr<JSStringRef> markerText(Adopt, JSStringCreateWithBSTR(textBSTR));
220     SysFreeString(textBSTR);
221     return markerText;
222 }
223
224 void LayoutTestController::waitForPolicyDelegate()
225 {
226     COMPtr<IWebView> webView;
227     if (FAILED(frame->webView(&webView)))
228         return;
229
230     setWaitToDump(true);
231     policyDelegate->setControllerToNotifyDone(this);
232     webView->setPolicyDelegate(policyDelegate);
233 }
234
235 size_t LayoutTestController::webHistoryItemCount()
236 {
237     COMPtr<IWebHistory> history;
238     if (FAILED(WebKitCreateInstance(CLSID_WebHistory, 0, __uuidof(history), reinterpret_cast<void**>(&history))))
239         return 0;
240
241     COMPtr<IWebHistory> sharedHistory;
242     if (FAILED(history->optionalSharedHistory(&sharedHistory)) || !sharedHistory)
243         return 0;
244
245     COMPtr<IWebHistoryPrivate> sharedHistoryPrivate;
246     if (FAILED(sharedHistory->QueryInterface(&sharedHistoryPrivate)))
247         return 0;
248
249     int count;
250     if (FAILED(sharedHistoryPrivate->allItems(&count, 0)))
251         return 0;
252
253     return count;
254 }
255
256 unsigned LayoutTestController::workerThreadCount() const
257 {
258     COMPtr<IWebWorkersPrivate> workers;
259     if (FAILED(WebKitCreateInstance(CLSID_WebWorkersPrivate, 0, __uuidof(workers), reinterpret_cast<void**>(&workers))))
260         return 0;
261     unsigned count;
262     if (FAILED(workers->workerThreadCount(&count)))
263         return 0;
264     return count;
265 }
266
267 JSRetainPtr<JSStringRef> LayoutTestController::platformName() const
268 {
269     JSRetainPtr<JSStringRef> platformName(Adopt, JSStringCreateWithUTF8CString("win"));
270     return platformName;
271 }
272
273 void LayoutTestController::notifyDone()
274 {
275     // Same as on mac.  This can be shared.
276     if (m_waitToDump && !topLoadingFrame && !WorkQueue::shared()->count())
277         dump();
278     m_waitToDump = false;
279 }
280
281 JSStringRef LayoutTestController::pathToLocalResource(JSContextRef context, JSStringRef url)
282 {
283     wstring input(JSStringGetCharactersPtr(url), JSStringGetLength(url));
284
285     wstring localPath;
286     if (!resolveCygwinPath(input, localPath)) {
287         printf("ERROR: Failed to resolve Cygwin path %S\n", input.c_str());
288         return 0;
289     }
290
291     return JSStringCreateWithCharacters(localPath.c_str(), localPath.length());
292 }
293
294 static wstring jsStringRefToWString(JSStringRef jsStr)
295 {
296     size_t length = JSStringGetLength(jsStr);
297     Vector<WCHAR> buffer(length + 1);
298     memcpy(buffer.data(), JSStringGetCharactersPtr(jsStr), length * sizeof(WCHAR));
299     buffer[length] = '\0';
300
301     return buffer.data();
302 }
303
304 void LayoutTestController::queueLoad(JSStringRef url, JSStringRef target)
305 {
306     COMPtr<IWebDataSource> dataSource;
307     if (FAILED(frame->dataSource(&dataSource)))
308         return;
309
310     COMPtr<IWebURLResponse> response;
311     if (FAILED(dataSource->response(&response)) || !response)
312         return;
313
314     BSTR responseURLBSTR;
315     if (FAILED(response->URL(&responseURLBSTR)))
316         return;
317     wstring responseURL(responseURLBSTR, SysStringLen(responseURLBSTR));
318     SysFreeString(responseURLBSTR);
319
320     // FIXME: We should do real relative URL resolution here.
321     int lastSlash = responseURL.rfind('/');
322     if (lastSlash != -1)
323         responseURL = responseURL.substr(0, lastSlash);
324
325     wstring wURL = jsStringRefToWString(url);
326     wstring wAbsoluteURL = responseURL + TEXT("/") + wURL;
327     JSRetainPtr<JSStringRef> jsAbsoluteURL(Adopt, JSStringCreateWithCharacters(wAbsoluteURL.data(), wAbsoluteURL.length()));
328
329     WorkQueue::shared()->queue(new LoadItem(jsAbsoluteURL.get(), target));
330 }
331
332 void LayoutTestController::setAcceptsEditing(bool acceptsEditing)
333 {
334     COMPtr<IWebView> webView;
335     if (FAILED(frame->webView(&webView)))
336         return;
337
338     COMPtr<IWebViewEditing> viewEditing;
339     if (FAILED(webView->QueryInterface(&viewEditing)))
340         return;
341
342     COMPtr<IWebEditingDelegate> delegate;
343     if (FAILED(viewEditing->editingDelegate(&delegate)))
344         return;
345
346     EditingDelegate* editingDelegate = (EditingDelegate*)(IWebEditingDelegate*)delegate.get();
347     editingDelegate->setAcceptsEditing(acceptsEditing);
348 }
349
350 void LayoutTestController::setAlwaysAcceptCookies(bool alwaysAcceptCookies)
351 {
352     if (alwaysAcceptCookies == m_alwaysAcceptCookies)
353         return;
354
355     if (!::setAlwaysAcceptCookies(alwaysAcceptCookies))
356         return;
357     m_alwaysAcceptCookies = alwaysAcceptCookies;
358 }
359
360 void LayoutTestController::setAuthorAndUserStylesEnabled(bool flag)
361 {
362     COMPtr<IWebView> webView;
363     if (FAILED(frame->webView(&webView)))
364         return;
365
366     COMPtr<IWebPreferences> preferences;
367     if (FAILED(webView->preferences(&preferences)))
368         return;
369
370     COMPtr<IWebPreferencesPrivate> prefsPrivate(Query, preferences);
371     if (!prefsPrivate)
372         return;
373
374     prefsPrivate->setAuthorAndUserStylesEnabled(flag);
375 }
376
377 void LayoutTestController::setAutofilled(JSContextRef context, JSValueRef nodeObject, bool autofilled)
378 {
379     COMPtr<IWebView> webView;
380     if (FAILED(frame->webView(&webView)))
381         return;
382
383     COMPtr<IWebViewPrivate> webViewPrivate(Query, webView);
384     if (!webViewPrivate)
385         return;
386
387     COMPtr<IDOMElement> element;
388     if (FAILED(webViewPrivate->elementFromJS(context, nodeObject, &element)))
389         return;
390
391     COMPtr<IFormsAutoFillTransition> autofillElement(Query, element);
392     if (!autofillElement)
393         return;
394
395     autofillElement->setAutofilled(autofilled);
396 }
397
398 void LayoutTestController::setCustomPolicyDelegate(bool setDelegate, bool permissive)
399 {
400     COMPtr<IWebView> webView;
401     if (FAILED(frame->webView(&webView)))
402         return;
403
404     if (setDelegate) {
405         policyDelegate->setPermissive(permissive);
406         webView->setPolicyDelegate(policyDelegate);
407     } else
408         webView->setPolicyDelegate(0);
409 }
410
411 void LayoutTestController::setMockDeviceOrientation(bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
412 {
413     // FIXME: Implement for DeviceOrientation layout tests.
414     // See https://bugs.webkit.org/show_bug.cgi?id=30335.
415 }
416
417 void LayoutTestController::setMockGeolocationPosition(double latitude, double longitude, double accuracy)
418 {
419     // FIXME: Implement for Geolocation layout tests.
420     // See https://bugs.webkit.org/show_bug.cgi?id=28264.
421 }
422
423 void LayoutTestController::setMockGeolocationError(int code, JSStringRef message)
424 {
425     // FIXME: Implement for Geolocation layout tests.
426     // See https://bugs.webkit.org/show_bug.cgi?id=28264.
427 }
428
429 void LayoutTestController::setGeolocationPermission(bool allow)
430 {
431     // FIXME: Implement for Geolocation layout tests.
432     setGeolocationPermissionCommon(allow);
433 }
434
435 int LayoutTestController::numberOfPendingGeolocationPermissionRequests()
436 {
437     // FIXME: Implement for Geolocation layout tests.
438     return -1;
439 }
440
441 void LayoutTestController::addMockSpeechInputResult(JSStringRef result, double confidence, JSStringRef language)
442 {
443     // FIXME: Implement for speech input layout tests.
444     // See https://bugs.webkit.org/show_bug.cgi?id=39485.
445 }
446
447 void LayoutTestController::setMockSpeechInputDumpRect(bool flag)
448 {
449     // FIXME: Implement for speech input layout tests.
450     // See https://bugs.webkit.org/show_bug.cgi?id=39485.
451 }
452
453 void LayoutTestController::startSpeechInput(JSContextRef inputElement)
454 {
455     // FIXME: Implement for speech input layout tests.
456     // See https://bugs.webkit.org/show_bug.cgi?id=39485.
457 }
458
459 void LayoutTestController::setIconDatabaseEnabled(bool iconDatabaseEnabled)
460 {
461     // See also <rdar://problem/6480108>
462     COMPtr<IWebIconDatabase> iconDatabase;
463     COMPtr<IWebIconDatabase> tmpIconDatabase;
464     if (FAILED(WebKitCreateInstance(CLSID_WebIconDatabase, 0, IID_IWebIconDatabase, (void**)&tmpIconDatabase)))
465         return;
466     if (FAILED(tmpIconDatabase->sharedIconDatabase(&iconDatabase)))
467         return;
468
469     iconDatabase->setEnabled(iconDatabaseEnabled);
470 }
471
472 void LayoutTestController::setMainFrameIsFirstResponder(bool flag)
473 {
474     // FIXME: Implement!
475 }
476
477 void LayoutTestController::setPrivateBrowsingEnabled(bool privateBrowsingEnabled)
478 {
479     COMPtr<IWebView> webView;
480     if (FAILED(frame->webView(&webView)))
481         return;
482
483     COMPtr<IWebPreferences> preferences;
484     if (FAILED(webView->preferences(&preferences)))
485         return;
486
487     preferences->setPrivateBrowsingEnabled(privateBrowsingEnabled);
488 }
489
490 void LayoutTestController::setXSSAuditorEnabled(bool enabled)
491 {
492     COMPtr<IWebView> webView;
493     if (FAILED(frame->webView(&webView)))
494         return;
495
496     COMPtr<IWebPreferences> preferences;
497     if (FAILED(webView->preferences(&preferences)))
498         return;
499
500     COMPtr<IWebPreferencesPrivate> prefsPrivate(Query, preferences);
501     if (!prefsPrivate)
502         return;
503
504     prefsPrivate->setXSSAuditorEnabled(enabled);
505 }
506
507 void LayoutTestController::setFrameFlatteningEnabled(bool enabled)
508 {
509     COMPtr<IWebView> webView;
510     if (FAILED(frame->webView(&webView)))
511         return;
512
513     COMPtr<IWebPreferences> preferences;
514     if (FAILED(webView->preferences(&preferences)))
515         return;
516
517     COMPtr<IWebPreferencesPrivate> prefsPrivate(Query, preferences);
518     if (!prefsPrivate)
519         return;
520
521     prefsPrivate->setFrameFlatteningEnabled(enabled);
522 }
523
524 void LayoutTestController::setSpatialNavigationEnabled(bool enabled)
525 {
526     // FIXME: Implement for SpatialNavigation layout tests.
527 }
528
529 void LayoutTestController::setAllowUniversalAccessFromFileURLs(bool enabled)
530 {
531     COMPtr<IWebView> webView;
532     if (FAILED(frame->webView(&webView)))
533         return;
534
535     COMPtr<IWebPreferences> preferences;
536     if (FAILED(webView->preferences(&preferences)))
537         return;
538
539     COMPtr<IWebPreferencesPrivate> prefsPrivate(Query, preferences);
540     if (!prefsPrivate)
541         return;
542
543     prefsPrivate->setAllowUniversalAccessFromFileURLs(enabled);
544 }
545
546 void LayoutTestController::setAllowFileAccessFromFileURLs(bool enabled)
547 {
548     COMPtr<IWebView> webView;
549     if (FAILED(frame->webView(&webView)))
550         return;
551
552     COMPtr<IWebPreferences> preferences;
553     if (FAILED(webView->preferences(&preferences)))
554         return;
555
556     COMPtr<IWebPreferencesPrivate> prefsPrivate(Query, preferences);
557     if (!prefsPrivate)
558         return;
559
560     prefsPrivate->setAllowFileAccessFromFileURLs(enabled);
561 }
562
563 void LayoutTestController::setPopupBlockingEnabled(bool enabled)
564 {
565     COMPtr<IWebView> webView;
566     if (FAILED(frame->webView(&webView)))
567         return;
568
569     COMPtr<IWebPreferences> preferences;
570     if (FAILED(webView->preferences(&preferences)))
571         return;
572
573     preferences->setJavaScriptCanOpenWindowsAutomatically(!enabled);
574 }
575
576 void LayoutTestController::setPluginsEnabled(bool flag)
577 {
578     // FIXME: Implement
579 }
580
581 void LayoutTestController::setJavaScriptCanAccessClipboard(bool enabled)
582 {
583     COMPtr<IWebView> webView;
584     if (FAILED(frame->webView(&webView)))
585         return;
586
587     COMPtr<IWebPreferences> preferences;
588     if (FAILED(webView->preferences(&preferences)))
589         return;
590
591     COMPtr<IWebPreferencesPrivate> prefsPrivate(Query, preferences);
592     if (!prefsPrivate)
593         return;
594
595     prefsPrivate->setJavaScriptCanAccessClipboard(enabled);
596 }
597
598 void LayoutTestController::setTabKeyCyclesThroughElements(bool shouldCycle)
599 {
600     COMPtr<IWebView> webView;
601     if (FAILED(frame->webView(&webView)))
602         return;
603
604     COMPtr<IWebViewPrivate> viewPrivate;
605     if (FAILED(webView->QueryInterface(&viewPrivate)))
606         return;
607
608     viewPrivate->setTabKeyCyclesThroughElements(shouldCycle ? TRUE : FALSE);
609 }
610
611 void LayoutTestController::setUseDashboardCompatibilityMode(bool flag)
612 {
613     // FIXME: Implement!
614 }
615
616 void LayoutTestController::setUserStyleSheetEnabled(bool flag)
617 {
618     COMPtr<IWebView> webView;
619     if (FAILED(frame->webView(&webView)))
620         return;
621
622     COMPtr<IWebPreferences> preferences;
623     if (FAILED(webView->preferences(&preferences)))
624         return;
625
626    preferences->setUserStyleSheetEnabled(flag);
627 }
628
629 bool appendComponentToPath(wstring& path, const wstring& component)
630 {
631     WCHAR buffer[MAX_PATH];
632
633     if (path.size() + 1 > MAX_PATH)
634         return false;
635
636     memcpy(buffer, path.data(), path.size() * sizeof(WCHAR));
637     buffer[path.size()] = '\0';
638
639     if (!PathAppendW(buffer, component.c_str()))
640         return false;
641
642     path = wstring(buffer);
643     return true;
644 }
645
646 static bool followShortcuts(wstring& path)
647 {
648     if (PathFileExists(path.c_str()))
649         return true;
650
651     // Do we have a shortcut?
652     wstring linkPath = path;
653     linkPath.append(TEXT(".lnk"));
654     if (!PathFileExists(linkPath.c_str()))
655        return true;
656
657     // We have a shortcut, find its target.
658     COMPtr<IShellLink> shortcut(Create, CLSID_ShellLink);
659     if (!shortcut)
660        return false;
661     COMPtr<IPersistFile> persistFile(Query, shortcut);
662     if (!shortcut)
663         return false;
664     if (FAILED(persistFile->Load(linkPath.c_str(), STGM_READ)))
665         return false;
666     if (FAILED(shortcut->Resolve(0, 0)))
667         return false;
668     WCHAR targetPath[MAX_PATH];
669     DWORD targetPathLen = _countof(targetPath);
670     if (FAILED(shortcut->GetPath(targetPath, targetPathLen, 0, 0)))
671         return false;
672     if (!PathFileExists(targetPath))
673         return false;
674     // Use the target path as the result path instead.
675     path = wstring(targetPath);
676
677     return true;
678 }
679
680 static bool resolveCygwinPath(const wstring& cygwinPath, wstring& windowsPath)
681 {
682     wstring fileProtocol = L"file://";
683     bool isFileProtocol = cygwinPath.find(fileProtocol) != string::npos;
684     if (cygwinPath[isFileProtocol ? 7 : 0] != '/')  // ensure path is absolute
685         return false;
686
687     // Get the Root path.
688     WCHAR rootPath[MAX_PATH];
689     DWORD rootPathSize = _countof(rootPath);
690     DWORD keyType;
691     DWORD result = ::SHGetValueW(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Cygnus Solutions\\Cygwin\\mounts v2\\/"), TEXT("native"), &keyType, &rootPath, &rootPathSize);
692
693     if (result != ERROR_SUCCESS || keyType != REG_SZ) {
694         // Cygwin 1.7 doesn't store Cygwin's root as a mount point anymore, because mount points are now stored in /etc/fstab.
695         // However, /etc/fstab doesn't contain any information about where / is located as a Windows path, so we need to use Cygwin's
696         // new registry key that has the root.
697         result = ::SHGetValueW(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Cygwin\\setup"), TEXT("rootdir"), &keyType, &rootPath, &rootPathSize);
698         if (result != ERROR_SUCCESS || keyType != REG_SZ)
699             return false;
700     }
701
702     windowsPath = wstring(rootPath, rootPathSize);
703
704     int oldPos = isFileProtocol ? 8 : 1;
705     while (1) {
706         int newPos = cygwinPath.find('/', oldPos);
707
708         if (newPos == -1) {
709             wstring pathComponent = cygwinPath.substr(oldPos);
710
711             if (!appendComponentToPath(windowsPath, pathComponent))
712                return false;
713
714             if (!followShortcuts(windowsPath))
715                 return false;
716
717             break;
718         }
719
720         wstring pathComponent = cygwinPath.substr(oldPos, newPos - oldPos);
721         if (!appendComponentToPath(windowsPath, pathComponent))
722             return false;
723
724         if (!followShortcuts(windowsPath))
725             return false;
726
727         oldPos = newPos + 1;
728     }
729
730     if (isFileProtocol)
731         windowsPath = fileProtocol + windowsPath;
732
733     return true;
734 }
735
736 void LayoutTestController::setUserStyleSheetLocation(JSStringRef jsURL)
737 {
738     COMPtr<IWebView> webView;
739     if (FAILED(frame->webView(&webView)))
740         return;
741
742     COMPtr<IWebPreferences> preferences;
743     if (FAILED(webView->preferences(&preferences)))
744         return;
745
746     RetainPtr<CFStringRef> urlString(AdoptCF, JSStringCopyCFString(0, jsURL));
747     RetainPtr<CFURLRef> url(AdoptCF, CFURLCreateWithString(0, urlString.get(), 0));
748     if (!url)
749         return;
750
751     // Now copy the file system path, POSIX style.
752     RetainPtr<CFStringRef> pathCF(AdoptCF, CFURLCopyFileSystemPath(url.get(), kCFURLPOSIXPathStyle));
753     if (!pathCF)
754         return;
755
756     wstring path = cfStringRefToWString(pathCF.get());
757
758     wstring resultPath;
759     if (!resolveCygwinPath(path, resultPath))
760         return;
761
762     // The path has been resolved, now convert it back to a CFURL.
763     int result = WideCharToMultiByte(CP_UTF8, 0, resultPath.c_str(), resultPath.size() + 1, 0, 0, 0, 0);
764     Vector<char> utf8Vector(result);
765     result = WideCharToMultiByte(CP_UTF8, 0, resultPath.c_str(), resultPath.size() + 1, utf8Vector.data(), result, 0, 0);
766     if (!result)
767         return;
768
769     url = CFURLCreateFromFileSystemRepresentation(0, (const UInt8*)utf8Vector.data(), utf8Vector.size() - 1, false);
770     if (!url)
771         return;
772
773     resultPath = cfStringRefToWString(CFURLGetString(url.get()));
774
775     BSTR resultPathBSTR = SysAllocStringLen(resultPath.data(), resultPath.size());
776     preferences->setUserStyleSheetLocation(resultPathBSTR);
777     SysFreeString(resultPathBSTR);
778 }
779
780 void LayoutTestController::setValueForUser(JSContextRef context, JSValueRef element, JSStringRef value)
781 {
782     COMPtr<IWebView> webView;
783     if (FAILED(frame->webView(&webView)))
784         return;
785
786     COMPtr<IWebViewPrivate> webViewPrivate(Query, webView);
787     if (!webViewPrivate)
788         return;
789
790     COMPtr<IDOMElement> domElement;
791     if (FAILED(webViewPrivate->elementFromJS(context, element, &domElement)))
792         return;
793
794     COMPtr<IDOMHTMLInputElement> domInputElement;
795     if (FAILED(domElement->QueryInterface(IID_IDOMHTMLInputElement, reinterpret_cast<void**>(&domInputElement))))
796         return;
797
798     _bstr_t valueBSTR(JSStringCopyBSTR(value), false);
799
800     domInputElement->setValueForUser(valueBSTR);
801 }
802
803 void LayoutTestController::setViewModeMediaFeature(JSStringRef mode)
804 {
805     // FIXME: implement
806 }
807
808 void LayoutTestController::setPersistentUserStyleSheetLocation(JSStringRef jsURL)
809 {
810     RetainPtr<CFStringRef> urlString(AdoptCF, JSStringCopyCFString(0, jsURL));
811     ::setPersistentUserStyleSheetLocation(urlString.get());
812 }
813
814 void LayoutTestController::clearPersistentUserStyleSheet()
815 {
816     ::setPersistentUserStyleSheetLocation(0);
817 }
818
819 void LayoutTestController::setWindowIsKey(bool flag)
820 {
821     COMPtr<IWebView> webView;
822     if (FAILED(frame->webView(&webView)))
823         return;
824
825     COMPtr<IWebViewPrivate> viewPrivate;
826     if (FAILED(webView->QueryInterface(&viewPrivate)))
827         return;
828
829     HWND webViewWindow;
830     if (FAILED(viewPrivate->viewWindow((OLE_HANDLE*)&webViewWindow)))
831         return;
832
833     ::SendMessage(webViewWindow, flag ? WM_SETFOCUS : WM_KILLFOCUS, (WPARAM)::GetDesktopWindow(), 0);
834 }
835
836 void LayoutTestController::setSmartInsertDeleteEnabled(bool flag)
837 {
838     COMPtr<IWebView> webView;
839     if (FAILED(frame->webView(&webView)))
840         return;
841
842     COMPtr<IWebViewEditing> viewEditing;
843     if (FAILED(webView->QueryInterface(&viewEditing)))
844         return;
845
846     viewEditing->setSmartInsertDeleteEnabled(flag ? TRUE : FALSE);
847 }
848
849 void LayoutTestController::setJavaScriptProfilingEnabled(bool flag)
850 {
851     COMPtr<IWebView> webView;
852     if (FAILED(frame->webView(&webView)))
853         return;
854
855     COMPtr<IWebViewPrivate> viewPrivate;
856     if (FAILED(webView->QueryInterface(&viewPrivate)))
857         return;
858
859     COMPtr<IWebInspector> inspector;
860     if (FAILED(viewPrivate->inspector(&inspector)))
861         return;
862
863     setDeveloperExtrasEnabled(flag);
864     inspector->setJavaScriptProfilingEnabled(flag);
865 }
866
867 void LayoutTestController::setSelectTrailingWhitespaceEnabled(bool flag)
868 {
869     COMPtr<IWebView> webView;
870     if (FAILED(frame->webView(&webView)))
871         return;
872
873     COMPtr<IWebViewEditing> viewEditing;
874     if (FAILED(webView->QueryInterface(&viewEditing)))
875         return;
876
877     viewEditing->setSelectTrailingWhitespaceEnabled(flag ? TRUE : FALSE);
878 }
879
880 static const CFTimeInterval waitToDumpWatchdogInterval = 30.0;
881
882 static void CALLBACK waitUntilDoneWatchdogFired(HWND, UINT, UINT_PTR, DWORD)
883 {
884     gLayoutTestController->waitToDumpWatchdogTimerFired();
885 }
886
887 void LayoutTestController::setWaitToDump(bool waitUntilDone)
888 {
889     m_waitToDump = waitUntilDone;
890     if (m_waitToDump && !waitToDumpWatchdog)
891         waitToDumpWatchdog = SetTimer(0, 0, waitToDumpWatchdogInterval * 1000, waitUntilDoneWatchdogFired);
892 }
893
894 int LayoutTestController::windowCount()
895 {
896     return openWindows().size();
897 }
898
899 bool LayoutTestController::elementDoesAutoCompleteForElementWithId(JSStringRef id)
900 {
901     COMPtr<IDOMDocument> document;
902     if (FAILED(frame->DOMDocument(&document)))
903         return false;
904
905     wstring idWstring = jsStringRefToWString(id);
906     BSTR idBSTR = SysAllocStringLen((OLECHAR*)idWstring.c_str(), idWstring.length());
907     COMPtr<IDOMElement> element;
908     HRESULT result = document->getElementById(idBSTR, &element);
909     SysFreeString(idBSTR);
910
911     if (FAILED(result))
912         return false;
913
914     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
915     if (!framePrivate)
916         return false;
917
918     BOOL autoCompletes;
919     if (FAILED(framePrivate->elementDoesAutoComplete(element.get(), &autoCompletes)))
920         return false;
921
922     return autoCompletes;
923 }
924
925 void LayoutTestController::execCommand(JSStringRef name, JSStringRef value)
926 {
927     wstring wName = jsStringRefToWString(name);
928     wstring wValue = jsStringRefToWString(value);
929
930     COMPtr<IWebView> webView;
931     if (FAILED(frame->webView(&webView)))
932         return;
933
934     COMPtr<IWebViewPrivate> viewPrivate;
935     if (FAILED(webView->QueryInterface(&viewPrivate)))
936         return;
937
938     BSTR nameBSTR = SysAllocStringLen((OLECHAR*)wName.c_str(), wName.length());
939     BSTR valueBSTR = SysAllocStringLen((OLECHAR*)wValue.c_str(), wValue.length());
940     viewPrivate->executeCoreCommandByName(nameBSTR, valueBSTR);
941
942     SysFreeString(nameBSTR);
943     SysFreeString(valueBSTR);
944 }
945
946 bool LayoutTestController::findString(JSContextRef /* context */, JSStringRef /* target */, JSObjectRef /* optionsArray */)
947 {
948     // FIXME: Implement
949     return false;
950 }
951
952 void LayoutTestController::setCacheModel(int)
953 {
954     // FIXME: Implement
955 }
956
957 bool LayoutTestController::isCommandEnabled(JSStringRef /*name*/)
958 {
959     printf("ERROR: LayoutTestController::isCommandEnabled() not implemented\n");
960     return false;
961 }
962
963 void LayoutTestController::clearAllApplicationCaches()
964 {
965     // FIXME: Implement to support application cache quotas.
966 }
967
968 void LayoutTestController::clearApplicationCacheForOrigin(JSStringRef origin)
969 {
970     // FIXME: Implement to support deleting all application cache for an origin.
971 }
972
973 void LayoutTestController::setApplicationCacheOriginQuota(unsigned long long quota)
974 {
975     // FIXME: Implement to support application cache quotas.
976 }
977
978 JSValueRef LayoutTestController::originsWithApplicationCache(JSContextRef context)
979 {
980     // FIXME: Implement to get origins that have application caches.
981     return JSValueMakeUndefined(context);
982 }
983
984 long long LayoutTestController::applicationCacheDiskUsageForOrigin(JSStringRef name)
985 {
986     // FIXME: Implement to get disk usage by all application caches for an origin.
987     return 0;
988 }
989
990 void LayoutTestController::clearAllDatabases()
991 {
992     COMPtr<IWebDatabaseManager> databaseManager;
993     COMPtr<IWebDatabaseManager> tmpDatabaseManager;
994     if (FAILED(WebKitCreateInstance(CLSID_WebDatabaseManager, 0, IID_IWebDatabaseManager, (void**)&tmpDatabaseManager)))
995         return;
996     if (FAILED(tmpDatabaseManager->sharedWebDatabaseManager(&databaseManager)))
997         return;
998
999     databaseManager->deleteAllDatabases();
1000 }
1001
1002 void LayoutTestController::overridePreference(JSStringRef key, JSStringRef value)
1003 {
1004     COMPtr<IWebView> webView;
1005     if (FAILED(frame->webView(&webView)))
1006         return;
1007
1008     COMPtr<IWebPreferences> preferences;
1009     if (FAILED(webView->preferences(&preferences)))
1010         return;
1011
1012     COMPtr<IWebPreferencesPrivate> prefsPrivate(Query, preferences);
1013     if (!prefsPrivate)
1014         return;
1015
1016     BSTR keyBSTR = JSStringCopyBSTR(key);
1017     BSTR valueBSTR = JSStringCopyBSTR(value);
1018     prefsPrivate->setPreferenceForTest(keyBSTR, valueBSTR);
1019     SysFreeString(keyBSTR);
1020     SysFreeString(valueBSTR);
1021 }
1022
1023 void LayoutTestController::setDatabaseQuota(unsigned long long quota)
1024 {
1025     COMPtr<IWebDatabaseManager> databaseManager;
1026     COMPtr<IWebDatabaseManager> tmpDatabaseManager;
1027
1028     if (FAILED(WebKitCreateInstance(CLSID_WebDatabaseManager, 0, IID_IWebDatabaseManager, (void**)&tmpDatabaseManager)))
1029         return;
1030     if (FAILED(tmpDatabaseManager->sharedWebDatabaseManager(&databaseManager)))
1031         return;
1032
1033     databaseManager->setQuota(TEXT("file:///"), quota);
1034 }
1035
1036 void LayoutTestController::goBack()
1037 {
1038     // FIXME: implement to enable loader/navigation-while-deferring-loads.html
1039 }
1040
1041 void LayoutTestController::setDefersLoading(bool)
1042 {
1043     // FIXME: implement to enable loader/navigation-while-deferring-loads.html
1044 }
1045
1046 void LayoutTestController::setDomainRelaxationForbiddenForURLScheme(bool forbidden, JSStringRef scheme)
1047 {
1048     COMPtr<IWebViewPrivate> webView;
1049     if (FAILED(WebKitCreateInstance(__uuidof(WebView), 0, __uuidof(webView), reinterpret_cast<void**>(&webView))))
1050         return;
1051
1052     BSTR schemeBSTR = JSStringCopyBSTR(scheme);
1053     webView->setDomainRelaxationForbiddenForURLScheme(forbidden, schemeBSTR);
1054     SysFreeString(schemeBSTR);
1055 }
1056
1057 void LayoutTestController::setAppCacheMaximumSize(unsigned long long size)
1058 {
1059     printf("ERROR: LayoutTestController::setAppCacheMaximumSize() not implemented\n");
1060 }
1061
1062 bool LayoutTestController::pauseAnimationAtTimeOnElementWithId(JSStringRef animationName, double time, JSStringRef elementId)
1063 {
1064     COMPtr<IDOMDocument> document;
1065     if (FAILED(frame->DOMDocument(&document)))
1066         return false;
1067
1068     BSTR idBSTR = JSStringCopyBSTR(elementId);
1069     COMPtr<IDOMElement> element;
1070     HRESULT hr = document->getElementById(idBSTR, &element);
1071     SysFreeString(idBSTR);
1072     if (FAILED(hr))
1073         return false;
1074
1075     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1076     if (!framePrivate)
1077         return false;
1078
1079     BSTR nameBSTR = JSStringCopyBSTR(animationName);
1080     BOOL wasRunning = FALSE;
1081     hr = framePrivate->pauseAnimation(nameBSTR, element.get(), time, &wasRunning);
1082     SysFreeString(nameBSTR);
1083
1084     return SUCCEEDED(hr) && wasRunning;
1085 }
1086
1087 bool LayoutTestController::pauseTransitionAtTimeOnElementWithId(JSStringRef propertyName, double time, JSStringRef elementId)
1088 {
1089     COMPtr<IDOMDocument> document;
1090     if (FAILED(frame->DOMDocument(&document)))
1091         return false;
1092
1093     BSTR idBSTR = JSStringCopyBSTR(elementId);
1094     COMPtr<IDOMElement> element;
1095     HRESULT hr = document->getElementById(idBSTR, &element);
1096     SysFreeString(idBSTR);
1097     if (FAILED(hr))
1098         return false;
1099
1100     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1101     if (!framePrivate)
1102         return false;
1103
1104     BSTR nameBSTR = JSStringCopyBSTR(propertyName);
1105     BOOL wasRunning = FALSE;
1106     hr = framePrivate->pauseTransition(nameBSTR, element.get(), time, &wasRunning);
1107     SysFreeString(nameBSTR);
1108
1109     return SUCCEEDED(hr) && wasRunning;
1110 }
1111
1112 unsigned LayoutTestController::numberOfActiveAnimations() const
1113 {
1114     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1115     if (!framePrivate)
1116         return 0;
1117
1118     UINT number = 0;
1119     if (FAILED(framePrivate->numberOfActiveAnimations(&number)))
1120         return 0;
1121
1122     return number;
1123 }
1124
1125 static _bstr_t bstrT(JSStringRef jsString)
1126 {
1127     // The false parameter tells the _bstr_t constructor to adopt the BSTR we pass it.
1128     return _bstr_t(JSStringCopyBSTR(jsString), false);
1129 }
1130
1131 void LayoutTestController::addOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef destinationProtocol, JSStringRef destinationHost, bool allowDestinationSubdomains)
1132 {
1133     COMPtr<IWebViewPrivate> webView;
1134     if (FAILED(WebKitCreateInstance(__uuidof(WebView), 0, __uuidof(webView), reinterpret_cast<void**>(&webView))))
1135         return;
1136
1137     webView->addOriginAccessWhitelistEntry(bstrT(sourceOrigin).GetBSTR(), bstrT(destinationProtocol).GetBSTR(), bstrT(destinationHost).GetBSTR(), allowDestinationSubdomains);
1138 }
1139
1140 void LayoutTestController::removeOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef destinationProtocol, JSStringRef destinationHost, bool allowDestinationSubdomains)
1141 {
1142     COMPtr<IWebViewPrivate> webView;
1143     if (FAILED(WebKitCreateInstance(__uuidof(WebView), 0, __uuidof(webView), reinterpret_cast<void**>(&webView))))
1144         return;
1145
1146     webView->removeOriginAccessWhitelistEntry(bstrT(sourceOrigin).GetBSTR(), bstrT(destinationProtocol).GetBSTR(), bstrT(destinationHost).GetBSTR(), allowDestinationSubdomains);
1147 }
1148
1149 void LayoutTestController::setScrollbarPolicy(JSStringRef orientation, JSStringRef policy)
1150 {
1151     // FIXME: implement
1152 }
1153
1154 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
1155 {
1156     COMPtr<IWebViewPrivate> webView;
1157     if (FAILED(WebKitCreateInstance(__uuidof(WebView), 0, __uuidof(webView), reinterpret_cast<void**>(&webView))))
1158         return;
1159
1160     COMPtr<IWebScriptWorld> world;
1161     if (FAILED(WebKitCreateInstance(__uuidof(WebScriptWorld), 0, __uuidof(world), reinterpret_cast<void**>(&world))))
1162         return;
1163
1164     webView->addUserScriptToGroup(_bstr_t(L"org.webkit.DumpRenderTree").GetBSTR(), world.get(), bstrT(source).GetBSTR(), 0, 0, 0, 0, 0, runAtStart ? WebInjectAtDocumentStart : WebInjectAtDocumentEnd);
1165 }
1166
1167
1168 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
1169 {
1170     COMPtr<IWebViewPrivate> webView;
1171     if (FAILED(WebKitCreateInstance(__uuidof(WebView), 0, __uuidof(webView), reinterpret_cast<void**>(&webView))))
1172         return;
1173
1174     COMPtr<IWebScriptWorld> world;
1175     if (FAILED(WebKitCreateInstance(__uuidof(WebScriptWorld), 0, __uuidof(world), reinterpret_cast<void**>(&world))))
1176         return;
1177
1178     webView->addUserStyleSheetToGroup(_bstr_t(L"org.webkit.DumpRenderTree").GetBSTR(), world.get(), bstrT(source).GetBSTR(), 0, 0, 0, 0, 0);
1179 }
1180
1181 void LayoutTestController::setDeveloperExtrasEnabled(bool enabled)
1182 {
1183     COMPtr<IWebView> webView;
1184     if (FAILED(frame->webView(&webView)))
1185         return;
1186
1187     COMPtr<IWebPreferences> preferences;
1188     if (FAILED(webView->preferences(&preferences)))
1189         return;
1190
1191     COMPtr<IWebPreferencesPrivate> prefsPrivate(Query, preferences);
1192     if (!prefsPrivate)
1193         return;
1194
1195     prefsPrivate->setDeveloperExtrasEnabled(enabled);
1196 }
1197
1198 void LayoutTestController::setAsynchronousSpellCheckingEnabled(bool)
1199 {
1200     // FIXME: Implement this.
1201 }
1202
1203 void LayoutTestController::showWebInspector()
1204 {
1205     COMPtr<IWebView> webView;
1206     if (FAILED(frame->webView(&webView)))
1207         return;
1208
1209     COMPtr<IWebViewPrivate> viewPrivate(Query, webView);
1210     if (!viewPrivate)
1211         return;
1212
1213     COMPtr<IWebInspector> inspector;
1214     if (SUCCEEDED(viewPrivate->inspector(&inspector)))
1215         inspector->show();
1216 }
1217
1218 void LayoutTestController::closeWebInspector()
1219 {
1220     COMPtr<IWebView> webView;
1221     if (FAILED(frame->webView(&webView)))
1222         return;
1223
1224     COMPtr<IWebViewPrivate> viewPrivate(Query, webView);
1225     if (!viewPrivate)
1226         return;
1227
1228     COMPtr<IWebInspector> inspector;
1229     if (FAILED(viewPrivate->inspector(&inspector)))
1230         return;
1231
1232     inspector->close();
1233 }
1234
1235 void LayoutTestController::evaluateInWebInspector(long callId, JSStringRef script)
1236 {
1237     COMPtr<IWebView> webView;
1238     if (FAILED(frame->webView(&webView)))
1239         return;
1240
1241     COMPtr<IWebViewPrivate> viewPrivate(Query, webView);
1242     if (!viewPrivate)
1243         return;
1244
1245     COMPtr<IWebInspector> inspector;
1246     if (FAILED(viewPrivate->inspector(&inspector)))
1247         return;
1248
1249     COMPtr<IWebInspectorPrivate> inspectorPrivate(Query, inspector);
1250     if (!inspectorPrivate)
1251         return;
1252
1253     inspectorPrivate->evaluateInFrontend(callId, bstrT(script).GetBSTR());
1254 }
1255
1256 typedef HashMap<unsigned, COMPtr<IWebScriptWorld> > WorldMap;
1257 static WorldMap& worldMap()
1258 {
1259     static WorldMap& map = *new WorldMap;
1260     return map;
1261 }
1262
1263 unsigned worldIDForWorld(IWebScriptWorld* world)
1264 {
1265     WorldMap::const_iterator end = worldMap().end();
1266     for (WorldMap::const_iterator it = worldMap().begin(); it != end; ++it) {
1267         if (it->second == world)
1268             return it->first;
1269     }
1270
1271     return 0;
1272 }
1273
1274 void LayoutTestController::evaluateScriptInIsolatedWorldAndReturnValue(unsigned worldID, JSObjectRef globalObject, JSStringRef script)
1275 {
1276     // FIXME: Implement this.
1277 }
1278
1279 void LayoutTestController::evaluateScriptInIsolatedWorld(unsigned worldID, JSObjectRef globalObject, JSStringRef script)
1280 {
1281     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1282     if (!framePrivate)
1283         return;
1284
1285     // A worldID of 0 always corresponds to a new world. Any other worldID corresponds to a world
1286     // that is created once and cached forever.
1287     COMPtr<IWebScriptWorld> world;
1288     if (!worldID) {
1289         if (FAILED(WebKitCreateInstance(__uuidof(WebScriptWorld), 0, __uuidof(world), reinterpret_cast<void**>(&world))))
1290             return;
1291     } else {
1292         COMPtr<IWebScriptWorld>& worldSlot = worldMap().add(worldID, 0).iterator->second;
1293         if (!worldSlot && FAILED(WebKitCreateInstance(__uuidof(WebScriptWorld), 0, __uuidof(worldSlot), reinterpret_cast<void**>(&worldSlot))))
1294             return;
1295         world = worldSlot;
1296     }
1297
1298     BSTR result;
1299     if (FAILED(framePrivate->stringByEvaluatingJavaScriptInScriptWorld(world.get(), globalObject, bstrT(script).GetBSTR(), &result)))
1300         return;
1301     SysFreeString(result);
1302 }
1303
1304 void LayoutTestController::removeAllVisitedLinks()
1305 {
1306     COMPtr<IWebHistory> history;
1307     if (FAILED(WebKitCreateInstance(CLSID_WebHistory, 0, __uuidof(history), reinterpret_cast<void**>(&history))))
1308         return;
1309
1310     COMPtr<IWebHistory> sharedHistory;
1311     if (FAILED(history->optionalSharedHistory(&sharedHistory)) || !sharedHistory)
1312         return;
1313
1314     COMPtr<IWebHistoryPrivate> sharedHistoryPrivate;
1315     if (FAILED(sharedHistory->QueryInterface(&sharedHistoryPrivate)))
1316         return;
1317
1318     sharedHistoryPrivate->removeAllVisitedLinks();
1319 }
1320
1321 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef id)
1322 {
1323     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1324     if (!framePrivate)
1325         return 0;
1326
1327     wstring idWstring = jsStringRefToWString(id);
1328     BSTR idBSTR = SysAllocStringLen((OLECHAR*)idWstring.c_str(), idWstring.length());
1329     BSTR counterValueBSTR;
1330     if (FAILED(framePrivate->counterValueForElementById(idBSTR, &counterValueBSTR)))
1331         return 0;
1332
1333     wstring counterValue(counterValueBSTR, SysStringLen(counterValueBSTR));
1334     SysFreeString(idBSTR);
1335     SysFreeString(counterValueBSTR);
1336     JSRetainPtr<JSStringRef> counterValueJS(Adopt, JSStringCreateWithCharacters(counterValue.data(), counterValue.length()));
1337     return counterValueJS;
1338 }
1339
1340 int LayoutTestController::pageNumberForElementById(JSStringRef id, float pageWidthInPixels, float pageHeightInPixels)
1341 {
1342     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1343     if (!framePrivate)
1344         return 0;
1345
1346     wstring idWstring = jsStringRefToWString(id);
1347     BSTR idBSTR = SysAllocStringLen((OLECHAR*)idWstring.c_str(), idWstring.length());
1348     int pageNumber = -1;
1349     if (FAILED(framePrivate->pageNumberForElementById(idBSTR, pageWidthInPixels, pageHeightInPixels, &pageNumber)))
1350         pageNumber = -1;
1351     SysFreeString(idBSTR);
1352     return pageNumber;
1353 }
1354
1355 int LayoutTestController::numberOfPages(float pageWidthInPixels, float pageHeightInPixels)
1356 {
1357     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1358     if (!framePrivate)
1359         return 0;
1360
1361     int pageNumber = -1;
1362     if (FAILED(framePrivate->numberOfPages(pageWidthInPixels, pageHeightInPixels, &pageNumber)))
1363         pageNumber = -1;
1364     return pageNumber;
1365 }
1366
1367 JSRetainPtr<JSStringRef> LayoutTestController::pageProperty(const char* propertyName, int pageNumber) const
1368 {
1369     // FIXME: Implement this.
1370     return JSRetainPtr<JSStringRef>();
1371 }
1372
1373 void LayoutTestController::apiTestNewWindowDataLoadBaseURL(JSStringRef utf8Data, JSStringRef baseURL)
1374 {
1375
1376 }
1377
1378 JSRetainPtr<JSStringRef> LayoutTestController::pageSizeAndMarginsInPixels(int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft) const
1379 {
1380     // FIXME: implement
1381     return JSRetainPtr<JSStringRef>();
1382 }
1383
1384 void LayoutTestController::apiTestGoToCurrentBackForwardItem()
1385 {
1386     COMPtr<IWebView> webView;
1387     if (FAILED(frame->webView(&webView)))
1388         return;
1389
1390     COMPtr<IWebBackForwardList> backForwardList;
1391     if (FAILED(webView->backForwardList(&backForwardList)))
1392         return;
1393
1394     COMPtr<IWebHistoryItem> item;
1395     if (FAILED(backForwardList->currentItem(&item)))
1396         return;
1397
1398     BOOL success;
1399     webView->goToBackForwardItem(item.get(), &success);
1400 }
1401
1402 void LayoutTestController::setWebViewEditable(bool)
1403 {
1404 }
1405
1406 void LayoutTestController::authenticateSession(JSStringRef, JSStringRef, JSStringRef)
1407 {
1408 }
1409
1410 void LayoutTestController::setEditingBehavior(const char* editingBehavior)
1411 {
1412     COMPtr<IWebView> webView;
1413     if (FAILED(frame->webView(&webView)))
1414         return;
1415
1416     COMPtr<IWebPreferences> preferences;
1417     if (FAILED(webView->preferences(&preferences)))
1418         return;
1419
1420     string behaviorString(editingBehavior);
1421     if (behaviorString == "mac")
1422         preferences->setEditingBehavior(WebKitEditingMacBehavior);
1423     else if (behaviorString == "win")
1424         preferences->setEditingBehavior(WebKitEditingWinBehavior);
1425     else if (behaviorString == "unix")
1426         preferences->setEditingBehavior(WebKitEditingUnixBehavior);
1427 }
1428
1429 void LayoutTestController::abortModal()
1430 {
1431 }
1432
1433 void LayoutTestController::dumpConfigurationForViewport(int /*deviceDPI*/, int /*deviceWidth*/, int /*deviceHeight*/, int /*availableWidth*/, int /*availableHeight*/)
1434 {
1435     // FIXME: Implement this.
1436 }
1437
1438 void LayoutTestController::setSerializeHTTPLoads(bool)
1439 {
1440     // FIXME: Implement.
1441 }
1442
1443 void LayoutTestController::syncLocalStorage()
1444 {
1445     // FIXME: Implement.
1446 }
1447
1448 void LayoutTestController::observeStorageTrackerNotifications(unsigned number)
1449 {
1450     // FIXME: Implement.
1451 }
1452
1453 void LayoutTestController::deleteAllLocalStorage()
1454 {
1455     // FIXME: Implement.
1456 }
1457
1458 JSValueRef LayoutTestController::originsWithLocalStorage(JSContextRef context)
1459 {
1460     // FIXME: Implement.
1461     return JSValueMakeUndefined(context);
1462 }
1463
1464 long long LayoutTestController::localStorageDiskUsageForOrigin(JSStringRef originIdentifier)
1465 {
1466     // FIXME: Implement to support getting local storage disk usage for an origin.
1467     return 0;
1468 }
1469
1470 void LayoutTestController::deleteLocalStorageForOrigin(JSStringRef URL)
1471 {
1472     // FIXME: Implement.
1473 }
1474
1475 void LayoutTestController::setMinimumTimerInterval(double minimumTimerInterval)
1476 {
1477     COMPtr<IWebView> webView;
1478     if (FAILED(frame->webView(&webView)))
1479         return;
1480
1481     COMPtr<IWebViewPrivate> viewPrivate(Query, webView);
1482     if (!viewPrivate)
1483         return;
1484
1485     viewPrivate->setMinimumTimerInterval(minimumTimerInterval);
1486 }
1487
1488 void LayoutTestController::setTextDirection(JSStringRef direction)
1489 {
1490     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1491     if (!framePrivate)
1492         return;
1493
1494     framePrivate->setTextDirection(bstrT(direction).GetBSTR());
1495 }
1496
1497 void LayoutTestController::allowRoundingHacks()
1498 {
1499 }
1500
1501 void LayoutTestController::addChromeInputField()
1502 {
1503 }
1504
1505 void LayoutTestController::removeChromeInputField()
1506 {
1507 }
1508
1509 void LayoutTestController::focusWebView()
1510 {
1511 }
1512
1513 void LayoutTestController::setBackingScaleFactor(double)
1514 {
1515 }
1516
1517 void LayoutTestController::simulateDesktopNotificationClick(JSStringRef title)
1518 {
1519     // FIXME: Implement.
1520 }
1521
1522 void LayoutTestController::resetPageVisibility()
1523 {
1524     // FIXME: Implement this.
1525 }
1526
1527 void LayoutTestController::setPageVisibility(const char*)
1528 {
1529     // FIXME: Implement this.
1530 }