Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: chrome/browser/resources/chromeos/arc_support/background.js

Issue 2380683008: Fix resource leak on extension window closing. (Closed)
Patch Set: Address comments. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/chromeos/arc/arc_support_host.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 /** 5 /**
6 * UI Pages. Note the order must be in sync with the ArcAuthService::UIPage 6 * UI Pages. Note the order must be in sync with the ArcAuthService::UIPage
7 * enum. 7 * enum.
8 * @type {Array<string>} 8 * @type {Array<string>}
9 */ 9 */
10 var UI_PAGES = ['none', 10 var UI_PAGES = ['none',
(...skipping 23 matching lines...) Expand all
34 */ 34 */
35 var termsView = null; 35 var termsView = null;
36 36
37 /** 37 /**
38 * Used for bidirectional communication with native code. 38 * Used for bidirectional communication with native code.
39 * @type {chrome.runtime.Port} 39 * @type {chrome.runtime.Port}
40 */ 40 */
41 var port = null; 41 var port = null;
42 42
43 /** 43 /**
44 * Indicates that window was closed internally and it is not required to send
45 * closure notification.
46 * @type {boolean}
47 */
48 var windowClosedInternally = false;
49
50 /**
51 * Timer for terms reload.
52 * @type {Object}
53 */
54 var termsReloadTimeout = null;
55
56 /**
57 * Stores current device id. 44 * Stores current device id.
58 * @type {string} 45 * @type {string}
59 */ 46 */
60 var currentDeviceId = null; 47 var currentDeviceId = null;
61 48
62 /** 49 /**
63 * Indicates that terms were accepted by user. 50 * Indicates that terms were accepted by user.
64 * @type {boolean} 51 * @type {boolean}
65 */ 52 */
66 var termsAccepted = false; 53 var termsAccepted = false;
(...skipping 17 matching lines...) Expand all
84 var INNER_WIDTH = 960; 71 var INNER_WIDTH = 960;
85 72
86 /** 73 /**
87 * Host window inner default height. 74 * Host window inner default height.
88 * @const {number} 75 * @const {number}
89 */ 76 */
90 var INNER_HEIGHT = 688; 77 var INNER_HEIGHT = 688;
91 78
92 79
93 /** 80 /**
94 * Closes current window in response to request from native code. This does not
95 * issue 'cancelAuthCode' message to native code.
96 */
97 function closeWindowInternally() {
98 windowClosedInternally = true;
99 appWindow.close();
100 appWindow = null;
101 }
102
103 /**
104 * Sends a native message to ArcSupportHost. 81 * Sends a native message to ArcSupportHost.
105 * @param {string} code The action code in message. 82 * @param {string} code The action code in message.
106 * @param {Object=} opt_Props Extra properties for the message. 83 * @param {Object=} opt_Props Extra properties for the message.
107 */ 84 */
108 function sendNativeMessage(code, opt_Props) { 85 function sendNativeMessage(code, opt_Props) {
109 var message = Object.assign({'action': code}, opt_Props); 86 var message = Object.assign({'action': code}, opt_Props);
110 port.postMessage(message); 87 port.postMessage(message);
111 } 88 }
112 89
113 /** 90 /**
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 } 271 }
295 272
296 if (message.action == 'initialize') { 273 if (message.action == 'initialize') {
297 initialize(message.data, message.deviceId); 274 initialize(message.data, message.deviceId);
298 } else if (message.action == 'setMetricsMode') { 275 } else if (message.action == 'setMetricsMode') {
299 setMetricsMode(message.text, message.canEnable, message.on); 276 setMetricsMode(message.text, message.canEnable, message.on);
300 } else if (message.action == 'setBackupAndRestoreMode') { 277 } else if (message.action == 'setBackupAndRestoreMode') {
301 setBackupRestoreMode(message.enabled, message.managed); 278 setBackupRestoreMode(message.enabled, message.managed);
302 } else if (message.action == 'setLocationServiceMode') { 279 } else if (message.action == 'setLocationServiceMode') {
303 setLocationServiceMode(message.enabled, message.managed); 280 setLocationServiceMode(message.enabled, message.managed);
304 } else if (message.action == 'closeUI') { 281 } else if (message.action == 'closeWindow') {
305 closeWindowInternally(); 282 if (appWindow) {
283 appWindow.close();
284 }
306 } else if (message.action == 'showPage') { 285 } else if (message.action == 'showPage') {
307 showPageWithStatus(message.page, message.status); 286 showPageWithStatus(message.page, message.status);
308 } else if (message.action == 'setWindowBounds') { 287 } else if (message.action == 'setWindowBounds') {
309 setWindowBounds(); 288 setWindowBounds();
310 } 289 }
311 } 290 }
312 291
313 /** 292 /**
314 * Connects to ArcSupportHost. 293 * Connects to ArcSupportHost.
315 */ 294 */
316 function connectPort() { 295 function connectPort() {
317 var hostName = 'com.google.arc_support'; 296 var hostName = 'com.google.arc_support';
318 port = chrome.runtime.connectNative(hostName); 297 port = chrome.runtime.connectNative(hostName);
319 port.onMessage.addListener(onNativeMessage); 298 port.onMessage.addListener(onNativeMessage);
320 } 299 }
321 300
322 /** 301 /**
323 * Shows requested page and hide others. Show appWindow if it was hidden before 302 * Shows requested page and hide others. Show appWindow if it was hidden before.
324 * for non 'none' pages. For 'none' page this closes host window. 303 * 'none' hides all views.
325 * @param {string} pageDivId id of divider of the page to show. 304 * @param {string} pageDivId id of divider of the page to show.
326 */ 305 */
327 function showPage(pageDivId) { 306 function showPage(pageDivId) {
328 if (!appWindow) { 307 if (!appWindow) {
329 return; 308 return;
330 } 309 }
331 310
332 if (pageDivId == 'none') {
333 closeWindowInternally();
334 return;
335 }
336
337 var doc = appWindow.contentWindow.document; 311 var doc = appWindow.contentWindow.document;
338 var pages = doc.getElementsByClassName('section'); 312 var pages = doc.getElementsByClassName('section');
339 var sendFeedbackElement = doc.getElementById('button-send-feedback'); 313 var sendFeedbackElement = doc.getElementById('button-send-feedback');
340 if (pageDivId == 'error-with-feedback') { 314 if (pageDivId == 'error-with-feedback') {
341 // Only show feedback button if the pageDivId is 'error-with-feedback'. 315 // Only show feedback button if the pageDivId is 'error-with-feedback'.
342 sendFeedbackElement.hidden = false; 316 sendFeedbackElement.hidden = false;
343 pageDivId = 'error'; 317 pageDivId = 'error';
344 } else { 318 } else {
345 sendFeedbackElement.hidden = true; 319 sendFeedbackElement.hidden = true;
346 } 320 }
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 var enableLocationService = doc.getElementById('enable-location-service'); 544 var enableLocationService = doc.getElementById('enable-location-service');
571 sendNativeMessage('setLocationService', { 545 sendNativeMessage('setLocationService', {
572 'enabled': enableLocationService.checked 546 'enabled': enableLocationService.checked
573 }); 547 });
574 548
575 sendNativeMessage('startLso'); 549 sendNativeMessage('startLso');
576 }; 550 };
577 551
578 var onCancel = function() { 552 var onCancel = function() {
579 if (appWindow) { 553 if (appWindow) {
580 windowClosedInternally = false;
581 appWindow.close(); 554 appWindow.close();
582 appWindow = null;
583 } 555 }
584 }; 556 };
585 557
586 var onRetry = function() { 558 var onRetry = function() {
587 if (termsAccepted) { 559 if (termsAccepted) {
588 sendNativeMessage('startLso'); 560 sendNativeMessage('startLso');
589 } else { 561 } else {
590 loadInitialTerms(); 562 loadInitialTerms();
591 } 563 }
592 }; 564 };
593 565
594 var onSendFeedback = function() { 566 var onSendFeedback = function() {
595 sendNativeMessage('sendFeedback'); 567 sendNativeMessage('sendFeedback');
596 }; 568 };
597 569
598 doc.getElementById('button-agree').addEventListener('click', onAgree); 570 doc.getElementById('button-agree').addEventListener('click', onAgree);
599 doc.getElementById('button-cancel').addEventListener('click', onCancel); 571 doc.getElementById('button-cancel').addEventListener('click', onCancel);
600 doc.getElementById('button-retry').addEventListener('click', onRetry); 572 doc.getElementById('button-retry').addEventListener('click', onRetry);
601 doc.getElementById('button-send-feedback') 573 doc.getElementById('button-send-feedback')
602 .addEventListener('click', onSendFeedback); 574 .addEventListener('click', onSendFeedback);
603 575
604 connectPort(); 576 connectPort();
605 }; 577 };
606 578
607 var onWindowCreated = function(createdWindow) { 579 var onWindowCreated = function(createdWindow) {
608 appWindow = createdWindow; 580 appWindow = createdWindow;
609 appWindow.contentWindow.onload = onAppContentLoad; 581 appWindow.contentWindow.onload = onAppContentLoad;
610 createdWindow.onClosed.addListener(onWindowClosed); 582 appWindow.onClosed.addListener(onWindowClosed);
611 583
612 setWindowBounds(); 584 setWindowBounds();
613 }; 585 };
614 586
615 var onWindowClosed = function() { 587 var onWindowClosed = function() {
616 if (termsReloadTimeout) { 588 appWindow = null;
617 clearTimeout(termsReloadTimeout);
618 termsReloadTimeout = null;
619 }
620 589
621 if (windowClosedInternally) { 590 // Notify to Chrome.
622 return; 591 sendNativeMessage('onWindowClosed');
623 } 592
624 sendNativeMessage('cancelAuthCode'); 593 // On window closed, then dispose the extension. So, close the port
594 // otherwise the background page would be kept alive so that the extension
595 // would not be unloaded.
596 port.disconnect();
597 port = null;
625 }; 598 };
626 599
627 windowClosedInternally = false;
628
629 var options = { 600 var options = {
630 'id': 'play_store_wnd', 601 'id': 'play_store_wnd',
631 'resizable': false, 602 'resizable': false,
632 'hidden': true, 603 'hidden': true,
633 'frame': { 604 'frame': {
634 type: 'chrome', 605 type: 'chrome',
635 color: '#ffffff' 606 color: '#ffffff'
636 }, 607 },
637 'innerBounds': { 608 'innerBounds': {
638 'width': INNER_WIDTH, 609 'width': INNER_WIDTH,
639 'height': INNER_HEIGHT 610 'height': INNER_HEIGHT
640 } 611 }
641 }; 612 };
642 chrome.app.window.create('main.html', options, onWindowCreated); 613 chrome.app.window.create('main.html', options, onWindowCreated);
643 }); 614 });
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/arc/arc_support_host.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698