Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / renderer / resources / guest_view / guest_view_iframe.js
blobdba1b9136f1b8535e8087bc392d70e03d9f07d5e
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);
14   if (!view)
15     return null;
17   var internalIframeElement = privates(view).internalElement;
18   if (internalIframeElement)
19     return internalIframeElement.contentWindow;
21   return null;
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);
30     return;
31   }
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);
39     if (!contentWindow) {
40       this.state = GuestViewImpl.GuestState.GUEST_STATE_CREATED;
41       this.internalInstanceId = 0;
42     } else {
43       // Only update the contentWindow if attaching is successful.
44       this.contentWindow = contentWindow;
45     }
47     this.handleCallback(callback);
48   };
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) {
65       return;
66     }
68     this.internalInstanceId = 0;
69     this.state = GuestViewImpl.GuestState.GUEST_STATE_CREATED;
70   }, viewInstanceId));
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);
78     return;
79   }
81   // Callback wrapper function to store the guestInstanceId from the
82   // createGuest() callback, handle potential creation failure, and advance the
83   // queue.
84   var callbackWrapper = function(callback, guestInfo) {
85     this.id = guestInfo.id;
87     // Check if creation failed.
88     if (this.id === 0) {
89       this.state = GuestViewImpl.GuestState.GUEST_STATE_START;
90       this.contentWindow = null;
91     }
93     ResizeEvent.addListener(this.callOnResize, {instanceId: this.id});
94     this.handleCallback(callback);
95   };
97   this.sendCreateRequest(createParams, callbackWrapper.bind(this, callback));
99   this.state = GuestViewImpl.GuestState.GUEST_STATE_CREATED;