cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / chrome / browser / resources / cryptotoken / webrequestsender.js
blob329a6ab4074ff499970fd86f5a88f9c54e1c6f90
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
3 // found in the LICENSE file.
5 /**
6 * @fileoverview Provides a representation of a web request sender, and
7 * utility functions for creating them.
8 */
9 'use strict';
11 /**
12 * @typedef {{
13 * origin: string,
14 * tlsChannelId: (string|undefined),
15 * tabId: (number|undefined)
16 * }}
18 var WebRequestSender;
20 /**
21 * Creates an object representing the sender's origin, and, if available,
22 * tab.
23 * @param {MessageSender} messageSender The message sender.
24 * @return {?WebRequestSender} The sender's origin and tab, or null if the
25 * sender is invalid.
27 function createSenderFromMessageSender(messageSender) {
28 var origin = getOriginFromUrl(/** @type {string} */ (messageSender.url));
29 if (!origin) {
30 return null;
32 var sender = {
33 origin: origin
35 if (messageSender.tlsChannelId) {
36 sender.tlsChannelId = messageSender.tlsChannelId;
38 if (messageSender.tab) {
39 sender.tabId = messageSender.tab.id;
41 return sender;
44 /**
45 * Checks whether the given tab could have sent a message from the given
46 * origin.
47 * @param {Tab} tab The tab to match
48 * @param {string} origin The origin to check.
49 * @return {Promise} A promise resolved with the tab id if it the tab could,
50 * have sent the request, and rejected if it can't.
52 function tabMatchesOrigin(tab, origin) {
53 // If the tab's origin matches, trust that the request came from this tab.
54 if (getOriginFromUrl(tab.url) == origin) {
55 return Promise.resolve(tab.id);
57 return Promise.reject(false);
60 /**
61 * Attempts to ensure that the tabId of the sender is set, using chrome.tabs
62 * when available.
63 * @param {WebRequestSender} sender The request sender.
64 * @return {Promise} A promise resolved once the tabId retrieval is done.
65 * The promise is rejected if the tabId is untrustworthy, e.g. if the
66 * user rapidly switched tabs.
68 function getTabIdWhenPossible(sender) {
69 if (sender.tabId) {
70 // Already got it? Done.
71 return Promise.resolve(true);
72 } else if (!chrome.tabs) {
73 // Can't get it? Done. (This happens to packaged apps, which can't access
74 // chrome.tabs.)
75 return Promise.resolve(true);
76 } else {
77 return new Promise(function(resolve, reject) {
78 chrome.tabs.query({active: true, lastFocusedWindow: true},
79 function(tabs) {
80 if (!tabs.length) {
81 // Safety check.
82 reject(false);
83 return;
85 var tab = tabs[0];
86 tabMatchesOrigin(tab, sender.origin).then(function(tabId) {
87 sender.tabId = tabId;
88 resolve(true);
89 }, function() {
90 // Didn't match? Check if the debugger is open.
91 if (tab.url.indexOf('chrome-devtools://') != 0) {
92 reject(false);
93 return;
95 // Debugger active: find first tab with the sender's origin.
96 chrome.tabs.query({active: true}, function(tabs) {
97 if (!tabs.length) {
98 // Safety check.
99 reject(false);
100 return;
102 var numRejected = 0;
103 for (var i = 0; i < tabs.length; i++) {
104 tab = tabs[i];
105 tabMatchesOrigin(tab, sender.origin).then(function(tabId) {
106 sender.tabId = tabId;
107 resolve(true);
108 }, function() {
109 if (++numRejected >= tabs.length) {
110 // None matches: reject.
111 reject(false);
123 * Checks whether the given tab is in the foreground, i.e. is the active tab
124 * of the focused window.
125 * @param {number} tabId The tab id to check.
126 * @return {Promise<boolean>} A promise for the result of the check.
128 function tabInForeground(tabId) {
129 return new Promise(function(resolve, reject) {
130 if (!chrome.tabs || !chrome.tabs.get) {
131 reject();
132 return;
134 if (!chrome.windows || !chrome.windows.get) {
135 reject();
136 return;
138 chrome.tabs.get(tabId, function(tab) {
139 if (chrome.runtime.lastError) {
140 resolve(false);
141 return;
143 if (!tab.active) {
144 resolve(false);
145 return;
147 chrome.windows.get(tab.windowId, function(aWindow) {
148 resolve(aWindow && aWindow.focused);