OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 goog.provide('__crWeb.dialogOverrides'); | 5 goog.provide('__crWeb.dialogOverrides'); |
6 | 6 |
7 goog.require('__crWeb.core'); | 7 goog.require('__crWeb.core'); |
8 | 8 |
9 // Namespace for this module. | |
10 __gCrWeb.dialogOverrides = {}; | 9 __gCrWeb.dialogOverrides = {}; |
11 | 10 |
12 // Beginning of anonymous object. | |
13 (function() { | 11 (function() { |
14 /* | 12 /* |
15 * Install a wrapper around functions displaying dialogs in order to catch | 13 * Installs a wrapper around geolocation functions displaying dialogs in order |
16 * code displaying dialog. | 14 * to control their suppression. |
17 * | 15 * |
18 * Since the Javascript on the page may cache the value of those functions | 16 * Since the Javascript on the page may cache the value of those functions |
19 * and invoke them later, we must only install the wrapper once and change | 17 * and invoke them later, we must only install the wrapper once and change |
20 * their behaviour when required. | 18 * their behaviour when required. |
21 * | 19 * |
22 * Returns a function that allows changing the value of the two booleans | 20 * Returns a function that allows changing the value of |
23 * |suppressDialogs| and |notifyAboutDialogs| that are tested by the wrappers. | 21 * |suppressGeolocationDialogs| that is tested by the wrappers. |
24 */ | 22 */ |
25 var installDialogOverridesMethods = function() { | 23 var installGeolocationDialogOverridesMethods = function() { |
26 var suppressDialogs = false; | 24 var suppressGeolocationDialogs = false; |
27 var notifyAboutDialogs = false; | |
28 | 25 |
29 // Returns a wrapper function around |originalDialog|. The wrapper may | 26 // Returns a wrapper function around original dialog function. The wrapper |
30 // suppress the dialog and notify host about show/suppress. | 27 // may suppress the dialog and notify host. |
31 var makeDialogWrapper = function(originalDialogGetter) { | 28 var makeDialogWrapper = function(originalDialogGetter) { |
32 return function() { | 29 return function() { |
33 if (!suppressDialogs) { | 30 if (suppressGeolocationDialogs) { |
| 31 __gCrWeb.message.invokeOnHost({ |
| 32 'command': 'geolocationDialog.suppressed' |
| 33 }); |
| 34 } else { |
34 return originalDialogGetter().apply(null, arguments); | 35 return originalDialogGetter().apply(null, arguments); |
35 } else if (notifyAboutDialogs) { | |
36 __gCrWeb.message.invokeOnHost({'command': 'dialog.suppressed'}); | |
37 } | 36 } |
38 }; | 37 }; |
39 }; | 38 }; |
40 | 39 |
41 // Install wrapper around the following properties of |window|. | |
42 var wrappedFunctionNames = ['alert', 'confirm', 'prompt', 'open']; | |
43 var len = wrappedFunctionNames.length; | |
44 for (var i = 0; i < len; i++) { | |
45 (function(wrappedFunctionName) { | |
46 var wrappedDialogMethod = window[wrappedFunctionName]; | |
47 window[wrappedFunctionName] = makeDialogWrapper( | |
48 function() { return wrappedDialogMethod; }); | |
49 })(wrappedFunctionNames[i]); | |
50 } | |
51 | |
52 // Reading or writing to the property 'geolocation' too early breaks | 40 // Reading or writing to the property 'geolocation' too early breaks |
53 // the API. Make a copy of navigator and stub in the required methods | 41 // the API. Make a copy of navigator and stub in the required methods |
54 // without touching the property. See crbug.com/280818 for more | 42 // without touching the property. See crbug.com/280818 for more |
55 // details. | 43 // details. |
56 var stubNavigator = {}; | 44 var stubNavigator = {}; |
57 | 45 |
58 // Copy all properties and functions without touching 'geolocation'. | 46 // Copy all properties and functions without touching 'geolocation'. |
59 var oldNavigator = navigator; | 47 var oldNavigator = navigator; |
60 for (var keyName in navigator) { | 48 for (var keyName in navigator) { |
61 if (keyName !== 'geolocation') { | 49 if (keyName !== 'geolocation') { |
(...skipping 30 matching lines...) Expand all Loading... |
92 }); | 80 }); |
93 })(geoPropNames[i]); | 81 })(geoPropNames[i]); |
94 } | 82 } |
95 stubNavigator.geolocation = geolocation; | 83 stubNavigator.geolocation = geolocation; |
96 } | 84 } |
97 | 85 |
98 // Install |stubNavigator| as |navigator|. | 86 // Install |stubNavigator| as |navigator|. |
99 /** @suppress {const} */ | 87 /** @suppress {const} */ |
100 navigator = stubNavigator; | 88 navigator = stubNavigator; |
101 | 89 |
102 // Returns the closure allowing to change |suppressDialogs| and | 90 // Returns the closure allowing to change |suppressGeolocationDialogs|. |
103 // |notifyAboutDialogs| variables. | 91 return function(setEnabled) { |
104 return function(setEnabled, setNotify) { | 92 suppressGeolocationDialogs = setEnabled; |
105 suppressDialogs = setEnabled; | |
106 notifyAboutDialogs = setNotify; | |
107 }; | 93 }; |
108 }; | 94 }; |
109 | 95 |
110 // Override certain methods that produce dialogs. This needs to be installed | 96 // Override geolocation methods that produce dialogs. |
111 // after other window methods overrides. | 97 __gCrWeb['setSuppressGeolocationDialogs'] = |
112 __gCrWeb['setSuppressDialogs'] = installDialogOverridesMethods(); | 98 installGeolocationDialogOverridesMethods(); |
113 | 99 |
114 }()); // End of anonymous object | 100 }()); // End of anonymous object |
OLD | NEW |