OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 SDK.CookieModel = class extends SDK.SDKModel { | 5 SDK.CookieModel = class extends SDK.SDKModel { |
6 /** | 6 /** |
7 * @param {!SDK.Target} target | 7 * @param {!SDK.Target} target |
| 8 * @param {!Protocol.Dispatcher} dispatcher |
8 */ | 9 */ |
9 constructor(target) { | 10 constructor(target, dispatcher) { |
10 super(target); | 11 super(target, dispatcher); |
| 12 this._networkAgent = dispatcher.networkAgent(); |
11 } | 13 } |
12 | 14 |
13 /** | 15 /** |
14 * @param {!Protocol.Network.Cookie} protocolCookie | 16 * @param {!Protocol.Network.Cookie} protocolCookie |
15 * @return {!SDK.Cookie} | 17 * @return {!SDK.Cookie} |
16 */ | 18 */ |
17 static _parseProtocolCookie(protocolCookie) { | 19 static _parseProtocolCookie(protocolCookie) { |
18 var cookie = new SDK.Cookie(protocolCookie.name, protocolCookie.value, null)
; | 20 var cookie = new SDK.Cookie(protocolCookie.name, protocolCookie.value, null)
; |
19 cookie.addAttribute('domain', protocolCookie['domain']); | 21 cookie.addAttribute('domain', protocolCookie['domain']); |
20 cookie.addAttribute('path', protocolCookie['path']); | 22 cookie.addAttribute('path', protocolCookie['path']); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 return resourceDomain === cookieDomain; | 57 return resourceDomain === cookieDomain; |
56 return !!resourceDomain.match( | 58 return !!resourceDomain.match( |
57 new RegExp('^([^\\.]+\\.)*' + cookieDomain.substring(1).escapeForRegExp(
) + '$', 'i')); | 59 new RegExp('^([^\\.]+\\.)*' + cookieDomain.substring(1).escapeForRegExp(
) + '$', 'i')); |
58 } | 60 } |
59 | 61 |
60 /** | 62 /** |
61 * @param {!Array<string>} urls | 63 * @param {!Array<string>} urls |
62 * @param {function(!Array<!SDK.Cookie>)} callback | 64 * @param {function(!Array<!SDK.Cookie>)} callback |
63 */ | 65 */ |
64 getCookiesAsync(urls, callback) { | 66 getCookiesAsync(urls, callback) { |
65 this.target().networkAgent().getCookies(urls, (err, cookies) => { | 67 this._networkAgent.getCookies(urls, (err, cookies) => { |
66 if (err) { | 68 if (err) { |
67 console.error(err); | 69 console.error(err); |
68 return callback([]); | 70 return callback([]); |
69 } | 71 } |
70 | 72 |
71 callback(cookies.map(cookie => SDK.CookieModel._parseProtocolCookie(cookie
))); | 73 callback(cookies.map(cookie => SDK.CookieModel._parseProtocolCookie(cookie
))); |
72 }); | 74 }); |
73 } | 75 } |
74 | 76 |
75 /** | 77 /** |
(...skipping 16 matching lines...) Expand all Loading... |
92 * @param {!SDK.Cookie} cookie | 94 * @param {!SDK.Cookie} cookie |
93 * @param {function(?Protocol.Error, boolean)} callback | 95 * @param {function(?Protocol.Error, boolean)} callback |
94 */ | 96 */ |
95 saveCookie(cookie, callback) { | 97 saveCookie(cookie, callback) { |
96 var domain = cookie.domain(); | 98 var domain = cookie.domain(); |
97 if (!domain.startsWith('.')) | 99 if (!domain.startsWith('.')) |
98 domain = ''; | 100 domain = ''; |
99 var expires = undefined; | 101 var expires = undefined; |
100 if (cookie.expires()) | 102 if (cookie.expires()) |
101 expires = Math.floor(Date.parse(cookie.expires()) / 1000); | 103 expires = Math.floor(Date.parse(cookie.expires()) / 1000); |
102 this.target().networkAgent().setCookie( | 104 this._networkAgent.setCookie( |
103 cookie.url(), cookie.name(), cookie.value(), domain, cookie.path(), cook
ie.secure(), cookie.httpOnly(), | 105 cookie.url(), cookie.name(), cookie.value(), domain, cookie.path(), cook
ie.secure(), cookie.httpOnly(), |
104 cookie.sameSite(), expires, callback); | 106 cookie.sameSite(), expires, callback); |
105 } | 107 } |
106 | 108 |
107 /** | 109 /** |
108 * @param {?string} domain | 110 * @param {?string} domain |
109 * @param {function(!Array<!SDK.Cookie>)} callback | 111 * @param {function(!Array<!SDK.Cookie>)} callback |
110 */ | 112 */ |
111 getCookiesForDomain(domain, callback) { | 113 getCookiesForDomain(domain, callback) { |
112 var resourceURLs = []; | 114 var resourceURLs = []; |
113 /** | 115 /** |
114 * @param {!SDK.Resource} resource | 116 * @param {!SDK.Resource} resource |
115 */ | 117 */ |
116 function populateResourceURLs(resource) { | 118 function populateResourceURLs(resource) { |
117 var url = resource.documentURL.asParsedURL(); | 119 var url = resource.documentURL.asParsedURL(); |
118 if (url && (!domain || url.securityOrigin() === domain)) | 120 if (url && (!domain || url.securityOrigin() === domain)) |
119 resourceURLs.push(resource.url); | 121 resourceURLs.push(resource.url); |
120 } | 122 } |
121 var resourceTreeModel = this.target().model(SDK.ResourceTreeModel); | 123 var resourceTreeModel = this.target().model(SDK.ResourceTreeModel); |
122 if (resourceTreeModel) | 124 if (resourceTreeModel) |
123 resourceTreeModel.forAllResources(populateResourceURLs); | 125 resourceTreeModel.forAllResources(populateResourceURLs); |
124 this.getCookiesAsync(resourceURLs, callback); | 126 this.getCookiesAsync(resourceURLs, callback); |
125 } | 127 } |
126 | 128 |
127 /** | 129 /** |
128 * @param {!Array<!SDK.Cookie>} cookies | 130 * @param {!Array<!SDK.Cookie>} cookies |
129 * @param {function()=} callback | 131 * @param {function()=} callback |
130 */ | 132 */ |
131 _deleteAll(cookies, callback) { | 133 _deleteAll(cookies, callback) { |
132 var networkAgent = this.target().networkAgent(); | 134 var networkAgent = this._networkAgent; |
133 function deleteCookie(cookie) { | 135 function deleteCookie(cookie) { |
134 return new Promise(resolve => networkAgent.deleteCookie(cookie.name(), coo
kie.url(), resolve)); | 136 return new Promise(resolve => networkAgent.deleteCookie(cookie.name(), coo
kie.url(), resolve)); |
135 } | 137 } |
136 Promise.all(cookies.map(deleteCookie)).then(callback || function() {}); | 138 Promise.all(cookies.map(deleteCookie)).then(callback || function() {}); |
137 } | 139 } |
138 }; | 140 }; |
139 | 141 |
140 SDK.SDKModel.register(SDK.CookieModel, SDK.Target.Capability.Network, false); | 142 SDK.SDKModel.register(SDK.CookieModel, SDK.Target.Capability.Network, false); |
OLD | NEW |