1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file contains the implementation of TestWebViewDelegate, which serves
// as the WebViewDelegate for the TestShellWebHost. The host is expected to
// have initialized a MessageLoop before these methods are called.
#include "config.h"
#include "webkit/tools/test_shell/test_webview_delegate.h"
#include "base/file_util.h"
#include "base/gfx/point.h"
#include "base/gfx/native_widget_types.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/trace_event.h"
#include "net/base/net_errors.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDragData.h"
#include "third_party/WebKit/WebKit/chromium/public/WebKit.h"
#include "third_party/WebKit/WebKit/chromium/public/WebScreenInfo.h"
#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
#include "webkit/glue/webdatasource.h"
#include "webkit/glue/webdropdata.h"
#include "webkit/glue/weberror.h"
#include "webkit/glue/webframe.h"
#include "webkit/glue/webpreferences.h"
#include "webkit/glue/weburlrequest.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/glue/webview.h"
#include "webkit/glue/plugins/plugin_list.h"
#include "webkit/glue/plugins/webplugin_delegate_impl.h"
#include "webkit/glue/window_open_disposition.h"
#include "webkit/tools/test_shell/test_navigation_controller.h"
#include "webkit/tools/test_shell/test_shell.h"
#include "webkit/tools/test_shell/test_webworker_helper.h"
#if defined(OS_WIN)
// TODO(port): make these files work everywhere.
#include "webkit/tools/test_shell/drag_delegate.h"
#include "webkit/tools/test_shell/drop_delegate.h"
#endif
using WebKit::WebDragData;
using WebKit::WebRect;
using WebKit::WebScreenInfo;
using WebKit::WebSize;
using WebKit::WebString;
using WebKit::WebWorker;
using WebKit::WebWorkerClient;
namespace {
// WebNavigationType debugging strings taken from PolicyDelegate.mm.
const char* kLinkClickedString = "link clicked";
const char* kFormSubmittedString = "form submitted";
const char* kBackForwardString = "back/forward";
const char* kReloadString = "reload";
const char* kFormResubmittedString = "form resubmitted";
const char* kOtherString = "other";
const char* kIllegalString = "illegal value";
int next_page_id_ = 1;
// Used to write a platform neutral file:/// URL by only taking the filename
// (e.g., converts "file:///tmp/foo.txt" to just "foo.txt").
std::wstring UrlSuitableForTestResult(const std::wstring& url) {
if (url.empty() || std::wstring::npos == url.find(L"file://"))
return url;
std::wstring filename = file_util::GetFilenameFromPath(url);
if (filename.empty())
return L"file:"; // A WebKit test has this in its expected output.
return filename;
}
// Adds a file called "DRTFakeFile" to |data_object| (CF_HDROP). Use to fake
// dragging a file.
void AddDRTFakeFileToDataObject(WebDragData* drag_data) {
drag_data->appendToFileNames(WebString::fromUTF8("DRTFakeFile"));
}
// Get a debugging string from a WebNavigationType.
const char* WebNavigationTypeToString(WebNavigationType type) {
switch (type) {
case WebNavigationTypeLinkClicked:
return kLinkClickedString;
case WebNavigationTypeFormSubmitted:
return kFormSubmittedString;
case WebNavigationTypeBackForward:
return kBackForwardString;
case WebNavigationTypeReload:
return kReloadString;
case WebNavigationTypeFormResubmitted:
return kFormResubmittedString;
case WebNavigationTypeOther:
return kOtherString;
}
return kIllegalString;
}
} // namespace
// WebViewDelegate -----------------------------------------------------------
WebView* TestWebViewDelegate::CreateWebView(WebView* webview,
bool user_gesture) {
return shell_->CreateWebView(webview);
}
WebWidget* TestWebViewDelegate::CreatePopupWidget(WebView* webview,
bool activatable) {
return shell_->CreatePopupWidget(webview);
}
void TestWebViewDelegate::OpenURL(WebView* webview, const GURL& url,
const GURL& referrer,
WindowOpenDisposition disposition) {
DCHECK_NE(disposition, CURRENT_TAB); // No code for this
if (disposition == SUPPRESS_OPEN)
return;
TestShell* shell = NULL;
if (TestShell::CreateNewWindow(UTF8ToWide(url.spec()), &shell))
shell->Show(shell->webView(), disposition);
}
void TestWebViewDelegate::DidStartLoading(WebView* webview) {
// Ignored
}
void TestWebViewDelegate::DidStopLoading(WebView* webview) {
// Ignored
}
void TestWebViewDelegate::WindowObjectCleared(WebFrame* webframe) {
shell_->BindJSObjectsToWindow(webframe);
}
WindowOpenDisposition TestWebViewDelegate::DispositionForNavigationAction(
WebView* webview,
WebFrame* frame,
const WebRequest* request,
WebNavigationType type,
WindowOpenDisposition disposition,
bool is_redirect) {
WindowOpenDisposition result;
if (policy_delegate_enabled_) {
std::wstring frame_name = frame->GetName();
std::string url_description;
if (request->GetURL().SchemeIs("file")) {
url_description = request->GetURL().ExtractFileName();
} else {
url_description = request->GetURL().spec();
}
printf("Policy delegate: attempt to load %s with navigation type '%s'\n",
url_description.c_str(), WebNavigationTypeToString(type));
result = policy_delegate_is_permissive_ ? CURRENT_TAB : IGNORE_ACTION;
if (policy_delegate_should_notify_done_)
shell_->layout_test_controller()->PolicyDelegateDone();
} else {
result = WebViewDelegate::DispositionForNavigationAction(
webview, frame, request, type, disposition, is_redirect);
}
return result;
}
void TestWebViewDelegate::AssignIdentifierToRequest(WebView* webview,
uint32 identifier,
const WebRequest& request) {
if (shell_->ShouldDumpResourceLoadCallbacks()) {
resource_identifier_map_[identifier] = request.GetURL().possibly_invalid_spec();
}
}
std::string TestWebViewDelegate::GetResourceDescription(uint32 identifier) {
ResourceMap::iterator it = resource_identifier_map_.find(identifier);
return it != resource_identifier_map_.end() ? it->second : "<unknown>";
}
void TestWebViewDelegate::WillSendRequest(WebView* webview,
uint32 identifier,
WebRequest* request) {
GURL url = request->GetURL();
std::string request_url = url.possibly_invalid_spec();
std::string host = request->GetURL().host();
if (shell_->ShouldDumpResourceLoadCallbacks()) {
printf("%s - willSendRequest <WebRequest URL \"%s\">\n",
GetResourceDescription(identifier).c_str(),
request_url.c_str());
}
if (TestShell::layout_test_mode() && !host.empty() &&
(url.SchemeIs("http") || url.SchemeIs("https")) &&
host != "127.0.0.1" &&
host != "255.255.255.255" && // Used in some tests that expect to get
// back an error.
host != "localhost") {
printf("Blocked access to external URL %s\n", request_url.c_str());
// To block the request, we set its URL to an empty one.
request->SetURL(GURL());
return;
}
TRACE_EVENT_BEGIN("url.load", identifier, request_url);
// Set the new substituted URL.
request->SetURL(GURL(TestShell::RewriteLocalUrl(request_url)));
}
void TestWebViewDelegate::DidFinishLoading(WebView* webview,
uint32 identifier) {
TRACE_EVENT_END("url.load", identifier, "");
if (shell_->ShouldDumpResourceLoadCallbacks()) {
printf("%s - didFinishLoading\n",
GetResourceDescription(identifier).c_str());
}
resource_identifier_map_.erase(identifier);
}
void TestWebViewDelegate::DidFailLoadingWithError(WebView* webview,
uint32 identifier,
const WebError& error) {
if (shell_->ShouldDumpResourceLoadCallbacks()) {
printf("%s - didFailLoadingWithError <WebError code %d,"
" failing URL \"%s\">\n",
GetResourceDescription(identifier).c_str(),
error.GetErrorCode(),
error.GetFailedURL().spec().c_str());
}
resource_identifier_map_.erase(identifier);
}
void TestWebViewDelegate::DidStartProvisionalLoadForFrame(
WebView* webview,
WebFrame* frame,
NavigationGesture gesture) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didStartProvisionalLoadForFrame\n",
GetFrameDescription(frame).c_str());
}
if (!top_loading_frame_) {
top_loading_frame_ = frame;
}
if (shell_->layout_test_controller()->StopProvisionalFrameLoads()) {
printf("%S - stopping load in didStartProvisionalLoadForFrame callback\n",
GetFrameDescription(frame).c_str());
frame->StopLoading();
}
UpdateAddressBar(webview);
}
void TestWebViewDelegate::DidReceiveServerRedirectForProvisionalLoadForFrame(
WebView* webview,
WebFrame* frame) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didReceiveServerRedirectForProvisionalLoadForFrame\n",
GetFrameDescription(frame).c_str());
}
UpdateAddressBar(webview);
}
void TestWebViewDelegate::DidFailProvisionalLoadWithError(
WebView* webview,
const WebError& error,
WebFrame* frame) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didFailProvisionalLoadWithError\n",
GetFrameDescription(frame).c_str());
}
LocationChangeDone(frame);
// Don't display an error page if we're running layout tests, because
// DumpRenderTree doesn't.
if (shell_->layout_test_mode())
return;
// Don't display an error page if this is simply a cancelled load. Aside
// from being dumb, WebCore doesn't expect it and it will cause a crash.
if (error.GetErrorCode() == net::ERR_ABORTED)
return;
const WebRequest& failed_request =
frame->GetProvisionalDataSource()->GetRequest();
TestShellExtraRequestData* extra_data =
static_cast<TestShellExtraRequestData*>(failed_request.GetExtraData());
bool replace = extra_data && extra_data->pending_page_id != -1;
scoped_ptr<WebRequest> request(failed_request.Clone());
std::string error_text =
StringPrintf("Error %d when loading url %s", error.GetErrorCode(),
request->GetURL().spec().c_str());
request->SetURL(GURL("testshell-error:"));
frame->LoadAlternateHTMLString(request.get(), error_text,
error.GetFailedURL(), replace);
}
void TestWebViewDelegate::DidCommitLoadForFrame(WebView* webview,
WebFrame* frame,
bool is_new_navigation) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didCommitLoadForFrame\n",
GetFrameDescription(frame).c_str());
}
UpdateForCommittedLoad(frame, is_new_navigation);
}
void TestWebViewDelegate::DidReceiveTitle(WebView* webview,
const std::wstring& title,
WebFrame* frame) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didReceiveTitle\n",
GetFrameDescription(frame).c_str());
}
if (shell_->ShouldDumpTitleChanges()) {
printf("TITLE CHANGED: %S\n", title.c_str());
}
SetPageTitle(title);
}
void TestWebViewDelegate::DidFinishLoadForFrame(WebView* webview,
WebFrame* frame) {
TRACE_EVENT_END("frame.load", this, frame->GetURL().spec());
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didFinishLoadForFrame\n",
GetFrameDescription(frame).c_str());
}
UpdateAddressBar(webview);
LocationChangeDone(frame);
}
void TestWebViewDelegate::DidFailLoadWithError(WebView* webview,
const WebError& error,
WebFrame* frame) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didFailLoadWithError\n",
GetFrameDescription(frame).c_str());
}
LocationChangeDone(frame);
}
void TestWebViewDelegate::DidFinishDocumentLoadForFrame(WebView* webview,
WebFrame* frame) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didFinishDocumentLoadForFrame\n",
GetFrameDescription(frame).c_str());
} else {
unsigned pending_unload_events = frame->PendingFrameUnloadEventCount();
if (pending_unload_events) {
printf("%S - has %u onunload handler(s)\n",
GetFrameDescription(frame).c_str(), pending_unload_events);
}
}
}
void TestWebViewDelegate::DidHandleOnloadEventsForFrame(WebView* webview,
WebFrame* frame) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didHandleOnloadEventsForFrame\n",
GetFrameDescription(frame).c_str());
}
}
void TestWebViewDelegate::DidChangeLocationWithinPageForFrame(
WebView* webview, WebFrame* frame, bool is_new_navigation) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didChangeLocationWithinPageForFrame\n",
GetFrameDescription(frame).c_str());
}
UpdateForCommittedLoad(frame, is_new_navigation);
}
void TestWebViewDelegate::DidReceiveIconForFrame(WebView* webview,
WebFrame* frame) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didReceiveIconForFrame\n",
GetFrameDescription(frame).c_str());
}
}
void TestWebViewDelegate::WillPerformClientRedirect(WebView* webview,
WebFrame* frame,
const std::wstring& dest_url,
unsigned int delay_seconds,
unsigned int fire_date) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
// FIXME: prettyprint the url?
printf("%S - willPerformClientRedirectToURL: %S\n",
GetFrameDescription(frame).c_str(), dest_url.c_str());
}
}
void TestWebViewDelegate::DidCancelClientRedirect(WebView* webview,
WebFrame* frame) {
if (shell_->ShouldDumpFrameLoadCallbacks()) {
printf("%S - didCancelClientRedirectForFrame\n",
GetFrameDescription(frame).c_str());
}
}
void TestWebViewDelegate::AddMessageToConsole(WebView* webview,
const std::wstring& message,
unsigned int line_no,
const std::wstring& source_id) {
if (!shell_->layout_test_mode()) {
logging::LogMessage("CONSOLE", 0).stream() << "\""
<< message.c_str()
<< ",\" source: "
<< source_id.c_str()
<< "("
<< line_no
<< ")";
} else {
// This matches win DumpRenderTree's UIDelegate.cpp.
std::wstring new_message = message;
if (!message.empty()) {
new_message = message;
size_t file_protocol = new_message.find(L"file://");
if (file_protocol != std::wstring::npos) {
new_message = new_message.substr(0, file_protocol) +
UrlSuitableForTestResult(new_message.substr(file_protocol));
}
}
std::string utf8 = WideToUTF8(new_message);
printf("CONSOLE MESSAGE: line %d: %s\n", line_no, utf8.c_str());
}
}
void TestWebViewDelegate::RunJavaScriptAlert(WebFrame* webframe,
const std::wstring& message) {
if (!shell_->layout_test_mode()) {
ShowJavaScriptAlert(message);
} else {
std::string utf8 = WideToUTF8(message);
printf("ALERT: %s\n", utf8.c_str());
}
}
bool TestWebViewDelegate::RunJavaScriptConfirm(WebFrame* webframe,
const std::wstring& message) {
if (shell_->layout_test_mode()) {
// When running tests, write to stdout.
std::string utf8 = WideToUTF8(message);
printf("CONFIRM: %s\n", utf8.c_str());
return true;
}
return false;
}
bool TestWebViewDelegate::RunJavaScriptPrompt(WebFrame* webframe,
const std::wstring& message,
const std::wstring& default_value,
std::wstring* result) {
if (shell_->layout_test_mode()) {
// When running tests, write to stdout.
std::string utf8_message = WideToUTF8(message);
std::string utf8_default_value = WideToUTF8(default_value);
printf("PROMPT: %s, default text: %s\n", utf8_message.c_str(),
utf8_default_value.c_str());
return true;
}
return false;
}
void TestWebViewDelegate::SetStatusbarText(WebView* webview,
const std::wstring& message) {
if (WebKit::layoutTestMode() &&
shell_->layout_test_controller()->ShouldDumpStatusCallbacks()) {
// When running tests, write to stdout.
printf("UI DELEGATE STATUS CALLBACK: setStatusText:%S\n", message.c_str());
}
}
void TestWebViewDelegate::StartDragging(WebView* webview,
const WebDragData& drag_data) {
if (WebKit::layoutTestMode()) {
WebDragData mutable_drag_data = drag_data;
if (shell_->layout_test_controller()->ShouldAddFileToPasteboard()) {
// Add a file called DRTFakeFile to the drag&drop clipboard.
AddDRTFakeFileToDataObject(&mutable_drag_data);
}
// When running a test, we need to fake a drag drop operation otherwise
// Windows waits for real mouse events to know when the drag is over.
EventSendingController::DoDragDrop(mutable_drag_data);
} else {
// TODO(tc): Drag and drop is disabled in the test shell because we need
// to be able to convert from WebDragData to an IDataObject.
//if (!drag_delegate_)
// drag_delegate_ = new TestDragDelegate(shell_->webViewWnd(),
// shell_->webView());
//const DWORD ok_effect = DROPEFFECT_COPY | DROPEFFECT_LINK | DROPEFFECT_MOVE;
//DWORD effect;
//HRESULT res = DoDragDrop(drop_data.data_object, drag_delegate_.get(),
// ok_effect, &effect);
//DCHECK(DRAGDROP_S_DROP == res || DRAGDROP_S_CANCEL == res);
}
webview->DragSourceSystemDragEnded();
}
void TestWebViewDelegate::ShowContextMenu(WebView* webview,
ContextNode node,
int x,
int y,
const GURL& link_url,
const GURL& image_url,
const GURL& page_url,
const GURL& frame_url,
const std::wstring& selection_text,
const std::wstring& misspelled_word,
int edit_flags,
const std::string& security_info,
const std::string& frame_charset) {
CapturedContextMenuEvent context(node, x, y);
captured_context_menu_events_.push_back(context);
}
// The output from these methods in layout test mode should match that
// expected by the layout tests. See EditingDelegate.m in DumpRenderTree.
bool TestWebViewDelegate::ShouldBeginEditing(WebView* webview,
std::wstring range) {
if (shell_->ShouldDumpEditingCallbacks()) {
std::string utf8 = WideToUTF8(range);
printf("EDITING DELEGATE: shouldBeginEditingInDOMRange:%s\n",
utf8.c_str());
}
return shell_->AcceptsEditing();
}
bool TestWebViewDelegate::ShouldEndEditing(WebView* webview,
std::wstring range) {
if (shell_->ShouldDumpEditingCallbacks()) {
std::string utf8 = WideToUTF8(range);
printf("EDITING DELEGATE: shouldEndEditingInDOMRange:%s\n",
utf8.c_str());
}
return shell_->AcceptsEditing();
}
bool TestWebViewDelegate::ShouldInsertNode(WebView* webview,
std::wstring node,
std::wstring range,
std::wstring action) {
if (shell_->ShouldDumpEditingCallbacks()) {
std::string utf8_node = WideToUTF8(node);
std::string utf8_range = WideToUTF8(range);
std::string utf8_action = WideToUTF8(action);
printf("EDITING DELEGATE: shouldInsertNode:%s "
"replacingDOMRange:%s givenAction:%s\n",
utf8_node.c_str(), utf8_range.c_str(), utf8_action.c_str());
}
return shell_->AcceptsEditing();
}
bool TestWebViewDelegate::ShouldInsertText(WebView* webview,
std::wstring text,
std::wstring range,
std::wstring action) {
if (shell_->ShouldDumpEditingCallbacks()) {
std::string utf8_text = WideToUTF8(text);
std::string utf8_range = WideToUTF8(range);
std::string utf8_action = WideToUTF8(action);
printf("EDITING DELEGATE: shouldInsertText:%s "
"replacingDOMRange:%s givenAction:%s\n",
utf8_text.c_str(), utf8_range.c_str(), utf8_action.c_str());
}
return shell_->AcceptsEditing();
}
bool TestWebViewDelegate::ShouldChangeSelectedRange(WebView* webview,
std::wstring fromRange,
std::wstring toRange,
std::wstring affinity,
bool stillSelecting) {
if (shell_->ShouldDumpEditingCallbacks()) {
std::string utf8_from = WideToUTF8(fromRange);
std::string utf8_to = WideToUTF8(toRange);
std::string utf8_affinity = WideToUTF8(affinity);
printf("EDITING DELEGATE: shouldChangeSelectedDOMRange:%s "
"toDOMRange:%s affinity:%s stillSelecting:%s\n",
utf8_from.c_str(),
utf8_to.c_str(),
utf8_affinity.c_str(),
(stillSelecting ? "TRUE" : "FALSE"));
}
return shell_->AcceptsEditing();
}
bool TestWebViewDelegate::ShouldDeleteRange(WebView* webview,
std::wstring range) {
if (shell_->ShouldDumpEditingCallbacks()) {
std::string utf8 = WideToUTF8(range);
printf("EDITING DELEGATE: shouldDeleteDOMRange:%s\n", utf8.c_str());
}
return shell_->AcceptsEditing();
}
bool TestWebViewDelegate::ShouldApplyStyle(WebView* webview,
std::wstring style,
std::wstring range) {
if (shell_->ShouldDumpEditingCallbacks()) {
std::string utf8_style = WideToUTF8(style);
std::string utf8_range = WideToUTF8(range);
printf("EDITING DELEGATE: shouldApplyStyle:%s toElementsInDOMRange:%s\n",
utf8_style.c_str(), utf8_range.c_str());
}
return shell_->AcceptsEditing();
}
bool TestWebViewDelegate::SmartInsertDeleteEnabled() {
return smart_insert_delete_enabled_;
}
bool TestWebViewDelegate::IsSelectTrailingWhitespaceEnabled() {
return select_trailing_whitespace_enabled_;
}
void TestWebViewDelegate::DidBeginEditing() {
if (shell_->ShouldDumpEditingCallbacks()) {
printf("EDITING DELEGATE: "
"webViewDidBeginEditing:WebViewDidBeginEditingNotification\n");
}
}
void TestWebViewDelegate::DidChangeSelection(bool is_empty_selection) {
if (shell_->ShouldDumpEditingCallbacks()) {
printf("EDITING DELEGATE: "
"webViewDidChangeSelection:WebViewDidChangeSelectionNotification\n");
}
UpdateSelectionClipboard(is_empty_selection);
}
void TestWebViewDelegate::DidChangeContents() {
if (shell_->ShouldDumpEditingCallbacks()) {
printf("EDITING DELEGATE: "
"webViewDidChange:WebViewDidChangeNotification\n");
}
}
void TestWebViewDelegate::DidEndEditing() {
if (shell_->ShouldDumpEditingCallbacks()) {
printf("EDITING DELEGATE: "
"webViewDidEndEditing:WebViewDidEndEditingNotification\n");
}
}
WebHistoryItem* TestWebViewDelegate::GetHistoryEntryAtOffset(int offset) {
TestNavigationEntry* entry = static_cast<TestNavigationEntry*>(
shell_->navigation_controller()->GetEntryAtOffset(offset));
if (!entry)
return NULL;
return entry->GetHistoryItem();
}
int TestWebViewDelegate::GetHistoryBackListCount() {
int current_index =
shell_->navigation_controller()->GetLastCommittedEntryIndex();
return current_index;
}
int TestWebViewDelegate::GetHistoryForwardListCount() {
int current_index =
shell_->navigation_controller()->GetLastCommittedEntryIndex();
return shell_->navigation_controller()->GetEntryCount() - current_index - 1;
}
void TestWebViewDelegate::SetUserStyleSheetEnabled(bool is_enabled) {
WebPreferences* prefs = shell_->GetWebPreferences();
prefs->user_style_sheet_enabled = is_enabled;
shell_->webView()->SetPreferences(*prefs);
}
void TestWebViewDelegate::SetUserStyleSheetLocation(const GURL& location) {
WebPreferences* prefs = shell_->GetWebPreferences();
prefs->user_style_sheet_enabled = true;
prefs->user_style_sheet_location = location;
shell_->webView()->SetPreferences(*prefs);
}
// WebWidgetDelegate ---------------------------------------------------------
gfx::NativeViewId TestWebViewDelegate::GetContainingView(WebWidget* webwidget) {
// For test shell, we pack a NativeView pointer into the NativeViewId since
// everything is single process.
if (WebWidgetHost* host = GetHostForWidget(webwidget))
return gfx::IdFromNativeView(host->view_handle());
return NULL;
}
void TestWebViewDelegate::DidInvalidateRect(WebWidget* webwidget,
const WebRect& rect) {
if (WebWidgetHost* host = GetHostForWidget(webwidget))
host->DidInvalidateRect(rect);
}
void TestWebViewDelegate::DidScrollRect(WebWidget* webwidget, int dx, int dy,
const WebRect& clip_rect) {
if (WebWidgetHost* host = GetHostForWidget(webwidget))
host->DidScrollRect(dx, dy, clip_rect);
}
void TestWebViewDelegate::Focus(WebWidget* webwidget) {
if (WebWidgetHost* host = GetHostForWidget(webwidget))
shell_->SetFocus(host, true);
}
void TestWebViewDelegate::Blur(WebWidget* webwidget) {
if (WebWidgetHost* host = GetHostForWidget(webwidget))
shell_->SetFocus(host, false);
}
bool TestWebViewDelegate::IsHidden(WebWidget* webwidget) {
return false;
}
WebScreenInfo TestWebViewDelegate::GetScreenInfo(WebWidget* webwidget) {
if (WebWidgetHost* host = GetHostForWidget(webwidget))
return host->GetScreenInfo();
return WebScreenInfo();
}
void TestWebViewDelegate::SetSmartInsertDeleteEnabled(bool enabled) {
smart_insert_delete_enabled_ = enabled;
// In upstream WebKit, smart insert/delete is mutually exclusive with select
// trailing whitespace, however, we allow both because Chromium on Windows
// allows both.
}
void TestWebViewDelegate::SetSelectTrailingWhitespaceEnabled(bool enabled) {
select_trailing_whitespace_enabled_ = enabled;
// In upstream WebKit, smart insert/delete is mutually exclusive with select
// trailing whitespace, however, we allow both because Chromium on Windows
// allows both.
}
void TestWebViewDelegate::RegisterDragDrop() {
#if defined(OS_WIN)
// TODO(port): add me once drag and drop works.
DCHECK(!drop_delegate_);
drop_delegate_ = new TestDropDelegate(shell_->webViewWnd(),
shell_->webView());
#endif
}
void TestWebViewDelegate::SetCustomPolicyDelegate(bool is_custom,
bool is_permissive) {
policy_delegate_enabled_ = is_custom;
policy_delegate_is_permissive_ = is_permissive;
}
void TestWebViewDelegate::WaitForPolicyDelegate() {
policy_delegate_enabled_ = true;
policy_delegate_should_notify_done_ = true;
}
// Private methods -----------------------------------------------------------
void TestWebViewDelegate::UpdateAddressBar(WebView* webView) {
WebFrame* mainFrame = webView->GetMainFrame();
WebDataSource* dataSource = mainFrame->GetDataSource();
if (!dataSource)
dataSource = mainFrame->GetProvisionalDataSource();
if (!dataSource)
return;
SetAddressBarURL(dataSource->GetRequest().GetMainDocumentURL());
}
void TestWebViewDelegate::LocationChangeDone(WebFrame* frame) {
if (frame == top_loading_frame_) {
top_loading_frame_ = NULL;
if (shell_->layout_test_mode())
shell_->layout_test_controller()->LocationChangeDone();
}
}
WebWidgetHost* TestWebViewDelegate::GetHostForWidget(WebWidget* webwidget) {
if (webwidget == shell_->webView())
return shell_->webViewHost();
if (webwidget == shell_->popup())
return shell_->popupHost();
return NULL;
}
void TestWebViewDelegate::UpdateForCommittedLoad(WebFrame* frame,
bool is_new_navigation) {
WebView* webview = shell_->webView();
// Code duplicated from RenderView::DidCommitLoadForFrame.
const WebRequest& request =
webview->GetMainFrame()->GetDataSource()->GetRequest();
TestShellExtraRequestData* extra_data =
static_cast<TestShellExtraRequestData*>(request.GetExtraData());
if (is_new_navigation) {
// New navigation.
UpdateSessionHistory(frame);
page_id_ = next_page_id_++;
} else if (extra_data && extra_data->pending_page_id != -1 &&
!extra_data->request_committed) {
// This is a successful session history navigation!
UpdateSessionHistory(frame);
page_id_ = extra_data->pending_page_id;
}
// Don't update session history multiple times.
if (extra_data)
extra_data->request_committed = true;
UpdateURL(frame);
}
void TestWebViewDelegate::UpdateURL(WebFrame* frame) {
WebDataSource* ds = frame->GetDataSource();
DCHECK(ds);
const WebRequest& request = ds->GetRequest();
// Type is unused.
scoped_ptr<TestNavigationEntry> entry(new TestNavigationEntry);
// Bug 654101: the referrer will be empty on https->http transitions. It
// would be nice if we could get the real referrer from somewhere.
entry->SetPageID(page_id_);
if (ds->HasUnreachableURL()) {
entry->SetURL(GURL(ds->GetUnreachableURL()));
} else {
entry->SetURL(GURL(request.GetURL()));
}
shell_->navigation_controller()->DidNavigateToEntry(entry.release());
last_page_id_updated_ = std::max(last_page_id_updated_, page_id_);
}
void TestWebViewDelegate::UpdateSessionHistory(WebFrame* frame) {
// If we have a valid page ID at this point, then it corresponds to the page
// we are navigating away from. Otherwise, this is the first navigation, so
// there is no past session history to record.
if (page_id_ == -1)
return;
TestNavigationEntry* entry = static_cast<TestNavigationEntry*>(
shell_->navigation_controller()->GetEntryWithPageID(page_id_));
if (!entry)
return;
std::string state;
if (!shell_->webView()->GetMainFrame()->GetPreviousHistoryState(&state))
return;
entry->SetContentState(state);
}
std::wstring TestWebViewDelegate::GetFrameDescription(WebFrame* webframe) {
std::wstring name = webframe->GetName();
if (webframe == shell_->webView()->GetMainFrame()) {
if (name.length())
return L"main frame \"" + name + L"\"";
else
return L"main frame";
} else {
if (name.length())
return L"frame \"" + name + L"\"";
else
return L"frame (anonymous)";
}
}
WebWorker* TestWebViewDelegate::CreateWebWorker(WebWorkerClient* client) {
#if ENABLE(WORKERS)
return TestWebWorkerHelper::CreateWebWorker(client);
#else
return NULL;
#endif
}
|