summaryrefslogtreecommitdiffstats
path: root/webkit/glue/devtools/js/net_agent.js
blob: 957e119d7e9a176b9676e318bef2e9788e5bddfb (plain)
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
// 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.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);
};


/**
 * Resets dom agent to its initial state.
 */
devtools.NetAgent.prototype.reset = function() {
  this.id_for_url_ = {};
};


/**
 * 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 = WebInspector.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 = WebInspector.resources[identifier];
  if (resource) {
    return;
  }

  WebInspector.addResource(identifier, request);
  var resource = WebInspector.resources[identifier];
  resource.startTime = request.startTime;
  this.id_for_url_[resource.url] = identifier;
};


/**
 * @see NetAgentDelegate.
 * {@inheritDoc}.
 */
devtools.NetAgent.prototype.didReceiveResponse = function(identifier, response) {
  var resource = WebInspector.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 = WebInspector.resources[identifier];
  if (!resource) {
    return;
  }
  resource.endTime = value.endTime;
  resource.finished = true;
  resource.failed = !!value.errorCode;
  if (resource.mainResource) {
    document.title = 'Developer Tools - ' + resource.url;
  }
};