67bc4a711bcd9a7c69741089bab0f7b0c8854f69
[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 void LayoutTestController::resumeAnimations() const
1126 {
1127     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1128     if (!framePrivate)
1129         return;
1130
1131     framePrivate->resumeAnimations();
1132 }
1133
1134 static _bstr_t bstrT(JSStringRef jsString)
1135 {
1136     // The false parameter tells the _bstr_t constructor to adopt the BSTR we pass it.
1137     return _bstr_t(JSStringCopyBSTR(jsString), false);
1138 }
1139
1140 void LayoutTestController::addOriginAccessWhitelistEntry(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->addOriginAccessWhitelistEntry(bstrT(sourceOrigin).GetBSTR(), bstrT(destinationProtocol).GetBSTR(), bstrT(destinationHost).GetBSTR(), allowDestinationSubdomains);
1147 }
1148
1149 void LayoutTestController::removeOriginAccessWhitelistEntry(JSStringRef sourceOrigin, JSStringRef destinationProtocol, JSStringRef destinationHost, bool allowDestinationSubdomains)
1150 {
1151     COMPtr<IWebViewPrivate> webView;
1152     if (FAILED(WebKitCreateInstance(__uuidof(WebView), 0, __uuidof(webView), reinterpret_cast<void**>(&webView))))
1153         return;
1154
1155     webView->removeOriginAccessWhitelistEntry(bstrT(sourceOrigin).GetBSTR(), bstrT(destinationProtocol).GetBSTR(), bstrT(destinationHost).GetBSTR(), allowDestinationSubdomains);
1156 }
1157
1158 void LayoutTestController::setScrollbarPolicy(JSStringRef orientation, JSStringRef policy)
1159 {
1160     // FIXME: implement
1161 }
1162
1163 void LayoutTestController::addUserScript(JSStringRef source, bool runAtStart, bool allFrames)
1164 {
1165     COMPtr<IWebViewPrivate> webView;
1166     if (FAILED(WebKitCreateInstance(__uuidof(WebView), 0, __uuidof(webView), reinterpret_cast<void**>(&webView))))
1167         return;
1168
1169     COMPtr<IWebScriptWorld> world;
1170     if (FAILED(WebKitCreateInstance(__uuidof(WebScriptWorld), 0, __uuidof(world), reinterpret_cast<void**>(&world))))
1171         return;
1172
1173     webView->addUserScriptToGroup(_bstr_t(L"org.webkit.DumpRenderTree").GetBSTR(), world.get(), bstrT(source).GetBSTR(), 0, 0, 0, 0, 0, runAtStart ? WebInjectAtDocumentStart : WebInjectAtDocumentEnd);
1174 }
1175
1176
1177 void LayoutTestController::addUserStyleSheet(JSStringRef source, bool allFrames)
1178 {
1179     COMPtr<IWebViewPrivate> webView;
1180     if (FAILED(WebKitCreateInstance(__uuidof(WebView), 0, __uuidof(webView), reinterpret_cast<void**>(&webView))))
1181         return;
1182
1183     COMPtr<IWebScriptWorld> world;
1184     if (FAILED(WebKitCreateInstance(__uuidof(WebScriptWorld), 0, __uuidof(world), reinterpret_cast<void**>(&world))))
1185         return;
1186
1187     webView->addUserStyleSheetToGroup(_bstr_t(L"org.webkit.DumpRenderTree").GetBSTR(), world.get(), bstrT(source).GetBSTR(), 0, 0, 0, 0, 0);
1188 }
1189
1190 void LayoutTestController::setDeveloperExtrasEnabled(bool enabled)
1191 {
1192     COMPtr<IWebView> webView;
1193     if (FAILED(frame->webView(&webView)))
1194         return;
1195
1196     COMPtr<IWebPreferences> preferences;
1197     if (FAILED(webView->preferences(&preferences)))
1198         return;
1199
1200     COMPtr<IWebPreferencesPrivate> prefsPrivate(Query, preferences);
1201     if (!prefsPrivate)
1202         return;
1203
1204     prefsPrivate->setDeveloperExtrasEnabled(enabled);
1205 }
1206
1207 void LayoutTestController::setAsynchronousSpellCheckingEnabled(bool)
1208 {
1209     // FIXME: Implement this.
1210 }
1211
1212 void LayoutTestController::showWebInspector()
1213 {
1214     COMPtr<IWebView> webView;
1215     if (FAILED(frame->webView(&webView)))
1216         return;
1217
1218     COMPtr<IWebViewPrivate> viewPrivate(Query, webView);
1219     if (!viewPrivate)
1220         return;
1221
1222     COMPtr<IWebInspector> inspector;
1223     if (SUCCEEDED(viewPrivate->inspector(&inspector)))
1224         inspector->show();
1225 }
1226
1227 void LayoutTestController::closeWebInspector()
1228 {
1229     COMPtr<IWebView> webView;
1230     if (FAILED(frame->webView(&webView)))
1231         return;
1232
1233     COMPtr<IWebViewPrivate> viewPrivate(Query, webView);
1234     if (!viewPrivate)
1235         return;
1236
1237     COMPtr<IWebInspector> inspector;
1238     if (FAILED(viewPrivate->inspector(&inspector)))
1239         return;
1240
1241     inspector->close();
1242 }
1243
1244 void LayoutTestController::evaluateInWebInspector(long callId, JSStringRef script)
1245 {
1246     COMPtr<IWebView> webView;
1247     if (FAILED(frame->webView(&webView)))
1248         return;
1249
1250     COMPtr<IWebViewPrivate> viewPrivate(Query, webView);
1251     if (!viewPrivate)
1252         return;
1253
1254     COMPtr<IWebInspector> inspector;
1255     if (FAILED(viewPrivate->inspector(&inspector)))
1256         return;
1257
1258     COMPtr<IWebInspectorPrivate> inspectorPrivate(Query, inspector);
1259     if (!inspectorPrivate)
1260         return;
1261
1262     inspectorPrivate->evaluateInFrontend(callId, bstrT(script).GetBSTR());
1263 }
1264
1265 typedef HashMap<unsigned, COMPtr<IWebScriptWorld> > WorldMap;
1266 static WorldMap& worldMap()
1267 {
1268     static WorldMap& map = *new WorldMap;
1269     return map;
1270 }
1271
1272 unsigned worldIDForWorld(IWebScriptWorld* world)
1273 {
1274     WorldMap::const_iterator end = worldMap().end();
1275     for (WorldMap::const_iterator it = worldMap().begin(); it != end; ++it) {
1276         if (it->second == world)
1277             return it->first;
1278     }
1279
1280     return 0;
1281 }
1282
1283 void LayoutTestController::evaluateScriptInIsolatedWorldAndReturnValue(unsigned worldID, JSObjectRef globalObject, JSStringRef script)
1284 {
1285     // FIXME: Implement this.
1286 }
1287
1288 void LayoutTestController::evaluateScriptInIsolatedWorld(unsigned worldID, JSObjectRef globalObject, JSStringRef script)
1289 {
1290     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1291     if (!framePrivate)
1292         return;
1293
1294     // A worldID of 0 always corresponds to a new world. Any other worldID corresponds to a world
1295     // that is created once and cached forever.
1296     COMPtr<IWebScriptWorld> world;
1297     if (!worldID) {
1298         if (FAILED(WebKitCreateInstance(__uuidof(WebScriptWorld), 0, __uuidof(world), reinterpret_cast<void**>(&world))))
1299             return;
1300     } else {
1301         COMPtr<IWebScriptWorld>& worldSlot = worldMap().add(worldID, 0).iterator->second;
1302         if (!worldSlot && FAILED(WebKitCreateInstance(__uuidof(WebScriptWorld), 0, __uuidof(worldSlot), reinterpret_cast<void**>(&worldSlot))))
1303             return;
1304         world = worldSlot;
1305     }
1306
1307     BSTR result;
1308     if (FAILED(framePrivate->stringByEvaluatingJavaScriptInScriptWorld(world.get(), globalObject, bstrT(script).GetBSTR(), &result)))
1309         return;
1310     SysFreeString(result);
1311 }
1312
1313 void LayoutTestController::removeAllVisitedLinks()
1314 {
1315     COMPtr<IWebHistory> history;
1316     if (FAILED(WebKitCreateInstance(CLSID_WebHistory, 0, __uuidof(history), reinterpret_cast<void**>(&history))))
1317         return;
1318
1319     COMPtr<IWebHistory> sharedHistory;
1320     if (FAILED(history->optionalSharedHistory(&sharedHistory)) || !sharedHistory)
1321         return;
1322
1323     COMPtr<IWebHistoryPrivate> sharedHistoryPrivate;
1324     if (FAILED(sharedHistory->QueryInterface(&sharedHistoryPrivate)))
1325         return;
1326
1327     sharedHistoryPrivate->removeAllVisitedLinks();
1328 }
1329
1330 JSRetainPtr<JSStringRef> LayoutTestController::counterValueForElementById(JSStringRef id)
1331 {
1332     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1333     if (!framePrivate)
1334         return 0;
1335
1336     wstring idWstring = jsStringRefToWString(id);
1337     BSTR idBSTR = SysAllocStringLen((OLECHAR*)idWstring.c_str(), idWstring.length());
1338     BSTR counterValueBSTR;
1339     if (FAILED(framePrivate->counterValueForElementById(idBSTR, &counterValueBSTR)))
1340         return 0;
1341
1342     wstring counterValue(counterValueBSTR, SysStringLen(counterValueBSTR));
1343     SysFreeString(idBSTR);
1344     SysFreeString(counterValueBSTR);
1345     JSRetainPtr<JSStringRef> counterValueJS(Adopt, JSStringCreateWithCharacters(counterValue.data(), counterValue.length()));
1346     return counterValueJS;
1347 }
1348
1349 int LayoutTestController::pageNumberForElementById(JSStringRef id, float pageWidthInPixels, float pageHeightInPixels)
1350 {
1351     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1352     if (!framePrivate)
1353         return 0;
1354
1355     wstring idWstring = jsStringRefToWString(id);
1356     BSTR idBSTR = SysAllocStringLen((OLECHAR*)idWstring.c_str(), idWstring.length());
1357     int pageNumber = -1;
1358     if (FAILED(framePrivate->pageNumberForElementById(idBSTR, pageWidthInPixels, pageHeightInPixels, &pageNumber)))
1359         pageNumber = -1;
1360     SysFreeString(idBSTR);
1361     return pageNumber;
1362 }
1363
1364 int LayoutTestController::numberOfPages(float pageWidthInPixels, float pageHeightInPixels)
1365 {
1366     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1367     if (!framePrivate)
1368         return 0;
1369
1370     int pageNumber = -1;
1371     if (FAILED(framePrivate->numberOfPages(pageWidthInPixels, pageHeightInPixels, &pageNumber)))
1372         pageNumber = -1;
1373     return pageNumber;
1374 }
1375
1376 JSRetainPtr<JSStringRef> LayoutTestController::pageProperty(const char* propertyName, int pageNumber) const
1377 {
1378     // FIXME: Implement this.
1379     return JSRetainPtr<JSStringRef>();
1380 }
1381
1382 void LayoutTestController::apiTestNewWindowDataLoadBaseURL(JSStringRef utf8Data, JSStringRef baseURL)
1383 {
1384
1385 }
1386
1387 JSRetainPtr<JSStringRef> LayoutTestController::pageSizeAndMarginsInPixels(int pageNumber, int width, int height, int marginTop, int marginRight, int marginBottom, int marginLeft) const
1388 {
1389     // FIXME: implement
1390     return JSRetainPtr<JSStringRef>();
1391 }
1392
1393 void LayoutTestController::apiTestGoToCurrentBackForwardItem()
1394 {
1395     COMPtr<IWebView> webView;
1396     if (FAILED(frame->webView(&webView)))
1397         return;
1398
1399     COMPtr<IWebBackForwardList> backForwardList;
1400     if (FAILED(webView->backForwardList(&backForwardList)))
1401         return;
1402
1403     COMPtr<IWebHistoryItem> item;
1404     if (FAILED(backForwardList->currentItem(&item)))
1405         return;
1406
1407     BOOL success;
1408     webView->goToBackForwardItem(item.get(), &success);
1409 }
1410
1411 void LayoutTestController::setWebViewEditable(bool)
1412 {
1413 }
1414
1415 void LayoutTestController::authenticateSession(JSStringRef, JSStringRef, JSStringRef)
1416 {
1417 }
1418
1419 void LayoutTestController::setEditingBehavior(const char* editingBehavior)
1420 {
1421     COMPtr<IWebView> webView;
1422     if (FAILED(frame->webView(&webView)))
1423         return;
1424
1425     COMPtr<IWebPreferences> preferences;
1426     if (FAILED(webView->preferences(&preferences)))
1427         return;
1428
1429     string behaviorString(editingBehavior);
1430     if (behaviorString == "mac")
1431         preferences->setEditingBehavior(WebKitEditingMacBehavior);
1432     else if (behaviorString == "win")
1433         preferences->setEditingBehavior(WebKitEditingWinBehavior);
1434     else if (behaviorString == "unix")
1435         preferences->setEditingBehavior(WebKitEditingUnixBehavior);
1436 }
1437
1438 void LayoutTestController::abortModal()
1439 {
1440 }
1441
1442 void LayoutTestController::dumpConfigurationForViewport(int /*deviceDPI*/, int /*deviceWidth*/, int /*deviceHeight*/, int /*availableWidth*/, int /*availableHeight*/)
1443 {
1444     // FIXME: Implement this.
1445 }
1446
1447 void LayoutTestController::setSerializeHTTPLoads(bool)
1448 {
1449     // FIXME: Implement.
1450 }
1451
1452 void LayoutTestController::syncLocalStorage()
1453 {
1454     // FIXME: Implement.
1455 }
1456
1457 void LayoutTestController::observeStorageTrackerNotifications(unsigned number)
1458 {
1459     // FIXME: Implement.
1460 }
1461
1462 void LayoutTestController::deleteAllLocalStorage()
1463 {
1464     // FIXME: Implement.
1465 }
1466
1467 JSValueRef LayoutTestController::originsWithLocalStorage(JSContextRef context)
1468 {
1469     // FIXME: Implement.
1470     return JSValueMakeUndefined(context);
1471 }
1472
1473 long long LayoutTestController::localStorageDiskUsageForOrigin(JSStringRef originIdentifier)
1474 {
1475     // FIXME: Implement to support getting local storage disk usage for an origin.
1476     return 0;
1477 }
1478
1479 void LayoutTestController::deleteLocalStorageForOrigin(JSStringRef URL)
1480 {
1481     // FIXME: Implement.
1482 }
1483
1484 void LayoutTestController::setMinimumTimerInterval(double minimumTimerInterval)
1485 {
1486     COMPtr<IWebView> webView;
1487     if (FAILED(frame->webView(&webView)))
1488         return;
1489
1490     COMPtr<IWebViewPrivate> viewPrivate(Query, webView);
1491     if (!viewPrivate)
1492         return;
1493
1494     viewPrivate->setMinimumTimerInterval(minimumTimerInterval);
1495 }
1496
1497 void LayoutTestController::setTextDirection(JSStringRef direction)
1498 {
1499     COMPtr<IWebFramePrivate> framePrivate(Query, frame);
1500     if (!framePrivate)
1501         return;
1502
1503     framePrivate->setTextDirection(bstrT(direction).GetBSTR());
1504 }
1505
1506 void LayoutTestController::allowRoundingHacks()
1507 {
1508 }
1509
1510 void LayoutTestController::addChromeInputField()
1511 {
1512 }
1513
1514 void LayoutTestController::removeChromeInputField()
1515 {
1516 }
1517
1518 void LayoutTestController::focusWebView()
1519 {
1520 }
1521
1522 void LayoutTestController::setBackingScaleFactor(double)
1523 {
1524 }
1525
1526 void LayoutTestController::simulateDesktopNotificationClick(JSStringRef title)
1527 {
1528     // FIXME: Implement.
1529 }
1530
1531 void LayoutTestController::resetPageVisibility()
1532 {
1533     // FIXME: Implement this.
1534 }
1535
1536 void LayoutTestController::setPageVisibility(const char*)
1537 {
1538     // FIXME: Implement this.
1539 }