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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
|
// Copyright 2014 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.
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
#include "base/message_loop/message_loop.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "components/browsing_data/storage_partition_http_cache_data_remover.h"
#include "components/guest_view/browser/guest_view_event.h"
#include "components/guest_view/browser/guest_view_manager.h"
#include "components/guest_view/common/guest_view_constants.h"
#include "components/web_cache/browser/web_cache_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_security_policy.h"
#include "content/public/browser/native_web_keyboard_event.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/resource_request_details.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/media_stream_request.h"
#include "content/public/common/page_zoom.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/stop_find_action.h"
#include "content/public/common/url_constants.h"
#include "extensions/browser/api/declarative/rules_registry_service.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h"
#include "extensions/browser/api/web_request/web_request_api.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/browser/guest_view/web_view/web_view_constants.h"
#include "extensions/browser/guest_view/web_view/web_view_content_script_manager.h"
#include "extensions/browser/guest_view/web_view/web_view_permission_helper.h"
#include "extensions/browser/guest_view/web_view/web_view_permission_types.h"
#include "extensions/browser/guest_view/web_view/web_view_renderer_state.h"
#include "extensions/common/constants.h"
#include "extensions/common/extension_messages.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/strings/grit/extensions_strings.h"
#include "ipc/ipc_message_macros.h"
#include "net/base/escape.h"
#include "net/base/net_errors.h"
#include "ui/base/models/simple_menu_model.h"
#include "url/url_constants.h"
using base::UserMetricsAction;
using content::GlobalRequestID;
using content::RenderFrameHost;
using content::RenderProcessHost;
using content::ResourceType;
using content::StoragePartition;
using content::WebContents;
using guest_view::GuestViewBase;
using guest_view::GuestViewEvent;
using guest_view::GuestViewManager;
using ui_zoom::ZoomController;
namespace extensions {
namespace {
// Returns storage partition removal mask from web_view clearData mask. Note
// that storage partition mask is a subset of webview's data removal mask.
uint32 GetStoragePartitionRemovalMask(uint32 web_view_removal_mask) {
uint32 mask = 0;
if (web_view_removal_mask & webview::WEB_VIEW_REMOVE_DATA_MASK_APPCACHE)
mask |= StoragePartition::REMOVE_DATA_MASK_APPCACHE;
if (web_view_removal_mask & webview::WEB_VIEW_REMOVE_DATA_MASK_COOKIES)
mask |= StoragePartition::REMOVE_DATA_MASK_COOKIES;
if (web_view_removal_mask & webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS)
mask |= StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
if (web_view_removal_mask & webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB)
mask |= StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
if (web_view_removal_mask & webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE)
mask |= StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
if (web_view_removal_mask & webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL)
mask |= StoragePartition::REMOVE_DATA_MASK_WEBSQL;
return mask;
}
std::string WindowOpenDispositionToString(
WindowOpenDisposition window_open_disposition) {
switch (window_open_disposition) {
case IGNORE_ACTION:
return "ignore";
case SAVE_TO_DISK:
return "save_to_disk";
case CURRENT_TAB:
return "current_tab";
case NEW_BACKGROUND_TAB:
return "new_background_tab";
case NEW_FOREGROUND_TAB:
return "new_foreground_tab";
case NEW_WINDOW:
return "new_window";
case NEW_POPUP:
return "new_popup";
default:
NOTREACHED() << "Unknown Window Open Disposition";
return "ignore";
}
}
static std::string TerminationStatusToString(base::TerminationStatus status) {
switch (status) {
case base::TERMINATION_STATUS_NORMAL_TERMINATION:
return "normal";
case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
case base::TERMINATION_STATUS_STILL_RUNNING:
return "abnormal";
#if defined(OS_CHROMEOS)
case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
return "oom killed";
#endif
case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
return "killed";
case base::TERMINATION_STATUS_PROCESS_CRASHED:
return "crashed";
case base::TERMINATION_STATUS_LAUNCH_FAILED:
return "failed to launch";
case base::TERMINATION_STATUS_MAX_ENUM:
break;
}
NOTREACHED() << "Unknown Termination Status.";
return "unknown";
}
std::string GetStoragePartitionIdFromSiteURL(const GURL& site_url) {
const std::string& partition_id = site_url.query();
bool persist_storage = site_url.path().find("persist") != std::string::npos;
return (persist_storage ? webview::kPersistPrefix : "") + partition_id;
}
void ParsePartitionParam(const base::DictionaryValue& create_params,
std::string* storage_partition_id,
bool* persist_storage) {
std::string partition_str;
if (!create_params.GetString(webview::kStoragePartitionId, &partition_str)) {
return;
}
// Since the "persist:" prefix is in ASCII, base::StartsWith will work fine on
// UTF-8 encoded |partition_id|. If the prefix is a match, we can safely
// remove the prefix without splicing in the middle of a multi-byte codepoint.
// We can use the rest of the string as UTF-8 encoded one.
if (base::StartsWith(partition_str, "persist:",
base::CompareCase::SENSITIVE)) {
size_t index = partition_str.find(":");
CHECK(index != std::string::npos);
// It is safe to do index + 1, since we tested for the full prefix above.
*storage_partition_id = partition_str.substr(index + 1);
if (storage_partition_id->empty()) {
// TODO(lazyboy): Better way to deal with this error.
return;
}
*persist_storage = true;
} else {
*storage_partition_id = partition_str;
*persist_storage = false;
}
}
void RemoveWebViewEventListenersOnIOThread(
void* profile,
int embedder_process_id,
int view_instance_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
ExtensionWebRequestEventRouter::GetInstance()->RemoveWebViewEventListeners(
profile,
embedder_process_id,
view_instance_id);
}
double ConvertZoomLevelToZoomFactor(double zoom_level) {
double zoom_factor = content::ZoomLevelToZoomFactor(zoom_level);
// Because the conversion from zoom level to zoom factor isn't perfect, the
// resulting zoom factor is rounded to the nearest 6th decimal place.
zoom_factor = round(zoom_factor * 1000000) / 1000000;
return zoom_factor;
}
using WebViewKey = std::pair<int, int>;
using WebViewKeyToIDMap = std::map<WebViewKey, int>;
static base::LazyInstance<WebViewKeyToIDMap> web_view_key_to_id_map =
LAZY_INSTANCE_INITIALIZER;
} // namespace
// static
void WebViewGuest::CleanUp(content::BrowserContext* browser_context,
int embedder_process_id,
int view_instance_id) {
GuestViewBase::CleanUp(browser_context, embedder_process_id,
view_instance_id);
// Clean up rules registries for the WebView.
WebViewKey key(embedder_process_id, view_instance_id);
auto it = web_view_key_to_id_map.Get().find(key);
if (it != web_view_key_to_id_map.Get().end()) {
auto rules_registry_id = it->second;
web_view_key_to_id_map.Get().erase(it);
RulesRegistryService* rrs =
RulesRegistryService::GetIfExists(browser_context);
if (rrs)
rrs->RemoveRulesRegistriesByID(rules_registry_id);
}
// Clean up web request event listeners for the WebView.
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
base::Bind(
&RemoveWebViewEventListenersOnIOThread,
browser_context,
embedder_process_id,
view_instance_id));
// Clean up content scripts for the WebView.
auto csm = WebViewContentScriptManager::Get(browser_context);
csm->RemoveAllContentScriptsForWebView(embedder_process_id, view_instance_id);
// Allow an extensions browser client to potentially perform more cleanup.
ExtensionsBrowserClient::Get()->CleanUpWebView(
browser_context, embedder_process_id, view_instance_id);
}
// static
GuestViewBase* WebViewGuest::Create(WebContents* owner_web_contents) {
return new WebViewGuest(owner_web_contents);
}
// static
bool WebViewGuest::GetGuestPartitionConfigForSite(
const GURL& site,
std::string* partition_domain,
std::string* partition_name,
bool* in_memory) {
if (!site.SchemeIs(content::kGuestScheme))
return false;
// Since guest URLs are only used for packaged apps, there must be an app
// id in the URL.
CHECK(site.has_host());
*partition_domain = site.host();
// Since persistence is optional, the path must either be empty or the
// literal string.
*in_memory = (site.path() != "/persist");
// The partition name is user supplied value, which we have encoded when the
// URL was created, so it needs to be decoded.
*partition_name =
net::UnescapeURLComponent(site.query(), net::UnescapeRule::NORMAL);
return true;
}
// static
std::string WebViewGuest::GetPartitionID(
const RenderProcessHost* render_process_host) {
WebViewRendererState* renderer_state = WebViewRendererState::GetInstance();
int process_id = render_process_host->GetID();
std::string partition_id;
if (renderer_state->IsGuest(process_id))
renderer_state->GetPartitionID(process_id, &partition_id);
return partition_id;
}
// static
const char WebViewGuest::Type[] = "webview";
// static
int WebViewGuest::GetOrGenerateRulesRegistryID(
int embedder_process_id,
int webview_instance_id) {
bool is_web_view = embedder_process_id && webview_instance_id;
if (!is_web_view)
return RulesRegistryService::kDefaultRulesRegistryID;
WebViewKey key = std::make_pair(embedder_process_id, webview_instance_id);
auto it = web_view_key_to_id_map.Get().find(key);
if (it != web_view_key_to_id_map.Get().end())
return it->second;
auto rph = RenderProcessHost::FromID(embedder_process_id);
int rules_registry_id =
RulesRegistryService::Get(rph->GetBrowserContext())->
GetNextRulesRegistryID();
web_view_key_to_id_map.Get()[key] = rules_registry_id;
return rules_registry_id;
}
bool WebViewGuest::CanRunInDetachedState() const {
return true;
}
void WebViewGuest::CreateWebContents(
const base::DictionaryValue& create_params,
const WebContentsCreatedCallback& callback) {
RenderProcessHost* owner_render_process_host =
owner_web_contents()->GetRenderProcessHost();
std::string storage_partition_id;
bool persist_storage = false;
ParsePartitionParam(create_params, &storage_partition_id, &persist_storage);
// Validate that the partition id coming from the renderer is valid UTF-8,
// since we depend on this in other parts of the code, such as FilePath
// creation. If the validation fails, treat it as a bad message and kill the
// renderer process.
if (!base::IsStringUTF8(storage_partition_id)) {
content::RecordAction(
base::UserMetricsAction("BadMessageTerminate_BPGM"));
owner_render_process_host->Shutdown(content::RESULT_CODE_KILLED_BAD_MESSAGE,
false);
callback.Run(nullptr);
return;
}
std::string url_encoded_partition = net::EscapeQueryParamValue(
storage_partition_id, false);
std::string partition_domain = GetOwnerSiteURL().host();
GURL guest_site(base::StringPrintf("%s://%s/%s?%s",
content::kGuestScheme,
partition_domain.c_str(),
persist_storage ? "persist" : "",
url_encoded_partition.c_str()));
// If we already have a webview tag in the same app using the same storage
// partition, we should use the same SiteInstance so the existing tag and
// the new tag can script each other.
auto guest_view_manager = GuestViewManager::FromBrowserContext(
owner_render_process_host->GetBrowserContext());
content::SiteInstance* guest_site_instance =
guest_view_manager->GetGuestSiteInstance(guest_site);
if (!guest_site_instance) {
// Create the SiteInstance in a new BrowsingInstance, which will ensure
// that webview tags are also not allowed to send messages across
// different partitions.
guest_site_instance = content::SiteInstance::CreateForURL(
owner_render_process_host->GetBrowserContext(), guest_site);
}
WebContents::CreateParams params(
owner_render_process_host->GetBrowserContext(),
guest_site_instance);
params.guest_delegate = this;
callback.Run(WebContents::Create(params));
}
void WebViewGuest::DidAttachToEmbedder() {
ApplyAttributes(*attach_params());
}
void WebViewGuest::DidDropLink(const GURL& url) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetString(guest_view::kUrl, url.spec());
DispatchEventToView(
new GuestViewEvent(webview::kEventDropLink, args.Pass()));
}
void WebViewGuest::DidInitialize(const base::DictionaryValue& create_params) {
script_executor_.reset(
new ScriptExecutor(web_contents(), &script_observers_));
notification_registrar_.Add(this,
content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME,
content::Source<WebContents>(web_contents()));
notification_registrar_.Add(this,
content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT,
content::Source<WebContents>(web_contents()));
if (web_view_guest_delegate_)
web_view_guest_delegate_->OnDidInitialize();
ExtensionsAPIClient::Get()->AttachWebContentsHelpers(web_contents());
web_view_permission_helper_.reset(new WebViewPermissionHelper(this));
rules_registry_id_ = GetOrGenerateRulesRegistryID(
owner_web_contents()->GetRenderProcessHost()->GetID(),
view_instance_id());
// We must install the mapping from guests to WebViews prior to resuming
// suspended resource loads so that the WebRequest API will catch resource
// requests.
PushWebViewStateToIOThread();
ApplyAttributes(create_params);
}
void WebViewGuest::ClearDataInternal(base::Time remove_since,
uint32 removal_mask,
const base::Closure& callback) {
uint32 storage_partition_removal_mask =
GetStoragePartitionRemovalMask(removal_mask);
if (!storage_partition_removal_mask) {
callback.Run();
return;
}
content::StoragePartition* partition =
content::BrowserContext::GetStoragePartition(
web_contents()->GetBrowserContext(),
web_contents()->GetSiteInstance());
partition->ClearData(
storage_partition_removal_mask,
content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL, GURL(),
content::StoragePartition::OriginMatcherFunction(), remove_since,
base::Time::Now(), callback);
}
void WebViewGuest::GuestViewDidStopLoading() {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
DispatchEventToView(
new GuestViewEvent(webview::kEventLoadStop, args.Pass()));
}
void WebViewGuest::EmbedderFullscreenToggled(bool entered_fullscreen) {
is_embedder_fullscreen_ = entered_fullscreen;
// If the embedder has got out of fullscreen, we get out of fullscreen
// mode as well.
if (!entered_fullscreen)
SetFullscreenState(false);
}
const char* WebViewGuest::GetAPINamespace() const {
return webview::kAPINamespace;
}
int WebViewGuest::GetTaskPrefix() const {
return IDS_EXTENSION_TASK_MANAGER_WEBVIEW_TAG_PREFIX;
}
void WebViewGuest::GuestDestroyed() {
RemoveWebViewStateFromIOThread(web_contents());
}
void WebViewGuest::GuestReady() {
// The guest RenderView should always live in an isolated guest process.
CHECK(web_contents()->GetRenderProcessHost()->IsForGuestsOnly());
Send(new ExtensionMsg_SetFrameName(web_contents()->GetRoutingID(), name_));
// We don't want to accidentally set the opacity of an interstitial page.
// WebContents::GetRenderWidgetHostView will return the RWHV of an
// interstitial page if one is showing at this time. We only want opacity
// to apply to web pages.
if (allow_transparency_) {
web_contents()->GetRenderViewHost()->GetView()->SetBackgroundColor(
SK_ColorTRANSPARENT);
} else {
web_contents()
->GetRenderViewHost()
->GetView()
->SetBackgroundColorToDefault();
}
}
void WebViewGuest::GuestSizeChangedDueToAutoSize(const gfx::Size& old_size,
const gfx::Size& new_size) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetInteger(webview::kOldHeight, old_size.height());
args->SetInteger(webview::kOldWidth, old_size.width());
args->SetInteger(webview::kNewHeight, new_size.height());
args->SetInteger(webview::kNewWidth, new_size.width());
DispatchEventToView(
new GuestViewEvent(webview::kEventSizeChanged, args.Pass()));
}
bool WebViewGuest::IsAutoSizeSupported() const {
return true;
}
void WebViewGuest::GuestZoomChanged(double old_zoom_level,
double new_zoom_level) {
// Dispatch the zoomchange event.
double old_zoom_factor = ConvertZoomLevelToZoomFactor(old_zoom_level);
double new_zoom_factor = ConvertZoomLevelToZoomFactor(new_zoom_level);
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetDouble(webview::kOldZoomFactor, old_zoom_factor);
args->SetDouble(webview::kNewZoomFactor, new_zoom_factor);
DispatchEventToView(
new GuestViewEvent(webview::kEventZoomChange, args.Pass()));
}
void WebViewGuest::WillDestroy() {
if (!attached() && GetOpener())
GetOpener()->pending_new_windows_.erase(this);
}
bool WebViewGuest::AddMessageToConsole(WebContents* source,
int32 level,
const base::string16& message,
int32 line_no,
const base::string16& source_id) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
// Log levels are from base/logging.h: LogSeverity.
args->SetInteger(webview::kLevel, level);
args->SetString(webview::kMessage, message);
args->SetInteger(webview::kLine, line_no);
args->SetString(webview::kSourceId, source_id);
DispatchEventToView(
new GuestViewEvent(webview::kEventConsoleMessage, args.Pass()));
return true;
}
void WebViewGuest::CloseContents(WebContents* source) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
DispatchEventToView(
new GuestViewEvent(webview::kEventClose, args.Pass()));
}
void WebViewGuest::FindReply(WebContents* source,
int request_id,
int number_of_matches,
const gfx::Rect& selection_rect,
int active_match_ordinal,
bool final_update) {
GuestViewBase::FindReply(source, request_id, number_of_matches,
selection_rect, active_match_ordinal, final_update);
find_helper_.FindReply(request_id, number_of_matches, selection_rect,
active_match_ordinal, final_update);
}
double WebViewGuest::GetZoom() const {
double zoom_level =
ZoomController::FromWebContents(web_contents())->GetZoomLevel();
return ConvertZoomLevelToZoomFactor(zoom_level);
}
ZoomController::ZoomMode WebViewGuest::GetZoomMode() {
return ZoomController::FromWebContents(web_contents())->zoom_mode();
}
bool WebViewGuest::HandleContextMenu(
const content::ContextMenuParams& params) {
if (!web_view_guest_delegate_)
return false;
return web_view_guest_delegate_->HandleContextMenu(params);
}
void WebViewGuest::HandleKeyboardEvent(
WebContents* source,
const content::NativeWebKeyboardEvent& event) {
if (HandleKeyboardShortcuts(event))
return;
GuestViewBase::HandleKeyboardEvent(source, event);
}
bool WebViewGuest::PreHandleGestureEvent(WebContents* source,
const blink::WebGestureEvent& event) {
return !allow_scaling_ && GuestViewBase::PreHandleGestureEvent(source, event);
}
void WebViewGuest::LoadProgressChanged(WebContents* source, double progress) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetString(guest_view::kUrl, web_contents()->GetURL().spec());
args->SetDouble(webview::kProgress, progress);
DispatchEventToView(
new GuestViewEvent(webview::kEventLoadProgress, args.Pass()));
}
void WebViewGuest::LoadAbort(bool is_top_level,
const GURL& url,
int error_code,
const std::string& error_type) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetBoolean(guest_view::kIsTopLevel, is_top_level);
args->SetString(guest_view::kUrl, url.possibly_invalid_spec());
args->SetInteger(guest_view::kCode, error_code);
args->SetString(guest_view::kReason, error_type);
DispatchEventToView(
new GuestViewEvent(webview::kEventLoadAbort, args.Pass()));
}
void WebViewGuest::SetContextMenuPosition(const gfx::Point& position) {
if (web_view_guest_delegate_)
web_view_guest_delegate_->SetContextMenuPosition(position);
}
void WebViewGuest::CreateNewGuestWebViewWindow(
const content::OpenURLParams& params) {
GuestViewManager* guest_manager =
GuestViewManager::FromBrowserContext(browser_context());
// Set the attach params to use the same partition as the opener.
// We pull the partition information from the site's URL, which is of the
// form guest://site/{persist}?{partition_name}.
const GURL& site_url = web_contents()->GetSiteInstance()->GetSiteURL();
const std::string storage_partition_id =
GetStoragePartitionIdFromSiteURL(site_url);
base::DictionaryValue create_params;
create_params.SetString(webview::kStoragePartitionId, storage_partition_id);
guest_manager->CreateGuest(WebViewGuest::Type,
embedder_web_contents(),
create_params,
base::Bind(&WebViewGuest::NewGuestWebViewCallback,
weak_ptr_factory_.GetWeakPtr(),
params));
}
void WebViewGuest::NewGuestWebViewCallback(const content::OpenURLParams& params,
WebContents* guest_web_contents) {
WebViewGuest* new_guest = WebViewGuest::FromWebContents(guest_web_contents);
new_guest->SetOpener(this);
// Take ownership of |new_guest|.
pending_new_windows_.insert(
std::make_pair(new_guest, NewWindowInfo(params.url, std::string())));
// Request permission to show the new window.
RequestNewWindowPermission(params.disposition,
gfx::Rect(),
params.user_gesture,
new_guest->web_contents());
}
// TODO(fsamuel): Find a reliable way to test the 'responsive' and
// 'unresponsive' events.
void WebViewGuest::RendererResponsive(WebContents* source) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetInteger(webview::kProcessId,
web_contents()->GetRenderProcessHost()->GetID());
DispatchEventToView(
new GuestViewEvent(webview::kEventResponsive, args.Pass()));
}
void WebViewGuest::RendererUnresponsive(WebContents* source) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetInteger(webview::kProcessId,
web_contents()->GetRenderProcessHost()->GetID());
DispatchEventToView(
new GuestViewEvent(webview::kEventUnresponsive, args.Pass()));
}
void WebViewGuest::Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) {
switch (type) {
case content::NOTIFICATION_LOAD_COMPLETED_MAIN_FRAME: {
DCHECK_EQ(content::Source<WebContents>(source).ptr(), web_contents());
if (content::Source<WebContents>(source).ptr() == web_contents())
LoadHandlerCalled();
break;
}
case content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT: {
DCHECK_EQ(content::Source<WebContents>(source).ptr(), web_contents());
content::ResourceRedirectDetails* resource_redirect_details =
content::Details<content::ResourceRedirectDetails>(details).ptr();
bool is_top_level = resource_redirect_details->resource_type ==
content::RESOURCE_TYPE_MAIN_FRAME;
LoadRedirect(resource_redirect_details->url,
resource_redirect_details->new_url,
is_top_level);
break;
}
default:
NOTREACHED() << "Unexpected notification sent.";
break;
}
}
void WebViewGuest::StartFindInternal(
const base::string16& search_text,
const blink::WebFindOptions& options,
scoped_refptr<WebViewInternalFindFunction> find_function) {
find_helper_.Find(web_contents(), search_text, options, find_function);
}
void WebViewGuest::StopFindingInternal(content::StopFindAction action) {
find_helper_.CancelAllFindSessions();
web_contents()->StopFinding(action);
}
bool WebViewGuest::Go(int relative_index) {
content::NavigationController& controller = web_contents()->GetController();
if (!controller.CanGoToOffset(relative_index))
return false;
controller.GoToOffset(relative_index);
return true;
}
void WebViewGuest::Reload() {
// TODO(fsamuel): Don't check for repost because we don't want to show
// Chromium's repost warning. We might want to implement a separate API
// for registering a callback if a repost is about to happen.
web_contents()->GetController().Reload(false);
}
void WebViewGuest::SetUserAgentOverride(
const std::string& user_agent_override) {
is_overriding_user_agent_ = !user_agent_override.empty();
if (is_overriding_user_agent_) {
content::RecordAction(UserMetricsAction("WebView.Guest.OverrideUA"));
}
web_contents()->SetUserAgentOverride(user_agent_override);
}
void WebViewGuest::Stop() {
web_contents()->Stop();
}
void WebViewGuest::Terminate() {
content::RecordAction(UserMetricsAction("WebView.Guest.Terminate"));
base::ProcessHandle process_handle =
web_contents()->GetRenderProcessHost()->GetHandle();
if (process_handle)
web_contents()->GetRenderProcessHost()->Shutdown(
content::RESULT_CODE_KILLED, false);
}
bool WebViewGuest::ClearData(base::Time remove_since,
uint32 removal_mask,
const base::Closure& callback) {
content::RecordAction(UserMetricsAction("WebView.Guest.ClearData"));
content::StoragePartition* partition =
content::BrowserContext::GetStoragePartition(
web_contents()->GetBrowserContext(),
web_contents()->GetSiteInstance());
if (!partition)
return false;
if (removal_mask & webview::WEB_VIEW_REMOVE_DATA_MASK_CACHE) {
// First clear http cache data and then clear the rest in
// |ClearDataInternal|.
int render_process_id = web_contents()->GetRenderProcessHost()->GetID();
// We need to clear renderer cache separately for our process because
// StoragePartitionHttpCacheDataRemover::ClearData() does not clear that.
web_cache::WebCacheManager::GetInstance()->Remove(render_process_id);
web_cache::WebCacheManager::GetInstance()->ClearCacheForProcess(
render_process_id);
base::Closure cache_removal_done_callback = base::Bind(
&WebViewGuest::ClearDataInternal, weak_ptr_factory_.GetWeakPtr(),
remove_since, removal_mask, callback);
// StoragePartitionHttpCacheDataRemover removes itself when it is done.
// components/, move |ClearCache| to WebViewGuest: http//crbug.com/471287.
browsing_data::StoragePartitionHttpCacheDataRemover::CreateForRange(
partition, remove_since, base::Time::Now())
->Remove(cache_removal_done_callback);
return true;
}
ClearDataInternal(remove_since, removal_mask, callback);
return true;
}
WebViewGuest::WebViewGuest(WebContents* owner_web_contents)
: GuestView<WebViewGuest>(owner_web_contents),
rules_registry_id_(RulesRegistryService::kInvalidRulesRegistryID),
find_helper_(this),
is_overriding_user_agent_(false),
allow_transparency_(false),
javascript_dialog_helper_(this),
allow_scaling_(false),
is_guest_fullscreen_(false),
is_embedder_fullscreen_(false),
last_fullscreen_permission_was_allowed_by_embedder_(false),
pending_zoom_factor_(0.0),
weak_ptr_factory_(this) {
web_view_guest_delegate_.reset(
ExtensionsAPIClient::Get()->CreateWebViewGuestDelegate(this));
}
WebViewGuest::~WebViewGuest() {
}
void WebViewGuest::DidCommitProvisionalLoadForFrame(
content::RenderFrameHost* render_frame_host,
const GURL& url,
ui::PageTransition transition_type) {
if (!render_frame_host->GetParent()) {
src_ = url;
// Handle a pending zoom if one exists.
if (pending_zoom_factor_) {
SetZoom(pending_zoom_factor_);
pending_zoom_factor_ = 0.0;
}
}
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetString(guest_view::kUrl, url.spec());
args->SetBoolean(guest_view::kIsTopLevel, !render_frame_host->GetParent());
args->SetString(webview::kInternalBaseURLForDataURL,
web_contents()
->GetController()
.GetLastCommittedEntry()
->GetBaseURLForDataURL()
.spec());
args->SetInteger(webview::kInternalCurrentEntryIndex,
web_contents()->GetController().GetCurrentEntryIndex());
args->SetInteger(webview::kInternalEntryCount,
web_contents()->GetController().GetEntryCount());
args->SetInteger(webview::kInternalProcessId,
web_contents()->GetRenderProcessHost()->GetID());
DispatchEventToView(
new GuestViewEvent(webview::kEventLoadCommit, args.Pass()));
find_helper_.CancelAllFindSessions();
}
void WebViewGuest::DidFailProvisionalLoad(
content::RenderFrameHost* render_frame_host,
const GURL& validated_url,
int error_code,
const base::string16& error_description,
bool was_ignored_by_handler) {
// Suppress loadabort for "mailto" URLs.
if (validated_url.SchemeIs(url::kMailToScheme))
return;
LoadAbort(!render_frame_host->GetParent(), validated_url, error_code,
net::ErrorToShortString(error_code));
}
void WebViewGuest::DidStartProvisionalLoadForFrame(
content::RenderFrameHost* render_frame_host,
const GURL& validated_url,
bool is_error_page,
bool is_iframe_srcdoc) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetString(guest_view::kUrl, validated_url.spec());
args->SetBoolean(guest_view::kIsTopLevel, !render_frame_host->GetParent());
DispatchEventToView(
new GuestViewEvent(webview::kEventLoadStart, args.Pass()));
}
void WebViewGuest::RenderProcessGone(base::TerminationStatus status) {
// Cancel all find sessions in progress.
find_helper_.CancelAllFindSessions();
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetInteger(webview::kProcessId,
web_contents()->GetRenderProcessHost()->GetID());
args->SetString(webview::kReason, TerminationStatusToString(status));
DispatchEventToView(
new GuestViewEvent(webview::kEventExit, args.Pass()));
}
void WebViewGuest::UserAgentOverrideSet(const std::string& user_agent) {
content::NavigationController& controller = web_contents()->GetController();
content::NavigationEntry* entry = controller.GetVisibleEntry();
if (!entry)
return;
entry->SetIsOverridingUserAgent(!user_agent.empty());
web_contents()->GetController().Reload(false);
}
void WebViewGuest::FrameNameChanged(RenderFrameHost* render_frame_host,
const std::string& name) {
if (render_frame_host->GetParent())
return;
if (name_ == name)
return;
ReportFrameNameChange(name);
}
void WebViewGuest::ReportFrameNameChange(const std::string& name) {
name_ = name;
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetString(webview::kName, name);
DispatchEventToView(
new GuestViewEvent(webview::kEventFrameNameChanged, args.Pass()));
}
void WebViewGuest::LoadHandlerCalled() {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
DispatchEventToView(
new GuestViewEvent(webview::kEventContentLoad, args.Pass()));
}
void WebViewGuest::LoadRedirect(const GURL& old_url,
const GURL& new_url,
bool is_top_level) {
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
args->SetBoolean(guest_view::kIsTopLevel, is_top_level);
args->SetString(webview::kNewURL, new_url.spec());
args->SetString(webview::kOldURL, old_url.spec());
DispatchEventToView(
new GuestViewEvent(webview::kEventLoadRedirect, args.Pass()));
}
void WebViewGuest::PushWebViewStateToIOThread() {
const GURL& site_url = web_contents()->GetSiteInstance()->GetSiteURL();
std::string partition_domain;
std::string partition_id;
bool in_memory;
if (!GetGuestPartitionConfigForSite(
site_url, &partition_domain, &partition_id, &in_memory)) {
NOTREACHED();
return;
}
WebViewRendererState::WebViewInfo web_view_info;
web_view_info.embedder_process_id =
owner_web_contents()->GetRenderProcessHost()->GetID();
web_view_info.instance_id = view_instance_id();
web_view_info.partition_id = partition_id;
web_view_info.owner_host = owner_host();
web_view_info.rules_registry_id = rules_registry_id_;
// Get content scripts IDs added by the guest.
WebViewContentScriptManager* manager =
WebViewContentScriptManager::Get(browser_context());
DCHECK(manager);
web_view_info.content_script_ids = manager->GetContentScriptIDSet(
web_view_info.embedder_process_id, web_view_info.instance_id);
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
base::Bind(&WebViewRendererState::AddGuest,
base::Unretained(WebViewRendererState::GetInstance()),
web_contents()->GetRenderProcessHost()->GetID(),
web_contents()->GetRoutingID(),
web_view_info));
}
// static
void WebViewGuest::RemoveWebViewStateFromIOThread(
WebContents* web_contents) {
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(
&WebViewRendererState::RemoveGuest,
base::Unretained(WebViewRendererState::GetInstance()),
web_contents->GetRenderProcessHost()->GetID(),
web_contents->GetRoutingID()));
}
void WebViewGuest::RequestMediaAccessPermission(
WebContents* source,
const content::MediaStreamRequest& request,
const content::MediaResponseCallback& callback) {
web_view_permission_helper_->RequestMediaAccessPermission(source,
request,
callback);
}
bool WebViewGuest::CheckMediaAccessPermission(WebContents* source,
const GURL& security_origin,
content::MediaStreamType type) {
return web_view_permission_helper_->CheckMediaAccessPermission(
source, security_origin, type);
}
void WebViewGuest::CanDownload(
const GURL& url,
const std::string& request_method,
const base::Callback<void(bool)>& callback) {
web_view_permission_helper_->CanDownload(url, request_method, callback);
}
void WebViewGuest::RequestPointerLockPermission(
bool user_gesture,
bool last_unlocked_by_target,
const base::Callback<void(bool)>& callback) {
web_view_permission_helper_->RequestPointerLockPermission(
user_gesture,
last_unlocked_by_target,
callback);
}
void WebViewGuest::SignalWhenReady(const base::Closure& callback) {
auto manager = WebViewContentScriptManager::Get(browser_context());
manager->SignalOnScriptsLoaded(callback);
}
bool WebViewGuest::ShouldHandleFindRequestsForEmbedder() const {
if (web_view_guest_delegate_)
return web_view_guest_delegate_->ShouldHandleFindRequestsForEmbedder();
return false;
}
void WebViewGuest::WillAttachToEmbedder() {
rules_registry_id_ = GetOrGenerateRulesRegistryID(
owner_web_contents()->GetRenderProcessHost()->GetID(),
view_instance_id());
// We must install the mapping from guests to WebViews prior to resuming
// suspended resource loads so that the WebRequest API will catch resource
// requests.
PushWebViewStateToIOThread();
}
content::JavaScriptDialogManager* WebViewGuest::GetJavaScriptDialogManager(
WebContents* source) {
return &javascript_dialog_helper_;
}
void WebViewGuest::NavigateGuest(const std::string& src,
bool force_navigation) {
if (src.empty())
return;
GURL url = ResolveURL(src);
// We wait for all the content scripts to load and then navigate the guest
// if the navigation is embedder-initiated. For browser-initiated navigations,
// content scripts will be ready.
if (force_navigation) {
SignalWhenReady(base::Bind(
&WebViewGuest::LoadURLWithParams, weak_ptr_factory_.GetWeakPtr(), url,
content::Referrer(), ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
GlobalRequestID(), force_navigation));
return;
}
LoadURLWithParams(url, content::Referrer(), ui::PAGE_TRANSITION_AUTO_TOPLEVEL,
GlobalRequestID(), force_navigation);
}
bool WebViewGuest::HandleKeyboardShortcuts(
const content::NativeWebKeyboardEvent& event) {
// <webview> outside of Chrome Apps do not handle keyboard shortcuts.
if (!GuestViewManager::FromBrowserContext(browser_context())->
IsOwnedByExtension(this)) {
return false;
}
if (event.type != blink::WebInputEvent::RawKeyDown)
return false;
// If the user hits the escape key without any modifiers then unlock the
// mouse if necessary.
if ((event.windowsKeyCode == ui::VKEY_ESCAPE) &&
!(event.modifiers & blink::WebInputEvent::InputModifiers)) {
return web_contents()->GotResponseToLockMouseRequest(false);
}
#if defined(OS_MACOSX)
if (event.modifiers != blink::WebInputEvent::MetaKey)
return false;
if (event.windowsKeyCode == ui::VKEY_OEM_4) {
Go(-1);
return true;
}
if (event.windowsKeyCode == ui::VKEY_OEM_6) {
Go(1);
return true;
}
#else
if (event.windowsKeyCode == ui::VKEY_BROWSER_BACK) {
Go(-1);
return true;
}
if (event.windowsKeyCode == ui::VKEY_BROWSER_FORWARD) {
Go(1);
return true;
}
#endif
return false;
}
void WebViewGuest::ApplyAttributes(const base::DictionaryValue& params) {
std::string name;
if (params.GetString(webview::kAttributeName, &name)) {
// If the guest window's name is empty, then the WebView tag's name is
// assigned. Otherwise, the guest window's name takes precedence over the
// WebView tag's name.
if (name_.empty())
SetName(name);
}
if (attached())
ReportFrameNameChange(name_);
std::string user_agent_override;
params.GetString(webview::kParameterUserAgentOverride, &user_agent_override);
SetUserAgentOverride(user_agent_override);
bool allow_transparency = false;
if (params.GetBoolean(webview::kAttributeAllowTransparency,
&allow_transparency)) {
// We need to set the background opaque flag after navigation to ensure that
// there is a RenderWidgetHostView available.
SetAllowTransparency(allow_transparency);
}
bool allow_scaling = false;
if (params.GetBoolean(webview::kAttributeAllowScaling, &allow_scaling))
SetAllowScaling(allow_scaling);
// Check for a pending zoom from before the first navigation.
params.GetDouble(webview::kInitialZoomFactor, &pending_zoom_factor_);
bool is_pending_new_window = false;
if (GetOpener()) {
// We need to do a navigation here if the target URL has changed between
// the time the WebContents was created and the time it was attached.
// We also need to do an initial navigation if a RenderView was never
// created for the new window in cases where there is no referrer.
auto it = GetOpener()->pending_new_windows_.find(this);
if (it != GetOpener()->pending_new_windows_.end()) {
const NewWindowInfo& new_window_info = it->second;
if (new_window_info.changed || !web_contents()->HasOpener())
NavigateGuest(new_window_info.url.spec(), false /* force_navigation */);
// Once a new guest is attached to the DOM of the embedder page, then the
// lifetime of the new guest is no longer managed by the opener guest.
GetOpener()->pending_new_windows_.erase(this);
is_pending_new_window = true;
}
}
// Only read the src attribute if this is not a New Window API flow.
if (!is_pending_new_window) {
std::string src;
if (params.GetString(webview::kAttributeSrc, &src))
NavigateGuest(src, true /* force_navigation */);
}
}
void WebViewGuest::ShowContextMenu(
int request_id,
const WebViewGuestDelegate::MenuItemVector* items) {
if (web_view_guest_delegate_)
web_view_guest_delegate_->OnShowContextMenu(request_id, items);
}
void WebViewGuest::SetName(const std::string& name) {
if (name_ == name)
return;
name_ = name;
Send(new ExtensionMsg_SetFrameName(routing_id(), name_));
}
void WebViewGuest::SetZoom(double zoom_factor) {
auto zoom_controller = ZoomController::FromWebContents(web_contents());
DCHECK(zoom_controller);
double zoom_level = content::ZoomFactorToZoomLevel(zoom_factor);
zoom_controller->SetZoomLevel(zoom_level);
}
void WebViewGuest::SetZoomMode(ZoomController::ZoomMode zoom_mode) {
ZoomController::FromWebContents(web_contents())->SetZoomMode(zoom_mode);
}
void WebViewGuest::SetAllowTransparency(bool allow) {
if (allow_transparency_ == allow)
return;
allow_transparency_ = allow;
if (!web_contents()->GetRenderViewHost()->GetView())
return;
if (allow_transparency_) {
web_contents()->GetRenderViewHost()->GetView()->SetBackgroundColor(
SK_ColorTRANSPARENT);
} else {
web_contents()
->GetRenderViewHost()
->GetView()
->SetBackgroundColorToDefault();
}
}
void WebViewGuest::SetAllowScaling(bool allow) {
allow_scaling_ = allow;
}
bool WebViewGuest::LoadDataWithBaseURL(const std::string& data_url,
const std::string& base_url,
const std::string& virtual_url,
std::string* error) {
// Make GURLs from URLs.
const GURL data_gurl = GURL(data_url);
const GURL base_gurl = GURL(base_url);
const GURL virtual_gurl = GURL(virtual_url);
// Check that the provided URLs are valid.
// |data_url| must be a valid data URL.
if (!data_gurl.is_valid() || !data_gurl.SchemeIs(url::kDataScheme)) {
base::SStringPrintf(
error, webview::kAPILoadDataInvalidDataURL, data_url.c_str());
return false;
}
// |base_url| must be a valid URL.
if (!base_gurl.is_valid()) {
base::SStringPrintf(
error, webview::kAPILoadDataInvalidBaseURL, base_url.c_str());
return false;
}
// |virtual_url| must be a valid URL.
if (!virtual_gurl.is_valid()) {
base::SStringPrintf(
error, webview::kAPILoadDataInvalidVirtualURL, virtual_url.c_str());
return false;
}
// Set up the parameters to load |data_url| with the specified |base_url|.
content::NavigationController::LoadURLParams load_params(data_gurl);
load_params.load_type = content::NavigationController::LOAD_TYPE_DATA;
load_params.base_url_for_data_url = base_gurl;
load_params.virtual_url_for_data_url = virtual_gurl;
load_params.override_user_agent =
content::NavigationController::UA_OVERRIDE_INHERIT;
// Navigate to the data URL.
GuestViewBase::LoadURLWithParams(load_params);
return true;
}
void WebViewGuest::AddNewContents(WebContents* source,
WebContents* new_contents,
WindowOpenDisposition disposition,
const gfx::Rect& initial_rect,
bool user_gesture,
bool* was_blocked) {
if (was_blocked)
*was_blocked = false;
RequestNewWindowPermission(disposition,
initial_rect,
user_gesture,
new_contents);
}
WebContents* WebViewGuest::OpenURLFromTab(
WebContents* source,
const content::OpenURLParams& params) {
// There are two use cases to consider from a security perspective:
// 1.) Renderer-initiated navigation to chrome:// must always be blocked even
// if the <webview> is in WebUI. This is handled by
// WebViewGuest::LoadURLWithParams. WebViewGuest::NavigateGuest will also
// call LoadURLWithParams. CreateNewGuestWebViewWindow creates a new
// WebViewGuest which will call NavigateGuest in DidInitialize.
// 2.) The Language Settings context menu item should always work, both in
// Chrome Apps and WebUI. This is a browser initiated request and so
// we pass it along to the embedder's WebContentsDelegate to get the
// browser to perform the action for the <webview>.
if (!params.is_renderer_initiated) {
if (!owner_web_contents()->GetDelegate())
return nullptr;
return owner_web_contents()->GetDelegate()->OpenURLFromTab(
owner_web_contents(), params);
}
if (!attached()) {
WebViewGuest* opener = GetOpener();
// If the guest wishes to navigate away prior to attachment then we save the
// navigation to perform upon attachment. Navigation initializes a lot of
// state that assumes an embedder exists, such as RenderWidgetHostViewGuest.
// Navigation also resumes resource loading. If we were created using
// newwindow (i.e. we have an opener), we don't allow navigation until
// attachment.
if (opener) {
auto it = opener->pending_new_windows_.find(this);
if (it == opener->pending_new_windows_.end())
return nullptr;
const NewWindowInfo& info = it->second;
NewWindowInfo new_window_info(params.url, info.name);
new_window_info.changed = new_window_info.url != info.url;
it->second = new_window_info;
return nullptr;
}
}
// This code path is taken if RenderFrameImpl::DecidePolicyForNavigation
// decides that a fork should happen. At the time of writing this comment,
// the only way a well behaving guest could hit this code path is if it
// navigates to a URL that's associated with the default search engine.
// This list of URLs is generated by search::GetSearchURLs. Validity checks
// are performed inside LoadURLWithParams such that if the guest attempts
// to navigate to a URL that it is not allowed to navigate to, a 'loadabort'
// event will fire in the embedder, and the guest will be navigated to
// about:blank.
if (params.disposition == CURRENT_TAB) {
LoadURLWithParams(params.url, params.referrer, params.transition,
params.transferred_global_request_id,
true /* force_navigation */);
return web_contents();
}
// This code path is taken if Ctrl+Click, middle click or any of the
// keyboard/mouse combinations are used to open a link in a new tab/window.
// This code path is also taken on client-side redirects from about:blank.
CreateNewGuestWebViewWindow(params);
return nullptr;
}
void WebViewGuest::WebContentsCreated(WebContents* source_contents,
int opener_render_frame_id,
const std::string& frame_name,
const GURL& target_url,
WebContents* new_contents) {
auto guest = WebViewGuest::FromWebContents(new_contents);
CHECK(guest);
guest->SetOpener(this);
guest->name_ = frame_name;
pending_new_windows_.insert(
std::make_pair(guest, NewWindowInfo(target_url, frame_name)));
}
void WebViewGuest::EnterFullscreenModeForTab(WebContents* web_contents,
const GURL& origin) {
// Ask the embedder for permission.
base::DictionaryValue request_info;
request_info.SetString(webview::kOrigin, origin.spec());
web_view_permission_helper_->RequestPermission(
WEB_VIEW_PERMISSION_TYPE_FULLSCREEN, request_info,
base::Bind(&WebViewGuest::OnFullscreenPermissionDecided,
weak_ptr_factory_.GetWeakPtr()),
false /* allowed_by_default */);
// TODO(lazyboy): Right now the guest immediately goes fullscreen within its
// bounds. If the embedder denies the permission then we will see a flicker.
// Once we have the ability to "cancel" a renderer/ fullscreen request:
// http://crbug.com/466854 this won't be necessary and we should be
// Calling SetFullscreenState(true) once the embedder allowed the request.
// Otherwise we would cancel renderer/ fullscreen if the embedder denied.
SetFullscreenState(true);
}
void WebViewGuest::ExitFullscreenModeForTab(WebContents* web_contents) {
SetFullscreenState(false);
}
bool WebViewGuest::IsFullscreenForTabOrPending(
const WebContents* web_contents) const {
return is_guest_fullscreen_;
}
void WebViewGuest::LoadURLWithParams(
const GURL& url,
const content::Referrer& referrer,
ui::PageTransition transition_type,
const GlobalRequestID& transferred_global_request_id,
bool force_navigation) {
if (!url.is_valid()) {
LoadAbort(true /* is_top_level */, url, net::ERR_INVALID_URL,
net::ErrorToShortString(net::ERR_INVALID_URL));
NavigateGuest(url::kAboutBlankURL, false /* force_navigation */);
return;
}
bool scheme_is_blocked =
(!content::ChildProcessSecurityPolicy::GetInstance()->IsWebSafeScheme(
url.scheme()) &&
!url.SchemeIs(url::kAboutScheme)) ||
url.SchemeIs(url::kJavaScriptScheme);
// Do not allow navigating a guest to schemes other than known safe schemes.
// This will block the embedder trying to load unwanted schemes, e.g.
// chrome://.
if (scheme_is_blocked) {
LoadAbort(true /* is_top_level */, url, net::ERR_DISALLOWED_URL_SCHEME,
net::ErrorToShortString(net::ERR_DISALLOWED_URL_SCHEME));
NavigateGuest(url::kAboutBlankURL, false /* force_navigation */);
return;
}
if (!force_navigation && (src_ == url))
return;
GURL validated_url(url);
web_contents()->GetRenderProcessHost()->FilterURL(false, &validated_url);
// As guests do not swap processes on navigation, only navigations to
// normal web URLs are supported. No protocol handlers are installed for
// other schemes (e.g., WebUI or extensions), and no permissions or bindings
// can be granted to the guest process.
content::NavigationController::LoadURLParams load_url_params(validated_url);
load_url_params.referrer = referrer;
load_url_params.transition_type = transition_type;
load_url_params.extra_headers = std::string();
load_url_params.transferred_global_request_id = transferred_global_request_id;
if (is_overriding_user_agent_) {
load_url_params.override_user_agent =
content::NavigationController::UA_OVERRIDE_TRUE;
}
GuestViewBase::LoadURLWithParams(load_url_params);
src_ = validated_url;
}
void WebViewGuest::RequestNewWindowPermission(WindowOpenDisposition disposition,
const gfx::Rect& initial_bounds,
bool user_gesture,
WebContents* new_contents) {
auto guest = WebViewGuest::FromWebContents(new_contents);
if (!guest)
return;
auto it = pending_new_windows_.find(guest);
if (it == pending_new_windows_.end())
return;
const NewWindowInfo& new_window_info = it->second;
// Retrieve the opener partition info if we have it.
const GURL& site_url = new_contents->GetSiteInstance()->GetSiteURL();
std::string storage_partition_id = GetStoragePartitionIdFromSiteURL(site_url);
base::DictionaryValue request_info;
request_info.SetInteger(webview::kInitialHeight, initial_bounds.height());
request_info.SetInteger(webview::kInitialWidth, initial_bounds.width());
request_info.Set(webview::kTargetURL,
new base::StringValue(new_window_info.url.spec()));
request_info.Set(webview::kName, new base::StringValue(new_window_info.name));
request_info.SetInteger(webview::kWindowID, guest->guest_instance_id());
// We pass in partition info so that window-s created through newwindow
// API can use it to set their partition attribute.
request_info.Set(webview::kStoragePartitionId,
new base::StringValue(storage_partition_id));
request_info.Set(
webview::kWindowOpenDisposition,
new base::StringValue(WindowOpenDispositionToString(disposition)));
web_view_permission_helper_->
RequestPermission(WEB_VIEW_PERMISSION_TYPE_NEW_WINDOW,
request_info,
base::Bind(&WebViewGuest::OnWebViewNewWindowResponse,
weak_ptr_factory_.GetWeakPtr(),
guest->guest_instance_id()),
false /* allowed_by_default */);
}
GURL WebViewGuest::ResolveURL(const std::string& src) {
if (!GuestViewManager::FromBrowserContext(browser_context())->
IsOwnedByExtension(this)) {
return GURL(src);
}
GURL default_url(base::StringPrintf("%s://%s/",
kExtensionScheme,
owner_host().c_str()));
return default_url.Resolve(src);
}
void WebViewGuest::OnWebViewNewWindowResponse(
int new_window_instance_id,
bool allow,
const std::string& user_input) {
auto guest =
WebViewGuest::From(owner_web_contents()->GetRenderProcessHost()->GetID(),
new_window_instance_id);
if (!guest)
return;
if (!allow)
guest->Destroy();
}
void WebViewGuest::OnFullscreenPermissionDecided(
bool allowed,
const std::string& user_input) {
last_fullscreen_permission_was_allowed_by_embedder_ = allowed;
SetFullscreenState(allowed);
}
bool WebViewGuest::GuestMadeEmbedderFullscreen() const {
return last_fullscreen_permission_was_allowed_by_embedder_ &&
is_embedder_fullscreen_;
}
void WebViewGuest::SetFullscreenState(bool is_fullscreen) {
if (is_fullscreen == is_guest_fullscreen_)
return;
bool was_fullscreen = is_guest_fullscreen_;
is_guest_fullscreen_ = is_fullscreen;
// If the embedder entered fullscreen because of us, it should exit fullscreen
// when we exit fullscreen.
if (was_fullscreen && GuestMadeEmbedderFullscreen()) {
// Dispatch a message so we can call document.webkitCancelFullscreen()
// on the embedder.
scoped_ptr<base::DictionaryValue> args(new base::DictionaryValue());
DispatchEventToView(
new GuestViewEvent(webview::kEventExitFullscreen, args.Pass()));
}
// Since we changed fullscreen state, sending a Resize message ensures that
// renderer/ sees the change.
web_contents()->GetRenderViewHost()->WasResized();
}
} // namespace extensions
|