OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2007, 2008 Apple Inc. All rights reserved. |
| 3 * Copyright (C) 2008 Matt Lilek <webkit@mattlilek.com> |
| 4 * Copyright (C) 2009 Google Inc. All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions |
| 8 * are met: |
| 9 * |
| 10 * 1. Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * 2. Redistributions in binary form must reproduce the above copyright |
| 13 * notice, this list of conditions and the following disclaimer in the |
| 14 * documentation and/or other materials provided with the distribution. |
| 15 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of |
| 16 * its contributors may be used to endorse or promote products derived |
| 17 * from this software without specific prior written permission. |
| 18 * |
| 19 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
| 20 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 #include "InspectorResource.h" |
| 33 |
| 34 #include "CachedResource.h" |
| 35 #include "DocLoader.h" |
| 36 #include "DocumentLoader.h" |
| 37 #include "Frame.h" |
| 38 #include "TextEncoding.h" |
| 39 |
| 40 #include <runtime/JSLock.h> |
| 41 |
| 42 namespace WebCore { |
| 43 |
| 44 // XMLHttpRequestResource Class |
| 45 |
| 46 struct XMLHttpRequestResource { |
| 47 XMLHttpRequestResource(const JSC::UString& sourceString) |
| 48 { |
| 49 JSC::JSLock lock(false); |
| 50 this->sourceString = sourceString.rep(); |
| 51 } |
| 52 |
| 53 ~XMLHttpRequestResource() |
| 54 { |
| 55 JSC::JSLock lock(false); |
| 56 sourceString.clear(); |
| 57 } |
| 58 |
| 59 RefPtr<JSC::UString::Rep> sourceString; |
| 60 }; |
| 61 |
| 62 InspectorResource::InspectorResource(long long identifier, DocumentLoader* d
ocumentLoader, Frame* frame) |
| 63 : identifier(identifier) |
| 64 , loader(documentLoader) |
| 65 , frame(frame) |
| 66 , scriptContext(0) |
| 67 , scriptObject(0) |
| 68 , expectedContentLength(0) |
| 69 , cached(false) |
| 70 , finished(false) |
| 71 , failed(false) |
| 72 , length(0) |
| 73 , responseStatusCode(0) |
| 74 , startTime(-1.0) |
| 75 , responseReceivedTime(-1.0) |
| 76 , endTime(-1.0) |
| 77 , xmlHttpRequestResource(0) |
| 78 { |
| 79 } |
| 80 |
| 81 InspectorResource::~InspectorResource() |
| 82 { |
| 83 setScriptObject(0, 0); |
| 84 } |
| 85 |
| 86 InspectorResource::Type InspectorResource::type() const |
| 87 { |
| 88 if (xmlHttpRequestResource) |
| 89 return XHR; |
| 90 |
| 91 if (requestURL == loader->requestURL()) |
| 92 return Doc; |
| 93 |
| 94 if (loader->frameLoader() && requestURL == loader->frameLoader()->iconURL()) |
| 95 return Image; |
| 96 |
| 97 CachedResource* cachedResource = frame->document()->docLoader()->cachedResou
rce(requestURL.string()); |
| 98 if (!cachedResource) |
| 99 return Other; |
| 100 |
| 101 switch (cachedResource->type()) { |
| 102 case CachedResource::ImageResource: |
| 103 return Image; |
| 104 case CachedResource::FontResource: |
| 105 return Font; |
| 106 case CachedResource::CSSStyleSheet: |
| 107 #if ENABLE(XSLT) |
| 108 case CachedResource::XSLStyleSheet: |
| 109 #endif |
| 110 return Stylesheet; |
| 111 case CachedResource::Script: |
| 112 return Script; |
| 113 default: |
| 114 return Other; |
| 115 } |
| 116 } |
| 117 |
| 118 void InspectorResource::setScriptObject(JSContextRef context, JSObjectRef newScr
iptObject) |
| 119 { |
| 120 if (scriptContext && scriptObject) |
| 121 JSValueUnprotect(scriptContext, scriptObject); |
| 122 |
| 123 scriptObject = newScriptObject; |
| 124 scriptContext = context; |
| 125 |
| 126 ASSERT((context && newScriptObject) || (!context && !newScriptObject)); |
| 127 if (context && newScriptObject) |
| 128 JSValueProtect(context, newScriptObject); |
| 129 } |
| 130 |
| 131 void InspectorResource::setXMLHttpRequestProperties(const JSC::UString& data) |
| 132 { |
| 133 xmlHttpRequestResource.set(new XMLHttpRequestResource(data)); |
| 134 } |
| 135 |
| 136 String InspectorResource::sourceString() const |
| 137 { |
| 138 if (xmlHttpRequestResource) |
| 139 return JSC::UString(xmlHttpRequestResource->sourceString); |
| 140 |
| 141 RefPtr<SharedBuffer> buffer; |
| 142 String textEncodingName; |
| 143 |
| 144 if (requestURL == loader->requestURL()) { |
| 145 buffer = loader->mainResourceData(); |
| 146 textEncodingName = frame->document()->inputEncoding(); |
| 147 } else { |
| 148 CachedResource* cachedResource = frame->document()->docLoader()->cachedR
esource(requestURL.string()); |
| 149 if (!cachedResource) |
| 150 return String(); |
| 151 |
| 152 if (cachedResource->isPurgeable()) { |
| 153 // If the resource is purgeable then make it unpurgeable to get |
| 154 // get its data. This might fail, in which case we return an |
| 155 // empty String. |
| 156 // FIXME: should we do something else in the case of a purged |
| 157 // resource that informs the user why there is no data in the |
| 158 // inspector? |
| 159 if (!cachedResource->makePurgeable(false)) |
| 160 return String(); |
| 161 } |
| 162 |
| 163 buffer = cachedResource->data(); |
| 164 textEncodingName = cachedResource->encoding(); |
| 165 } |
| 166 |
| 167 if (!buffer) |
| 168 return String(); |
| 169 |
| 170 TextEncoding encoding(textEncodingName); |
| 171 if (!encoding.isValid()) |
| 172 encoding = WindowsLatin1Encoding(); |
| 173 return encoding.decode(buffer->data(), buffer->size()); |
| 174 } |
| 175 |
| 176 } // namespace WebCore |
OLD | NEW |