void Loader::Host::didReceiveData(SubresourceLoader* loader, const char* data, int size) { RefPtr<Host> protector(this); Request* request = m_requestsLoading.get(loader); if (!request) return; CachedResource* resource = request->cachedResource(); ASSERT(!resource->isCacheValidator()); if (resource->errorOccurred()) return; if (resource->response().httpStatusCode() / 100 == 4) { // Treat a 4xx response like a network error for all resources but images (which will ignore the error and continue to load for // legacy compatibility). resource->httpStatusCodeError(); return; } // Set the data. if (request->isMultipart()) { // The loader delivers the data in a multipart section all at once, send eof. // The resource data will change as the next part is loaded, so we need to make a copy. RefPtr<SharedBuffer> copiedData = SharedBuffer::create(data, size); resource->data(copiedData.release(), true); } else if (request->isIncremental()) resource->data(loader->resourceData(), false); }
void Loader::Host::didReceiveData(SubresourceLoader* loader, const char* data, int size) { Request* request = m_requestsLoading.get(loader); if (!request) return; CachedResource* resource = request->cachedResource(); ASSERT(!resource->isCacheValidator()); if (resource->errorOccurred()) return; m_processingResource = true; if (resource->response().httpStatusCode() / 100 == 4) { // Treat a 4xx response like a network error. resource->error(); m_processingResource = false; return; } // Set the data. if (request->isMultipart()) { // The loader delivers the data in a multipart section all at once, send eof. // The resource data will change as the next part is loaded, so we need to make a copy. RefPtr<SharedBuffer> copiedData = SharedBuffer::create(data, size); resource->data(copiedData.release(), true); } else if (request->isIncremental()) resource->data(loader->resourceData(), false); m_processingResource = false; }
void Loader::Host::didFinishLoading(SubresourceLoader* loader) { RequestMap::iterator i = m_requestsLoading.find(loader); if (i == m_requestsLoading.end()) return; Request* request = i->second; m_requestsLoading.remove(i); DocLoader* docLoader = request->docLoader(); if (!request->isMultipart()) docLoader->decrementRequestCount(); CachedResource* resource = request->cachedResource(); // If we got a 4xx response, we're pretending to have received a network // error, so we can't send the successful data() and finish() callbacks. if (!resource->errorOccurred()) { docLoader->setLoadInProgress(true); resource->data(loader->resourceData(), true); resource->finish(); } delete request; docLoader->setLoadInProgress(false); #if REQUEST_DEBUG KURL u(resource->url()); printf("HOST %s COUNT %d RECEIVED %s\n", u.host().latin1().data(), m_requestsLoading.size(), resource->url().latin1().data()); #endif servePendingRequests(); }
PassRefPtr<SharedBuffer> InspectorResourceAgent::resourceData(Frame* frame, const KURL& url, String* textEncodingName) { FrameLoader* frameLoader = frame->loader(); DocumentLoader* loader = frameLoader->documentLoader(); if (equalIgnoringFragmentIdentifier(url, loader->url())) { *textEncodingName = frame->document()->inputEncoding(); return frameLoader->documentLoader()->mainResourceData(); } CachedResource* cachedResource = InspectorResourceAgent::cachedResource(frame, url); if (!cachedResource) return 0; if (cachedResource->isPurgeable()) { // If the resource is purgeable then make it unpurgeable to get // get its data. This might fail, in which case we return an // empty String. // FIXME: should we do something else in the case of a purged // resource that informs the user why there is no data in the // inspector? if (!cachedResource->makePurgeable(false)) return 0; } *textEncodingName = cachedResource->encoding(); return cachedResource->data(); }
PassRefPtr<SharedBuffer> InspectorResource::resourceData(String* textEncodingName) const { if (m_requestURL == m_loader->requestURL()) { *textEncodingName = m_frame->document()->inputEncoding(); return m_loader->mainResourceData(); } CachedResource* cachedResource = this->cachedResource(); if (!cachedResource) return 0; if (cachedResource->isPurgeable()) { // If the resource is purgeable then make it unpurgeable to get // get its data. This might fail, in which case we return an // empty String. // FIXME: should we do something else in the case of a purged // resource that informs the user why there is no data in the // inspector? if (!cachedResource->makePurgeable(false)) return 0; } *textEncodingName = cachedResource->encoding(); return cachedResource->data(); }
void Loader::didReceiveData(SubresourceLoader* loader, const char* data, int size) { Request* request = m_requestsLoading.get(loader); if (!request) return; CachedResource* object = request->cachedResource(); // Set the data. if (request->isMultipart()) { // The loader delivers the data in a multipart section all at once, send eof. // The resource data will change as the next part is loaded, so we need to make a copy. SharedBuffer* copiedData = new SharedBuffer(data, size); object->data(copiedData, true); } else if (request->isIncremental()) object->data(loader->resourceData(), false); }
WebResource* WebDataSource::subresourceForURL(String url) { Document *doc = m_loader->frameLoader()->frame()->document(); if (!doc) return 0; CachedResource *cachedResource = doc->docLoader()->cachedResource(String(url)); if (!cachedResource) return 0; return WebResource::createInstance(cachedResource->data(), cachedResource->response()); }
PassRefPtr<ArchiveResource> DocumentLoader::subresource(const KURL& url) const { if (!isCommitted()) return 0; Document* doc = m_frame->document(); if (!doc) return archiveResourceForURL(url); CachedResource* resource = doc->docLoader()->cachedResource(url); if (!resource || resource->preloadResult() == CachedResource::PreloadReferenced) return archiveResourceForURL(url); return ArchiveResource::create(resource->data(), url, resource->response()); }
PassRefPtr<ArchiveResource> DocumentLoader::subresource(const KURL& url) const { if (!isCommitted()) return 0; CachedResource* resource = m_frame->document()->docLoader()->cachedResource(url); if (!resource || !resource->isLoaded()) return archiveResourceForURL(url); // FIXME: This has the side effect of making the resource non-purgeable. // It would be better if it didn't have this permanent effect. if (!resource->makePurgeable(false)) return 0; RefPtr<SharedBuffer> data = resource->data(); if (!data) return 0; return ArchiveResource::create(data.release(), url, resource->response()); }
void Loader::Host::didFinishLoading(SubresourceLoader* loader) { RefPtr<Host> myProtector(this); RequestMap::iterator i = m_requestsLoading.find(loader); if (i == m_requestsLoading.end()) return; Request* request = i->second; m_requestsLoading.remove(i); DocLoader* docLoader = request->docLoader(); // Prevent the document from being destroyed before we are done with // the docLoader that it will delete when the document gets deleted. RefPtr<Document> protector(docLoader->doc()); if (!request->isMultipart()) docLoader->decrementRequestCount(); CachedResource* resource = request->cachedResource(); ASSERT(!resource->resourceToRevalidate()); // If we got a 4xx response, we're pretending to have received a network // error, so we can't send the successful data() and finish() callbacks. if (!resource->errorOccurred()) { docLoader->setLoadInProgress(true); resource->data(loader->resourceData(), true); resource->finish(); } delete request; docLoader->setLoadInProgress(false); docLoader->checkForPendingPreloads(); #if REQUEST_DEBUG KURL u(ParsedURLString, resource->url()); printf("HOST %s COUNT %d RECEIVED %s\n", u.host().latin1().data(), m_requestsLoading.size(), resource->url().latin1().data()); #endif servePendingRequests(); }
String InspectorResource::sourceString() const { if (!m_xmlHttpResponseText.isNull()) return String(m_xmlHttpResponseText); RefPtr<SharedBuffer> buffer; String textEncodingName; if (m_requestURL == m_loader->requestURL()) { buffer = m_loader->mainResourceData(); textEncodingName = m_frame->document()->inputEncoding(); } else { CachedResource* cachedResource = m_frame->document()->docLoader()->cachedResource(requestURL()); if (!cachedResource) return String(); if (cachedResource->isPurgeable()) { // If the resource is purgeable then make it unpurgeable to get // get its data. This might fail, in which case we return an // empty String. // FIXME: should we do something else in the case of a purged // resource that informs the user why there is no data in the // inspector? if (!cachedResource->makePurgeable(false)) return String(); } buffer = cachedResource->data(); textEncodingName = cachedResource->encoding(); } if (!buffer) return String(); TextEncoding encoding(textEncodingName); if (!encoding.isValid()) encoding = WindowsLatin1Encoding(); return encoding.decode(buffer->data(), buffer->size()); }
void Loader::didFinishLoading(SubresourceLoader* loader) { RequestMap::iterator i = m_requestsLoading.find(loader); if (i == m_requestsLoading.end()) return; Request* req = i->second; m_requestsLoading.remove(i); DocLoader* docLoader = req->docLoader(); if (!req->isMultipart()) docLoader->decrementRequestCount(); CachedResource* object = req->cachedResource(); docLoader->setLoadInProgress(true); object->data(loader->resourceData(), true); docLoader->setLoadInProgress(false); object->finish(); delete req; servePendingRequests(); }
HRESULT STDMETHODCALLTYPE WebDataSource::subresourceForURL( /* [in] */ BSTR url, /* [retval][out] */ IWebResource** resource) { if (!resource) { ASSERT_NOT_REACHED(); return E_POINTER; } *resource = 0; Document *doc = m_loader->frameLoader()->frame()->document(); if (!doc) return E_FAIL; CachedResource *cachedResource = doc->docLoader()->cachedResource(String(url)); if (!cachedResource) return E_FAIL; *resource = WebResource::createInstance(cachedResource->data(), cachedResource->response()); return S_OK; }
PassRefPtr<SharedBuffer> InspectorPageAgent::resourceData(Frame* frame, const KURL& url, String* textEncodingName) { RefPtr<SharedBuffer> buffer; FrameLoader* frameLoader = frame->loader(); DocumentLoader* loader = frameLoader->documentLoader(); if (equalIgnoringFragmentIdentifier(url, loader->url())) { *textEncodingName = frame->document()->inputEncoding(); buffer = frameLoader->documentLoader()->mainResourceData(); if (buffer) return buffer; } CachedResource* cachedResource = InspectorPageAgent::cachedResource(frame, url); if (!cachedResource) return 0; bool hasZeroSize; bool prepared = prepareCachedResourceBuffer(cachedResource, &hasZeroSize); if (!prepared) return 0; *textEncodingName = cachedResource->encoding(); return hasZeroSize ? SharedBuffer::create() : cachedResource->data(); }
static JSValueRef addSourceToFrame(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* /*exception*/) { JSValueRef undefined = JSValueMakeUndefined(ctx); InspectorController* controller = reinterpret_cast<InspectorController*>(JSObjectGetPrivate(thisObject)); if (argumentCount < 2 || !controller) return undefined; JSValueRef identifierValue = arguments[0]; if (!JSValueIsNumber(ctx, identifierValue)) return undefined; unsigned long identifier = static_cast<unsigned long>(JSValueToNumber(ctx, identifierValue, 0)); RefPtr<InspectorResource> resource = controller->resources().get(identifier); ASSERT(resource); if (!resource) return undefined; RefPtr<SharedBuffer> buffer; if (resource->requestURL == resource->loader->requestURL()) buffer = resource->loader->mainResourceData(); else { FrameLoader* frameLoader = resource->loader->frameLoader(); if (!frameLoader) return undefined; Document* doc = frameLoader->frame()->document(); if (!doc) return undefined; CachedResource* cachedResource = doc->docLoader()->cachedResource(resource->requestURL.url()); if (!cachedResource) return undefined; buffer = cachedResource->data(); } if (!buffer) return undefined; String textEncodingName = resource->loader->overrideEncoding(); if (!textEncodingName) textEncodingName = resource->textEncodingName; TextEncoding encoding(textEncodingName); if (!encoding.isValid()) encoding = WindowsLatin1Encoding(); String sourceString = encoding.decode(buffer->data(), buffer->size()); Node* node = toNode(toJS(arguments[1])); ASSERT(node); if (!node) return undefined; if (!node->attached()) { ASSERT_NOT_REACHED(); return undefined; } ASSERT(node->isElementNode()); if (!node->isElementNode()) return undefined; Element* element = static_cast<Element*>(node); ASSERT(element->isFrameOwnerElement()); if (!element->isFrameOwnerElement()) return undefined; HTMLFrameOwnerElement* frameOwner = static_cast<HTMLFrameOwnerElement*>(element); ASSERT(frameOwner->contentFrame()); if (!frameOwner->contentFrame()) return undefined; FrameLoader* loader = frameOwner->contentFrame()->loader(); loader->setResponseMIMEType(resource->mimeType); loader->begin(); loader->write(sourceString); loader->end(); return undefined; }
PassRefPtr<LegacyWebArchive> LegacyWebArchive::create(const String& markupString, Frame* frame, const Vector<Node*>& nodes) { ASSERT(frame); const ResourceResponse& response = frame->loader()->documentLoader()->response(); KURL responseURL = response.url(); // it's possible to have a response without a URL here // <rdar://problem/5454935> if (responseURL.isNull()) responseURL = KURL(ParsedURLString, ""); PassRefPtr<ArchiveResource> mainResource = ArchiveResource::create(utf8Buffer(markupString), responseURL, response.mimeType(), "UTF-8", frame->tree()->uniqueName()); Vector<PassRefPtr<LegacyWebArchive> > subframeArchives; Vector<PassRefPtr<ArchiveResource> > subresources; HashSet<KURL> uniqueSubresources; size_t nodesSize = nodes.size(); for (size_t i = 0; i < nodesSize; ++i) { Node* node = nodes[i]; Frame* childFrame; if ((node->hasTagName(HTMLNames::frameTag) || node->hasTagName(HTMLNames::iframeTag) || node->hasTagName(HTMLNames::objectTag)) && (childFrame = static_cast<HTMLFrameOwnerElement*>(node)->contentFrame())) { RefPtr<LegacyWebArchive> subframeArchive = create(childFrame->document()); if (subframeArchive) subframeArchives.append(subframeArchive); else LOG_ERROR("Unabled to archive subframe %s", childFrame->tree()->uniqueName().string().utf8().data()); } else { ListHashSet<KURL> subresourceURLs; node->getSubresourceURLs(subresourceURLs); DocumentLoader* documentLoader = frame->loader()->documentLoader(); ListHashSet<KURL>::iterator iterEnd = subresourceURLs.end(); for (ListHashSet<KURL>::iterator iter = subresourceURLs.begin(); iter != iterEnd; ++iter) { const KURL& subresourceURL = *iter; if (uniqueSubresources.contains(subresourceURL)) continue; uniqueSubresources.add(subresourceURL); RefPtr<ArchiveResource> resource = documentLoader->subresource(subresourceURL); if (resource) { subresources.append(resource.release()); continue; } CachedResource* cachedResource = memoryCache()->resourceForURL(subresourceURL); if (cachedResource) { resource = ArchiveResource::create(cachedResource->data(), subresourceURL, cachedResource->response()); if (resource) { subresources.append(resource.release()); continue; } } // FIXME: should do something better than spew to console here LOG_ERROR("Failed to archive subresource for %s", subresourceURL.string().utf8().data()); } } } // Add favicon if one exists for this page, if we are archiving the entire page. if (nodesSize && nodes[0]->isDocumentNode() && iconDatabase().isEnabled()) { const String& iconURL = iconDatabase().synchronousIconURLForPageURL(responseURL); if (!iconURL.isEmpty() && iconDatabase().synchronousIconDataKnownForIconURL(iconURL)) { if (Image* iconImage = iconDatabase().synchronousIconForPageURL(responseURL, IntSize(16, 16))) { if (RefPtr<ArchiveResource> resource = ArchiveResource::create(iconImage->data(), KURL(ParsedURLString, iconURL), "image/x-icon", "", "")) subresources.append(resource.release()); } } } return create(mainResource, subresources, subframeArchives); }
PassRefPtr<LegacyWebArchive> LegacyWebArchive::create(const String& markupString, Frame* frame, Vector<Node*>& nodes) { ASSERT(frame); const ResourceResponse& response = frame->loader()->documentLoader()->response(); KURL responseURL = response.url(); // it's possible to have a response without a URL here // <rdar://problem/5454935> if (responseURL.isNull()) responseURL = KURL(""); PassRefPtr<ArchiveResource> mainResource = ArchiveResource::create(utf8Buffer(markupString), responseURL, response.mimeType(), "UTF-8", frame->tree()->name()); Vector<PassRefPtr<LegacyWebArchive> > subframeArchives; Vector<PassRefPtr<ArchiveResource> > subresources; HashSet<KURL> uniqueSubresources; Vector<Node*>::iterator it = nodes.begin(); Vector<Node*>::iterator end = nodes.end(); for (; it != end; ++it) { Frame* childFrame; if (((*it)->hasTagName(HTMLNames::frameTag) || (*it)->hasTagName(HTMLNames::iframeTag) || (*it)->hasTagName(HTMLNames::objectTag)) && (childFrame = static_cast<HTMLFrameOwnerElement*>(*it)->contentFrame())) { RefPtr<LegacyWebArchive> subframeArchive; if (Document* document = childFrame->document()) subframeArchive = LegacyWebArchive::create(document); else subframeArchive = create(childFrame); if (subframeArchive) subframeArchives.append(subframeArchive); else LOG_ERROR("Unabled to archive subframe %s", childFrame->tree()->name().string().utf8().data()); } else { ListHashSet<KURL> subresourceURLs; (*it)->getSubresourceURLs(subresourceURLs); DocumentLoader* documentLoader = frame->loader()->documentLoader(); ListHashSet<KURL>::iterator iterEnd = subresourceURLs.end(); for (ListHashSet<KURL>::iterator iter = subresourceURLs.begin(); iter != iterEnd; ++iter) { const KURL& subresourceURL = *iter; if (uniqueSubresources.contains(subresourceURL)) continue; uniqueSubresources.add(subresourceURL); RefPtr<ArchiveResource> resource = documentLoader->subresource(subresourceURL); if (resource) { subresources.append(resource.release()); continue; } CachedResource *cachedResource = cache()->resourceForURL(subresourceURL); if (cachedResource) { resource = ArchiveResource::create(cachedResource->data(), subresourceURL, cachedResource->response()); if (resource) { subresources.append(resource.release()); continue; } } // FIXME: should do something better than spew to console here LOG_ERROR("Failed to archive subresource for %s", subresourceURL.string().utf8().data()); } } } return create(mainResource, subresources, subframeArchives); }