Skip to content

Commit 4d4eaf9

Browse files
committed
fix($sniffer): don't use history.pushState for sandboxed Chrome apps
Check in $sniffer if operating in a sandboxed Chrome app, which does not have access to chrome.app.runtime like other apps, but also does not have access to history.pushState. If inside a sandboxed Chrome app, do not try and use history.pushState, else an error will occur. See angular#11932 and angular#13945 for previous work.
1 parent a272a3c commit 4d4eaf9

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/ng/sniffer.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,13 @@ function $SnifferProvider() {
2121
this.$get = ['$window', '$document', function($window, $document) {
2222
var eventSupport = {},
2323
// Chrome Packaged Apps are not allowed to access `history.pushState`. They can be detected by
24-
// the presence of `chrome.app.runtime` (see https://developer.chrome.com/apps/api_index)
25-
isChromePackagedApp = $window.chrome && $window.chrome.app && $window.chrome.app.runtime,
24+
// the presence of `chrome.app.runtime` (see https://developer.chrome.com/apps/api_index).
25+
// For sandboxed apps, check for an extension runtime ID, but no access to other Chrome
26+
// runtime APIs. See https://developer.chrome.com/apps/manifest/sandbox
27+
isChromePackagedApp =
28+
$window.chrome &&
29+
($window.chrome.app && $window.chrome.app.runtime ||
30+
!$window.chrome.app && $window.chrome.runtime && $window.chrome.runtime.id),
2631
hasHistoryPushState = !isChromePackagedApp && $window.history && $window.history.pushState,
2732
android =
2833
toInt((/android (\d+)/.exec(lowercase(($window.navigator || {}).userAgent)) || [])[1]),

test/ng/snifferSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,26 @@ describe('$sniffer', function() {
9090

9191
expect(pushStateAccessCount).toBe(0);
9292
});
93+
94+
it('should not try to access `history.pushState` in sandbox Chrome Packaged Apps', function() {
95+
var pushStateAccessCount = 0;
96+
97+
var mockHistory = Object.create(Object.prototype, {
98+
pushState: {get: function() { pushStateAccessCount++; return noop; }}
99+
});
100+
var mockWindow = {
101+
chrome: {
102+
runtime: {
103+
id: 'x'
104+
}
105+
},
106+
history: mockHistory
107+
};
108+
109+
sniffer(mockWindow);
110+
111+
expect(pushStateAccessCount).toBe(0);
112+
});
93113
});
94114

95115

0 commit comments

Comments
 (0)