1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview 'Net' manages resources along with the corresponding
* HTTP requests and responses.
* web inspector.
*/
goog.provide('devtools.NetAgent');
devtools.NetAgent = function() {
this.resources_ = {};
this.id_for_url_ = {};
RemoteNetAgent.GetResourceContentResult =
devtools.Callback.processCallback;
RemoteNetAgent.WillSendRequest =
goog.bind(this.willSendRequest, this);
RemoteNetAgent.DidReceiveResponse =
goog.bind(this.didReceiveResponse, this);
RemoteNetAgent.DidFinishLoading =
goog.bind(this.didFinishLoading, this);
RemoteNetAgent.DidFailLoading =
goog.bind(this.didFailLoading, this);
};
/**
* Resets dom agent to its initial state.
*/
devtools.NetAgent.prototype.reset = function() {
this.resources_ = {};
this.id_for_url_ = {};
};
/**
* Returns resource object for given identifier.
* @param {number} identifier Identifier to get resource for.
* @return {WebInspector.Resouce} Resulting resource.
*/
devtools.NetAgent.prototype.getResource = function(identifier) {
return this.resources_[identifier];
};
/**
* Asynchronously queries for the resource content.
* @param {number} identifier Resource identifier.
* @param {function(string):undefined} opt_callback Callback to call when
* result is available.
*/
devtools.NetAgent.prototype.getResourceContentAsync = function(identifier,
opt_callback) {
var resource = this.resources_[identifier];
if (!resource) {
return;
}
var mycallback = function(content) {
if (opt_callback) {
opt_callback(content);
}
};
RemoteNetAgent.GetResourceContent(
devtools.Callback.wrap(mycallback), identifier, resource.url);
};
/**
* @see NetAgentDelegate.
* {@inheritDoc}.
*/
devtools.NetAgent.prototype.willSendRequest = function(identifier, request) {
// Resource object is already created.
var resource = this.resources_[identifier];
if (resource) {
return;
}
var mainResource = false;
var cached = false;
var resource = new WebInspector.Resource(request.requestHeaders,
request.url, request.domain, request.path, request.lastPathComponent,
identifier, mainResource, cached);
resource.startTime = request.startTime;
WebInspector.addResource(resource);
this.resources_[identifier] = resource;
this.id_for_url_[request.url] = identifier;
};
/**
* @see NetAgentDelegate.
* {@inheritDoc}.
*/
devtools.NetAgent.prototype.didReceiveResponse = function(identifier, response) {
var resource = this.resources_[identifier];
if (!resource) {
return;
}
resource.expectedContentLength = response.expectedContentLength;
resource.responseStatusCode = response.responseStatusCode;
resource.responseHeaders = response.responseHeaders;
resource.mimeType = response.mimeType;
resource.suggestedFilename = response.suggestedFilename;
var mimeType = response.mimeType;
if (mimeType.indexOf('image/') == 0) {
resource.type = WebInspector.Resource.Type.Image;
} else if (mimeType.indexOf('text/html') == 0) {
resource.type = WebInspector.Resource.Type.Document;
} else if (mimeType.indexOf('script') != -1 ||
resource.url.indexOf('.js') == resource.url.length - 3) {
resource.type = WebInspector.Resource.Type.Script;
} else if (mimeType.indexOf('text/css') == 0) {
resource.type = WebInspector.Resource.Type.Stylesheet;
} else {
resource.type = WebInspector.Resource.Type.Other;
}
resource.responseReceivedTime = response.responseReceivedTime;
};
/**
* @see NetAgentDelegate.
* {@inheritDoc}.
*/
devtools.NetAgent.prototype.didFinishLoading = function(identifier, value) {
// When loading main resource we are only getting the didFinishLoading
// that is happening after the reset. Replay previous commands here.
this.willSendRequest(identifier, value);
this.didReceiveResponse(identifier, value);
var resource = this.resources_[identifier];
if (!resource) {
return;
}
resource.endTime = value.endTime;
resource.finished = true;
resource.failed = false;
};
/**
* @see NetAgentDelegate.
* {@inheritDoc}.
*/
devtools.NetAgent.prototype.didFailLoading = function(identifier, value) {
var resource = this.resources_[identifier];
if (!resource) {
return;
}
resource.endTime = value.endTime;
resource.finished = false;
resource.failed = true;
};
|