1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // --site-per-process overrides for guest_view.js.
7 var GuestView = require('guestView').GuestView;
8 var GuestViewImpl = require('guestView').GuestViewImpl;
9 var GuestViewInternalNatives = requireNative('guest_view_internal');
10 var ResizeEvent = require('guestView').ResizeEvent;
12 var getIframeContentWindow = function(viewInstanceId) {
13 var view = GuestViewInternalNatives.GetViewFromID(viewInstanceId);
17 var internalIframeElement = privates(view).internalElement;
18 if (internalIframeElement)
19 return internalIframeElement.contentWindow;
24 // Internal implementation of attach().
25 GuestViewImpl.prototype.attachImpl$ = function(
26 internalInstanceId, viewInstanceId, attachParams, callback) {
27 // Check the current state.
28 if (!this.checkState('attach')) {
29 this.handleCallback(callback);
33 // Callback wrapper function to store the contentWindow from the attachGuest()
34 // callback, handle potential attaching failure, register an automatic detach,
35 // and advance the queue.
36 var callbackWrapper = function(callback, contentWindow) {
37 // Check if attaching failed.
38 contentWindow = getIframeContentWindow(viewInstanceId);
40 this.state = GuestViewImpl.GuestState.GUEST_STATE_CREATED;
41 this.internalInstanceId = 0;
43 // Only update the contentWindow if attaching is successful.
44 this.contentWindow = contentWindow;
47 this.handleCallback(callback);
50 attachParams['instanceId'] = viewInstanceId;
51 var contentWindow = getIframeContentWindow(viewInstanceId);
52 // |contentWindow| is used to retrieve the RenderFrame in cpp.
53 GuestViewInternalNatives.AttachIframeGuest(
54 internalInstanceId, this.id, attachParams, contentWindow,
55 callbackWrapper.bind(this, callback));
57 this.internalInstanceId = internalInstanceId;
58 this.state = GuestViewImpl.GuestState.GUEST_STATE_ATTACHED;
60 // Detach automatically when the container is destroyed.
61 GuestViewInternalNatives.RegisterDestructionCallback(
62 internalInstanceId, this.weakWrapper(function() {
63 if (this.state != GuestViewImpl.GuestState.GUEST_STATE_ATTACHED ||
64 this.internalInstanceId != internalInstanceId) {
68 this.internalInstanceId = 0;
69 this.state = GuestViewImpl.GuestState.GUEST_STATE_CREATED;
73 // Internal implementation of create().
74 GuestViewImpl.prototype.createImpl$ = function(createParams, callback) {
75 // Check the current state.
76 if (!this.checkState('create')) {
77 this.handleCallback(callback);
81 // Callback wrapper function to store the guestInstanceId from the
82 // createGuest() callback, handle potential creation failure, and advance the
84 var callbackWrapper = function(callback, guestInfo) {
85 this.id = guestInfo.id;
87 // Check if creation failed.
89 this.state = GuestViewImpl.GuestState.GUEST_STATE_START;
90 this.contentWindow = null;
93 ResizeEvent.addListener(this.callOnResize, {instanceId: this.id});
94 this.handleCallback(callback);
97 this.sendCreateRequest(createParams, callbackWrapper.bind(this, callback));
99 this.state = GuestViewImpl.GuestState.GUEST_STATE_CREATED;