forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_contents_impl.cc
3671 lines (3148 loc) · 132 KB
/
web_contents_impl.cc
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
// Copyright (c) 2012 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 "content/browser/web_contents/web_contents_impl.h"
#include <utility>
#include "base/command_line.h"
#include "base/debug/trace_event.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/metrics/stats_counters.h"
#include "base/process/process.h"
#include "base/strings/string16.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "content/browser/browser_plugin/browser_plugin_embedder.h"
#include "content/browser/browser_plugin/browser_plugin_guest.h"
#include "content/browser/browser_plugin/browser_plugin_guest_manager.h"
#include "content/browser/child_process_security_policy_impl.h"
#include "content/browser/devtools/devtools_manager_impl.h"
#include "content/browser/dom_storage/dom_storage_context_wrapper.h"
#include "content/browser/dom_storage/session_storage_namespace_impl.h"
#include "content/browser/download/download_stats.h"
#include "content/browser/download/mhtml_generation_manager.h"
#include "content/browser/download/save_package.h"
#include "content/browser/frame_host/cross_process_frame_connector.h"
#include "content/browser/frame_host/interstitial_page_impl.h"
#include "content/browser/frame_host/navigation_entry_impl.h"
#include "content/browser/frame_host/navigator_impl.h"
#include "content/browser/frame_host/render_frame_host_impl.h"
#include "content/browser/frame_host/render_widget_host_view_child_frame.h"
#include "content/browser/host_zoom_map_impl.h"
#include "content/browser/loader/resource_dispatcher_host_impl.h"
#include "content/browser/message_port_message_filter.h"
#include "content/browser/message_port_service.h"
#include "content/browser/power_save_blocker_impl.h"
#include "content/browser/renderer_host/render_process_host_impl.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/site_instance_impl.h"
#include "content/browser/web_contents/web_contents_view_guest.h"
#include "content/browser/webui/generic_handler.h"
#include "content/browser/webui/web_ui_controller_factory_registry.h"
#include "content/browser/webui/web_ui_impl.h"
#include "content/common/browser_plugin/browser_plugin_constants.h"
#include "content/common/browser_plugin/browser_plugin_messages.h"
#include "content/common/frame_messages.h"
#include "content/common/image_messages.h"
#include "content/common/ssl_status_serialization.h"
#include "content/common/view_messages.h"
#include "content/port/browser/render_view_host_delegate_view.h"
#include "content/port/browser/render_widget_host_view_port.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/color_chooser.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/download_manager.h"
#include "content/public/browser/download_url_parameters.h"
#include "content/public/browser/invalidate_type.h"
#include "content/public/browser/javascript_dialog_manager.h"
#include "content/public/browser/load_from_memory_cache_details.h"
#include "content/public/browser/load_notification_details.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/resource_request_details.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/user_metrics.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/common/bindings_policy.h"
#include "content/public/common/content_constants.h"
#include "content/public/common/content_switches.h"
#include "content/public/common/page_zoom.h"
#include "content/public/common/result_codes.h"
#include "content/public/common/url_constants.h"
#include "content/public/common/url_utils.h"
#include "net/base/mime_util.h"
#include "net/base/net_util.h"
#include "net/http/http_cache.h"
#include "net/http/http_transaction_factory.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "ui/base/layout.h"
#include "ui/gfx/display.h"
#include "ui/gfx/screen.h"
#include "ui/gl/gl_switches.h"
#include "webkit/common/webpreferences.h"
#if defined(OS_ANDROID)
#include "content/browser/android/date_time_chooser_android.h"
#include "content/browser/renderer_host/java/java_bridge_dispatcher_host_manager.h"
#include "content/browser/web_contents/web_contents_android.h"
#include "content/common/java_bridge_messages.h"
#include "content/public/browser/android/content_view_core.h"
#endif
#if defined(OS_MACOSX)
#include "base/mac/foundation_util.h"
#include "ui/gl/io_surface_support_mac.h"
#endif
// Cross-Site Navigations
//
// If a WebContentsImpl is told to navigate to a different web site (as
// determined by SiteInstance), it will replace its current RenderViewHost with
// a new RenderViewHost dedicated to the new SiteInstance. This works as
// follows:
//
// - RVHM::Navigate determines whether the destination is cross-site, and if so,
// it creates a pending_render_view_host_.
// - The pending RVH is "suspended," so that no navigation messages are sent to
// its renderer until the beforeunload JavaScript handler has a chance to
// run in the current RVH.
// - The pending RVH tells CrossSiteRequestManager (a thread-safe singleton)
// that it has a pending cross-site request. We will check this on the IO
// thread when deciding how to handle the response.
// - The current RVH runs its beforeunload handler. If it returns false, we
// cancel all the pending logic. Otherwise we allow the pending RVH to send
// the navigation request to its renderer.
// - ResourceDispatcherHost receives a ResourceRequest on the IO thread for the
// main resource load on the pending RVH. It creates a
// CrossSiteResourceHandler to check whether a process swap is needed when
// the request is ready to commit.
// - When RDH receives a response, the BufferedResourceHandler determines
// whether it is a download. If so, it sends a message to the new renderer
// causing it to cancel the request, and the download proceeds. For now, the
// pending RVH remains until the next DidNavigate event for this
// WebContentsImpl. This isn't ideal, but it doesn't affect any functionality.
// - After RDH receives a response and determines that it is safe and not a
// download, the CrossSiteResourceHandler checks whether a process swap is
// needed (either because CrossSiteRequestManager has state for it or because
// a transfer was needed for a redirect).
// - If so, CrossSiteResourceHandler pauses the response to first run the old
// page's unload handler. It does this by asynchronously calling the
// OnCrossSiteResponse method of RenderFrameHostManager on the UI thread,
// which sends a SwapOut message to the current RVH.
// - Once the unload handler is finished, RVHM::SwappedOut checks if a transfer
// to a new process is needed, based on the stored pending_nav_params_. (This
// is independent of whether we started out with a cross-process navigation.)
// - If not, it just tells the ResourceDispatcherHost to resume the response
// to its current RenderViewHost.
// - If so, it cancels the current pending RenderViewHost and sets up a new
// navigation using RequestTransferURL. When the transferred request
// arrives in the ResourceDispatcherHost, we transfer the response and
// resume it.
// - The pending renderer sends a FrameNavigate message that invokes the
// DidNavigate method. This replaces the current RVH with the
// pending RVH.
// - The previous renderer is kept swapped out in RenderFrameHostManager in case
// the user goes back. The process only stays live if another tab is using
// it, but if so, the existing frame relationships will be maintained.
namespace content {
namespace {
const char kDotGoogleDotCom[] = ".google.com";
#if defined(OS_ANDROID)
const char kWebContentsAndroidKey[] = "web_contents_android";
#endif // OS_ANDROID
base::LazyInstance<std::vector<WebContentsImpl::CreatedCallback> >
g_created_callbacks = LAZY_INSTANCE_INITIALIZER;
static int StartDownload(content::RenderViewHost* rvh,
const GURL& url,
bool is_favicon,
uint32_t max_bitmap_size) {
static int g_next_image_download_id = 0;
rvh->Send(new ImageMsg_DownloadImage(rvh->GetRoutingID(),
++g_next_image_download_id,
url,
is_favicon,
max_bitmap_size));
return g_next_image_download_id;
}
void NotifyCacheOnIO(
scoped_refptr<net::URLRequestContextGetter> request_context,
const GURL& url,
const std::string& http_method) {
request_context->GetURLRequestContext()->http_transaction_factory()->
GetCache()->OnExternalCacheHit(url, http_method);
}
// Helper function for retrieving all the sites in a frame tree.
bool CollectSites(BrowserContext* context,
std::set<GURL>* sites,
FrameTreeNode* node) {
sites->insert(SiteInstance::GetSiteForURL(context, node->current_url()));
return true;
}
bool ForEachFrameInternal(
const base::Callback<void(RenderFrameHost*)>& on_frame,
FrameTreeNode* node) {
on_frame.Run(node->current_frame_host());
return true;
}
void SendToAllFramesInternal(IPC::Message* message, RenderFrameHost* rfh) {
IPC::Message* message_copy = new IPC::Message(*message);
message_copy->set_routing_id(rfh->GetRoutingID());
rfh->Send(message_copy);
}
} // namespace
WebContents* WebContents::Create(const WebContents::CreateParams& params) {
return WebContentsImpl::CreateWithOpener(
params, static_cast<WebContentsImpl*>(params.opener));
}
WebContents* WebContents::CreateWithSessionStorage(
const WebContents::CreateParams& params,
const SessionStorageNamespaceMap& session_storage_namespace_map) {
WebContentsImpl* new_contents = new WebContentsImpl(
params.browser_context, NULL);
for (SessionStorageNamespaceMap::const_iterator it =
session_storage_namespace_map.begin();
it != session_storage_namespace_map.end();
++it) {
new_contents->GetController()
.SetSessionStorageNamespace(it->first, it->second.get());
}
new_contents->Init(params);
return new_contents;
}
void WebContentsImpl::AddCreatedCallback(const CreatedCallback& callback) {
g_created_callbacks.Get().push_back(callback);
}
void WebContentsImpl::RemoveCreatedCallback(const CreatedCallback& callback) {
for (size_t i = 0; i < g_created_callbacks.Get().size(); ++i) {
if (g_created_callbacks.Get().at(i).Equals(callback)) {
g_created_callbacks.Get().erase(g_created_callbacks.Get().begin() + i);
return;
}
}
}
WebContents* WebContents::FromRenderViewHost(const RenderViewHost* rvh) {
return rvh->GetDelegate()->GetAsWebContents();
}
WebContents* WebContents::FromRenderFrameHost(RenderFrameHost* rfh) {
RenderFrameHostImpl* rfh_impl = static_cast<RenderFrameHostImpl*>(rfh);
if (!rfh_impl)
return NULL;
return rfh_impl->delegate()->GetAsWebContents();
}
// WebContentsImpl::DestructionObserver ----------------------------------------
class WebContentsImpl::DestructionObserver : public WebContentsObserver {
public:
DestructionObserver(WebContentsImpl* owner, WebContents* watched_contents)
: WebContentsObserver(watched_contents),
owner_(owner) {
}
// WebContentsObserver:
virtual void WebContentsDestroyed(WebContents* web_contents) OVERRIDE {
owner_->OnWebContentsDestroyed(static_cast<WebContentsImpl*>(web_contents));
}
private:
WebContentsImpl* owner_;
DISALLOW_COPY_AND_ASSIGN(DestructionObserver);
};
// WebContentsImpl -------------------------------------------------------------
WebContentsImpl::WebContentsImpl(
BrowserContext* browser_context,
WebContentsImpl* opener)
: delegate_(NULL),
controller_(this, browser_context),
render_view_host_delegate_view_(NULL),
opener_(opener),
#if defined(OS_WIN)
accessible_parent_(NULL),
#endif
frame_tree_(new NavigatorImpl(&controller_, this),
this, this, this, this),
is_loading_(false),
crashed_status_(base::TERMINATION_STATUS_STILL_RUNNING),
crashed_error_code_(0),
waiting_for_response_(false),
load_state_(net::LOAD_STATE_IDLE, base::string16()),
upload_size_(0),
upload_position_(0),
displayed_insecure_content_(false),
capturer_count_(0),
should_normally_be_visible_(true),
is_being_destroyed_(false),
notify_disconnection_(false),
dialog_manager_(NULL),
is_showing_before_unload_dialog_(false),
last_active_time_(base::TimeTicks::Now()),
closed_by_user_gesture_(false),
minimum_zoom_percent_(static_cast<int>(kMinimumZoomFactor * 100)),
maximum_zoom_percent_(static_cast<int>(kMaximumZoomFactor * 100)),
temporary_zoom_settings_(false),
color_chooser_identifier_(0),
render_view_message_source_(NULL),
fullscreen_widget_routing_id_(MSG_ROUTING_NONE),
is_subframe_(false) {
for (size_t i = 0; i < g_created_callbacks.Get().size(); i++)
g_created_callbacks.Get().at(i).Run(this);
frame_tree_.SetFrameRemoveListener(
base::Bind(&WebContentsImpl::OnFrameRemoved,
base::Unretained(this)));
}
WebContentsImpl::~WebContentsImpl() {
is_being_destroyed_ = true;
ClearAllPowerSaveBlockers();
for (std::set<RenderWidgetHostImpl*>::iterator iter =
created_widgets_.begin(); iter != created_widgets_.end(); ++iter) {
(*iter)->DetachDelegate();
}
created_widgets_.clear();
// Clear out any JavaScript state.
if (dialog_manager_)
dialog_manager_->WebContentsDestroyed(this);
if (color_chooser_)
color_chooser_->End();
NotifyDisconnected();
// Notify any observer that have a reference on this WebContents.
NotificationService::current()->Notify(
NOTIFICATION_WEB_CONTENTS_DESTROYED,
Source<WebContents>(this),
NotificationService::NoDetails());
RenderViewHost* pending_rvh = GetRenderManager()->pending_render_view_host();
if (pending_rvh) {
FOR_EACH_OBSERVER(WebContentsObserver,
observers_,
RenderViewDeleted(pending_rvh));
}
FOR_EACH_OBSERVER(WebContentsObserver,
observers_,
RenderViewDeleted(GetRenderManager()->current_host()));
FOR_EACH_OBSERVER(WebContentsObserver,
observers_,
WebContentsImplDestroyed());
SetDelegate(NULL);
STLDeleteContainerPairSecondPointers(destruction_observers_.begin(),
destruction_observers_.end());
}
WebContentsImpl* WebContentsImpl::CreateWithOpener(
const WebContents::CreateParams& params,
WebContentsImpl* opener) {
TRACE_EVENT0("browser", "WebContentsImpl::CreateWithOpener");
WebContentsImpl* new_contents = new WebContentsImpl(
params.browser_context, opener);
new_contents->Init(params);
return new_contents;
}
// static
BrowserPluginGuest* WebContentsImpl::CreateGuest(
BrowserContext* browser_context,
SiteInstance* site_instance,
int guest_instance_id,
scoped_ptr<base::DictionaryValue> extra_params) {
WebContentsImpl* new_contents = new WebContentsImpl(browser_context, NULL);
// This makes |new_contents| act as a guest.
// For more info, see comment above class BrowserPluginGuest.
BrowserPluginGuest::Create(
guest_instance_id, site_instance, new_contents, extra_params.Pass());
WebContents::CreateParams create_params(browser_context, site_instance);
new_contents->Init(create_params);
// We are instantiating a WebContents for browser plugin. Set its subframe bit
// to true.
new_contents->is_subframe_ = true;
return new_contents->browser_plugin_guest_.get();
}
RenderFrameHostManager* WebContentsImpl::GetRenderManagerForTesting() {
return GetRenderManager();
}
bool WebContentsImpl::OnMessageReceived(RenderViewHost* render_view_host,
const IPC::Message& message) {
return OnMessageReceived(render_view_host, NULL, message);
}
bool WebContentsImpl::OnMessageReceived(RenderViewHost* render_view_host,
RenderFrameHost* render_frame_host,
const IPC::Message& message) {
DCHECK(render_view_host || render_frame_host);
if (GetWebUI() &&
static_cast<WebUIImpl*>(GetWebUI())->OnMessageReceived(message)) {
return true;
}
ObserverListBase<WebContentsObserver>::Iterator it(observers_);
WebContentsObserver* observer;
while ((observer = it.GetNext()) != NULL)
if (observer->OnMessageReceived(message))
return true;
// Message handlers should be aware of which
// RenderViewHost/RenderFrameHost sent the message, which is temporarily
// stored in render_(view|frame)_message_source_.
if (render_frame_host)
render_frame_message_source_ = render_frame_host;
else
render_view_message_source_ = render_view_host;
bool handled = true;
bool message_is_ok = true;
IPC_BEGIN_MESSAGE_MAP_EX(WebContentsImpl, message, message_is_ok)
IPC_MESSAGE_HANDLER(FrameHostMsg_PepperPluginHung, OnPepperPluginHung)
IPC_MESSAGE_HANDLER(FrameHostMsg_PluginCrashed, OnPluginCrashed)
IPC_MESSAGE_HANDLER(FrameHostMsg_DomOperationResponse,
OnDomOperationResponse)
IPC_MESSAGE_HANDLER(ViewHostMsg_DidLoadResourceFromMemoryCache,
OnDidLoadResourceFromMemoryCache)
IPC_MESSAGE_HANDLER(ViewHostMsg_DidDisplayInsecureContent,
OnDidDisplayInsecureContent)
IPC_MESSAGE_HANDLER(ViewHostMsg_DidRunInsecureContent,
OnDidRunInsecureContent)
IPC_MESSAGE_HANDLER(FrameHostMsg_DidFinishDocumentLoad,
OnDocumentLoadedInFrame)
IPC_MESSAGE_HANDLER(ViewHostMsg_DidFinishLoad, OnDidFinishLoad)
IPC_MESSAGE_HANDLER(ViewHostMsg_GoToEntryAtOffset, OnGoToEntryAtOffset)
IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateZoomLimits, OnUpdateZoomLimits)
IPC_MESSAGE_HANDLER(ViewHostMsg_EnumerateDirectory, OnEnumerateDirectory)
IPC_MESSAGE_HANDLER(ViewHostMsg_JSOutOfMemory, OnJSOutOfMemory)
IPC_MESSAGE_HANDLER(ViewHostMsg_RegisterProtocolHandler,
OnRegisterProtocolHandler)
IPC_MESSAGE_HANDLER(ViewHostMsg_Find_Reply, OnFindReply)
IPC_MESSAGE_HANDLER(ViewHostMsg_AppCacheAccessed, OnAppCacheAccessed)
IPC_MESSAGE_HANDLER(ViewHostMsg_OpenColorChooser, OnOpenColorChooser)
IPC_MESSAGE_HANDLER(ViewHostMsg_EndColorChooser, OnEndColorChooser)
IPC_MESSAGE_HANDLER(ViewHostMsg_SetSelectedColorInColorChooser,
OnSetSelectedColorInColorChooser)
IPC_MESSAGE_HANDLER(ViewHostMsg_WebUISend, OnWebUISend)
IPC_MESSAGE_HANDLER(ViewHostMsg_RequestPpapiBrokerPermission,
OnRequestPpapiBrokerPermission)
IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginHostMsg_AllocateInstanceID,
OnBrowserPluginMessage(message))
IPC_MESSAGE_HANDLER_GENERIC(BrowserPluginHostMsg_Attach,
OnBrowserPluginMessage(message))
IPC_MESSAGE_HANDLER(ImageHostMsg_DidDownloadImage, OnDidDownloadImage)
IPC_MESSAGE_HANDLER(ViewHostMsg_UpdateFaviconURL, OnUpdateFaviconURL)
#if defined(OS_ANDROID)
IPC_MESSAGE_HANDLER(ViewHostMsg_FindMatchRects_Reply,
OnFindMatchRectsReply)
IPC_MESSAGE_HANDLER(ViewHostMsg_OpenDateTimeDialog,
OnOpenDateTimeDialog)
IPC_MESSAGE_HANDLER_DELAY_REPLY(JavaBridgeHostMsg_GetChannelHandle,
OnJavaBridgeGetChannelHandle)
#endif
IPC_MESSAGE_HANDLER(ViewHostMsg_MediaPlayingNotification,
OnMediaPlayingNotification)
IPC_MESSAGE_HANDLER(ViewHostMsg_MediaPausedNotification,
OnMediaPausedNotification)
IPC_MESSAGE_HANDLER(ViewHostMsg_DidFirstVisuallyNonEmptyPaint,
OnFirstVisuallyNonEmptyPaint)
IPC_MESSAGE_HANDLER(ViewHostMsg_ShowValidationMessage,
OnShowValidationMessage)
IPC_MESSAGE_HANDLER(ViewHostMsg_HideValidationMessage,
OnHideValidationMessage)
IPC_MESSAGE_HANDLER(ViewHostMsg_MoveValidationMessage,
OnMoveValidationMessage)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP_EX()
render_view_message_source_ = NULL;
render_frame_message_source_ = NULL;
if (!message_is_ok) {
RecordAction(base::UserMetricsAction("BadMessageTerminate_RVD"));
GetRenderProcessHost()->ReceivedBadMessage();
}
return handled;
}
void WebContentsImpl::RunFileChooser(
RenderViewHost* render_view_host,
const FileChooserParams& params) {
if (delegate_)
delegate_->RunFileChooser(this, params);
}
NavigationControllerImpl& WebContentsImpl::GetController() {
return controller_;
}
const NavigationControllerImpl& WebContentsImpl::GetController() const {
return controller_;
}
BrowserContext* WebContentsImpl::GetBrowserContext() const {
return controller_.GetBrowserContext();
}
const GURL& WebContentsImpl::GetURL() const {
// We may not have a navigation entry yet.
NavigationEntry* entry = controller_.GetVisibleEntry();
return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
}
const GURL& WebContentsImpl::GetVisibleURL() const {
// We may not have a navigation entry yet.
NavigationEntry* entry = controller_.GetVisibleEntry();
return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
}
const GURL& WebContentsImpl::GetLastCommittedURL() const {
// We may not have a navigation entry yet.
NavigationEntry* entry = controller_.GetLastCommittedEntry();
return entry ? entry->GetVirtualURL() : GURL::EmptyGURL();
}
WebContentsDelegate* WebContentsImpl::GetDelegate() {
return delegate_;
}
void WebContentsImpl::SetDelegate(WebContentsDelegate* delegate) {
// TODO(cbentzel): remove this debugging code?
if (delegate == delegate_)
return;
if (delegate_)
delegate_->Detach(this);
delegate_ = delegate;
if (delegate_) {
delegate_->Attach(this);
// Ensure the visible RVH reflects the new delegate's preferences.
if (view_)
view_->SetOverscrollControllerEnabled(delegate->CanOverscrollContent());
}
}
RenderProcessHost* WebContentsImpl::GetRenderProcessHost() const {
RenderViewHostImpl* host = GetRenderManager()->current_host();
return host ? host->GetProcess() : NULL;
}
RenderFrameHost* WebContentsImpl::GetMainFrame() {
return frame_tree_.root()->current_frame_host();
}
void WebContentsImpl::ForEachFrame(
const base::Callback<void(RenderFrameHost*)>& on_frame) {
frame_tree_.ForEach(base::Bind(&ForEachFrameInternal, on_frame));
}
void WebContentsImpl::SendToAllFrames(IPC::Message* message) {
ForEachFrame(base::Bind(&SendToAllFramesInternal, message));
delete message;
}
RenderViewHost* WebContentsImpl::GetRenderViewHost() const {
return GetRenderManager()->current_host();
}
void WebContentsImpl::GetRenderViewHostAtPosition(
int x,
int y,
const base::Callback<void(RenderViewHost*, int, int)>& callback) {
BrowserPluginEmbedder* embedder = GetBrowserPluginEmbedder();
if (embedder)
embedder->GetRenderViewHostAtPosition(x, y, callback);
else
callback.Run(GetRenderViewHost(), x, y);
}
WebContents* WebContentsImpl::GetEmbedderWebContents() const {
BrowserPluginGuest* guest = GetBrowserPluginGuest();
if (guest)
return guest->embedder_web_contents();
return NULL;
}
int WebContentsImpl::GetEmbeddedInstanceID() const {
BrowserPluginGuest* guest = GetBrowserPluginGuest();
if (guest)
return guest->instance_id();
return 0;
}
int WebContentsImpl::GetRoutingID() const {
if (!GetRenderViewHost())
return MSG_ROUTING_NONE;
return GetRenderViewHost()->GetRoutingID();
}
int WebContentsImpl::GetFullscreenWidgetRoutingID() const {
return fullscreen_widget_routing_id_;
}
RenderWidgetHostView* WebContentsImpl::GetRenderWidgetHostView() const {
return GetRenderManager()->GetRenderWidgetHostView();
}
RenderWidgetHostViewPort* WebContentsImpl::GetRenderWidgetHostViewPort() const {
BrowserPluginGuest* guest = GetBrowserPluginGuest();
if (guest && guest->embedder_web_contents()) {
return guest->embedder_web_contents()->GetRenderWidgetHostViewPort();
}
return RenderWidgetHostViewPort::FromRWHV(GetRenderWidgetHostView());
}
RenderWidgetHostView* WebContentsImpl::GetFullscreenRenderWidgetHostView()
const {
RenderWidgetHost* const widget_host =
RenderWidgetHostImpl::FromID(GetRenderProcessHost()->GetID(),
GetFullscreenWidgetRoutingID());
return widget_host ? widget_host->GetView() : NULL;
}
WebContentsView* WebContentsImpl::GetView() const {
return view_.get();
}
WebUI* WebContentsImpl::CreateWebUI(const GURL& url) {
WebUIImpl* web_ui = new WebUIImpl(this);
WebUIController* controller = WebUIControllerFactoryRegistry::GetInstance()->
CreateWebUIControllerForURL(web_ui, url);
if (controller) {
web_ui->AddMessageHandler(new GenericHandler());
web_ui->SetController(controller);
return web_ui;
}
delete web_ui;
return NULL;
}
WebUI* WebContentsImpl::GetWebUI() const {
return GetRenderManager()->web_ui() ? GetRenderManager()->web_ui()
: GetRenderManager()->pending_web_ui();
}
WebUI* WebContentsImpl::GetCommittedWebUI() const {
return GetRenderManager()->web_ui();
}
void WebContentsImpl::SetUserAgentOverride(const std::string& override) {
if (GetUserAgentOverride() == override)
return;
renderer_preferences_.user_agent_override = override;
// Send the new override string to the renderer.
RenderViewHost* host = GetRenderViewHost();
if (host)
host->SyncRendererPrefs();
// Reload the page if a load is currently in progress to avoid having
// different parts of the page loaded using different user agents.
NavigationEntry* entry = controller_.GetVisibleEntry();
if (is_loading_ && entry != NULL && entry->GetIsOverridingUserAgent())
controller_.ReloadIgnoringCache(true);
FOR_EACH_OBSERVER(WebContentsObserver, observers_,
UserAgentOverrideSet(override));
}
const std::string& WebContentsImpl::GetUserAgentOverride() const {
return renderer_preferences_.user_agent_override;
}
#if defined(OS_WIN)
void WebContentsImpl::SetParentNativeViewAccessible(
gfx::NativeViewAccessible accessible_parent) {
accessible_parent_ = accessible_parent;
if (GetRenderViewHost())
GetRenderViewHostImpl()->SetParentNativeViewAccessible(accessible_parent);
}
#endif
const base::string16& WebContentsImpl::GetTitle() const {
// Transient entries take precedence. They are used for interstitial pages
// that are shown on top of existing pages.
NavigationEntry* entry = controller_.GetTransientEntry();
std::string accept_languages =
GetContentClient()->browser()->GetAcceptLangs(
GetBrowserContext());
if (entry) {
return entry->GetTitleForDisplay(accept_languages);
}
WebUI* our_web_ui = GetRenderManager()->pending_web_ui() ?
GetRenderManager()->pending_web_ui() : GetRenderManager()->web_ui();
if (our_web_ui) {
// Don't override the title in view source mode.
entry = controller_.GetVisibleEntry();
if (!(entry && entry->IsViewSourceMode())) {
// Give the Web UI the chance to override our title.
const base::string16& title = our_web_ui->GetOverriddenTitle();
if (!title.empty())
return title;
}
}
// We use the title for the last committed entry rather than a pending
// navigation entry. For example, when the user types in a URL, we want to
// keep the old page's title until the new load has committed and we get a new
// title.
entry = controller_.GetLastCommittedEntry();
// We make an exception for initial navigations.
if (controller_.IsInitialNavigation()) {
// We only want to use the title from the visible entry in one of two cases:
// 1. There's already a committed entry for an initial navigation, in which
// case we are doing a history navigation in a new tab (e.g., Ctrl+Back).
// 2. The pending entry has been explicitly assigned a title to display.
//
// If there's no last committed entry and no assigned title, we should fall
// back to |page_title_when_no_navigation_entry_| rather than showing the
// URL.
if (entry ||
(controller_.GetVisibleEntry() &&
!controller_.GetVisibleEntry()->GetTitle().empty())) {
entry = controller_.GetVisibleEntry();
}
}
if (entry) {
return entry->GetTitleForDisplay(accept_languages);
}
// |page_title_when_no_navigation_entry_| is finally used
// if no title cannot be retrieved.
return page_title_when_no_navigation_entry_;
}
int32 WebContentsImpl::GetMaxPageID() {
return GetMaxPageIDForSiteInstance(GetSiteInstance());
}
int32 WebContentsImpl::GetMaxPageIDForSiteInstance(
SiteInstance* site_instance) {
if (max_page_ids_.find(site_instance->GetId()) == max_page_ids_.end())
max_page_ids_[site_instance->GetId()] = -1;
return max_page_ids_[site_instance->GetId()];
}
void WebContentsImpl::UpdateMaxPageID(int32 page_id) {
UpdateMaxPageIDForSiteInstance(GetSiteInstance(), page_id);
}
void WebContentsImpl::UpdateMaxPageIDForSiteInstance(
SiteInstance* site_instance, int32 page_id) {
if (GetMaxPageIDForSiteInstance(site_instance) < page_id)
max_page_ids_[site_instance->GetId()] = page_id;
}
void WebContentsImpl::CopyMaxPageIDsFrom(WebContents* web_contents) {
WebContentsImpl* contents = static_cast<WebContentsImpl*>(web_contents);
max_page_ids_ = contents->max_page_ids_;
}
SiteInstance* WebContentsImpl::GetSiteInstance() const {
return GetRenderManager()->current_host()->GetSiteInstance();
}
SiteInstance* WebContentsImpl::GetPendingSiteInstance() const {
RenderViewHost* dest_rvh = GetRenderManager()->pending_render_view_host() ?
GetRenderManager()->pending_render_view_host() :
GetRenderManager()->current_host();
return dest_rvh->GetSiteInstance();
}
bool WebContentsImpl::IsLoading() const {
return is_loading_;
}
bool WebContentsImpl::IsWaitingForResponse() const {
return waiting_for_response_;
}
const net::LoadStateWithParam& WebContentsImpl::GetLoadState() const {
return load_state_;
}
const base::string16& WebContentsImpl::GetLoadStateHost() const {
return load_state_host_;
}
uint64 WebContentsImpl::GetUploadSize() const {
return upload_size_;
}
uint64 WebContentsImpl::GetUploadPosition() const {
return upload_position_;
}
std::set<GURL> WebContentsImpl::GetSitesInTab() const {
std::set<GURL> sites;
frame_tree_.ForEach(base::Bind(&CollectSites,
base::Unretained(GetBrowserContext()),
base::Unretained(&sites)));
return sites;
}
const std::string& WebContentsImpl::GetEncoding() const {
return encoding_;
}
bool WebContentsImpl::DisplayedInsecureContent() const {
return displayed_insecure_content_;
}
void WebContentsImpl::IncrementCapturerCount(const gfx::Size& capture_size) {
DCHECK(!is_being_destroyed_);
++capturer_count_;
DVLOG(1) << "There are now " << capturer_count_
<< " capturing(s) of WebContentsImpl@" << this;
// Note: This provides a hint to upstream code to size the views optimally
// for quality (e.g., to avoid scaling).
if (!capture_size.IsEmpty() && preferred_size_for_capture_.IsEmpty()) {
preferred_size_for_capture_ = capture_size;
OnPreferredSizeChanged(preferred_size_);
}
}
void WebContentsImpl::DecrementCapturerCount() {
--capturer_count_;
DVLOG(1) << "There are now " << capturer_count_
<< " capturing(s) of WebContentsImpl@" << this;
DCHECK_LE(0, capturer_count_);
if (is_being_destroyed_)
return;
if (capturer_count_ == 0) {
const gfx::Size old_size = preferred_size_for_capture_;
preferred_size_for_capture_ = gfx::Size();
OnPreferredSizeChanged(old_size);
}
if (IsHidden()) {
DVLOG(1) << "Executing delayed WasHidden().";
WasHidden();
}
}
int WebContentsImpl::GetCapturerCount() const {
return capturer_count_;
}
bool WebContentsImpl::IsCrashed() const {
return (crashed_status_ == base::TERMINATION_STATUS_PROCESS_CRASHED ||
crashed_status_ == base::TERMINATION_STATUS_ABNORMAL_TERMINATION ||
crashed_status_ == base::TERMINATION_STATUS_PROCESS_WAS_KILLED);
}
void WebContentsImpl::SetIsCrashed(base::TerminationStatus status,
int error_code) {
if (status == crashed_status_)
return;
crashed_status_ = status;
crashed_error_code_ = error_code;
NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
}
base::TerminationStatus WebContentsImpl::GetCrashedStatus() const {
return crashed_status_;
}
bool WebContentsImpl::IsBeingDestroyed() const {
return is_being_destroyed_;
}
void WebContentsImpl::NotifyNavigationStateChanged(unsigned changed_flags) {
if (delegate_)
delegate_->NavigationStateChanged(this, changed_flags);
}
base::TimeTicks WebContentsImpl::GetLastActiveTime() const {
return last_active_time_;
}
void WebContentsImpl::WasShown() {
controller_.SetActive(true);
RenderWidgetHostViewPort* rwhv =
RenderWidgetHostViewPort::FromRWHV(GetRenderWidgetHostView());
if (rwhv) {
rwhv->Show();
#if defined(OS_MACOSX)
rwhv->SetActive(true);
#endif
}
last_active_time_ = base::TimeTicks::Now();
// The resize rect might have changed while this was inactive -- send the new
// one to make sure it's up to date.
RenderViewHostImpl* rvh =
static_cast<RenderViewHostImpl*>(GetRenderViewHost());
if (rvh) {
rvh->ResizeRectChanged(GetRootWindowResizerRect());
}
FOR_EACH_OBSERVER(WebContentsObserver, observers_, WasShown());
should_normally_be_visible_ = true;
}
void WebContentsImpl::WasHidden() {
// If there are entities capturing screenshots or video (e.g., mirroring),
// don't activate the "disable rendering" optimization.
if (capturer_count_ == 0) {
// |GetRenderViewHost()| can be NULL if the user middle clicks a link to
// open a tab in the background, then closes the tab before selecting it.
// This is because closing the tab calls WebContentsImpl::Destroy(), which
// removes the |GetRenderViewHost()|; then when we actually destroy the
// window, OnWindowPosChanged() notices and calls WasHidden() (which
// calls us).
RenderWidgetHostViewPort* rwhv =
RenderWidgetHostViewPort::FromRWHV(GetRenderWidgetHostView());
if (rwhv)
rwhv->Hide();
}
FOR_EACH_OBSERVER(WebContentsObserver, observers_, WasHidden());
should_normally_be_visible_ = false;
}
bool WebContentsImpl::NeedToFireBeforeUnload() {
// TODO(creis): Should we fire even for interstitial pages?
return WillNotifyDisconnection() &&
!ShowingInterstitialPage() &&
!static_cast<RenderViewHostImpl*>(
GetRenderViewHost())->SuddenTerminationAllowed();
}
void WebContentsImpl::Stop() {
GetRenderManager()->Stop();
FOR_EACH_OBSERVER(WebContentsObserver, observers_, NavigationStopped());
}
WebContents* WebContentsImpl::Clone() {
// We use our current SiteInstance since the cloned entry will use it anyway.
// We pass our own opener so that the cloned page can access it if it was
// before.
CreateParams create_params(GetBrowserContext(), GetSiteInstance());
create_params.initial_size = view_->GetContainerSize();
WebContentsImpl* tc = CreateWithOpener(create_params, opener_);
tc->GetController().CopyStateFrom(controller_);
FOR_EACH_OBSERVER(WebContentsObserver,
observers_,
DidCloneToNewWebContents(this, tc));
return tc;
}
void WebContentsImpl::Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) {
switch (type) {
case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
RenderWidgetHost* host = Source<RenderWidgetHost>(source).ptr();
for (PendingWidgetViews::iterator i = pending_widget_views_.begin();
i != pending_widget_views_.end(); ++i) {
if (host->GetView() == i->second) {
pending_widget_views_.erase(i);
break;
}
}
break;
}
default: