Changeset 85172 in webkit


Ignore:
Timestamp:
Apr 28, 2011 2:06:58 AM (13 years ago)
Author:
apavlov@chromium.org
Message:

2011-04-26 Alexander Pavlov <apavlov@chromium.org>

Reviewed by Pavel Feldman.

Web Inspector: Use CachedResource to retrieve charset-decoded stylesheet text
https://bugs.webkit.org/show_bug.cgi?id=59326

Concrete CachedResource successors for stylesheets and scripts
can decode their content better than the generic approach we use.

  • inspector/InspectorPageAgent.cpp: (WebCore::decodeSharedBuffer): (WebCore::prepareCachedResourceBuffer): (WebCore::cachedResourceDecoded): (WebCore::InspectorPageAgent::resourceContent): (WebCore::InspectorPageAgent::resourceData):
  • inspector/InspectorStyleSheet.cpp: (WebCore::InspectorStyleSheet::originalStyleSheetText):
Location:
trunk/Source/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r85171 r85172  
     12011-04-26  Alexander Pavlov  <apavlov@chromium.org>
     2
     3        Reviewed by Pavel Feldman.
     4
     5        Web Inspector: Use CachedResource to retrieve charset-decoded stylesheet text
     6        https://bugs.webkit.org/show_bug.cgi?id=59326
     7
     8        Concrete CachedResource successors for stylesheets and scripts
     9        can decode their content better than the generic approach we use.
     10
     11        * inspector/InspectorPageAgent.cpp:
     12        (WebCore::decodeSharedBuffer):
     13        (WebCore::prepareCachedResourceBuffer):
     14        (WebCore::cachedResourceDecoded):
     15        (WebCore::InspectorPageAgent::resourceContent):
     16        (WebCore::InspectorPageAgent::resourceData):
     17        * inspector/InspectorStyleSheet.cpp:
     18        (WebCore::InspectorStyleSheet::originalStyleSheetText):
     19
    1202011-04-28  Jon Lee  <jonlee@apple.com>
    221
  • trunk/Source/WebCore/inspector/InspectorPageAgent.cpp

    r84904 r85172  
    3636
    3737#include "Base64.h"
     38#include "CachedCSSStyleSheet.h"
     39#include "CachedResource.h"
    3840#include "CachedResourceLoader.h"
     41#include "CachedScript.h"
    3942#include "Cookie.h"
    4043#include "CookieJar.h"
     
    6265namespace WebCore {
    6366
    64 PassOwnPtr<InspectorPageAgent> InspectorPageAgent::create(InstrumentingAgents* instrumentingAgents, Page* page, InjectedScriptManager* injectedScriptManager)
    65 {
    66     return adoptPtr(new InspectorPageAgent(instrumentingAgents, page, injectedScriptManager));
    67 }
    68 
    69 void InspectorPageAgent::resourceContent(ErrorString* errorString, Frame* frame, const KURL& url, String* result)
    70 {
    71     if (!frame) {
    72         *errorString = "No frame to get resource content for";
    73         return;
    74     }
    75 
    76     String textEncodingName;
    77     RefPtr<SharedBuffer> buffer = InspectorPageAgent::resourceData(frame, url, &textEncodingName);
    78 
     67static bool decodeSharedBuffer(PassRefPtr<SharedBuffer> buffer, const String& textEncodingName, String* result)
     68{
    7969    if (buffer) {
    8070        TextEncoding encoding(textEncodingName);
     
    8272            encoding = WindowsLatin1Encoding();
    8373        *result = encoding.decode(buffer->data(), buffer->size());
    84         return;
    85     }
    86     *errorString = "No resource with given URL found";
    87 }
    88 
    89 void InspectorPageAgent::resourceContentBase64(ErrorString* errorString, Frame* frame, const KURL& url, String* result)
    90 {
    91     String textEncodingName;
    92     RefPtr<SharedBuffer> data = InspectorPageAgent::resourceData(frame, url, &textEncodingName);
    93     if (!data) {
    94         *result = String();
    95         *errorString = "No resource with given URL found";
    96         return;
    97     }
    98 
    99     *result = base64Encode(data->data(), data->size());
    100 }
    101 
    102 PassRefPtr<SharedBuffer> InspectorPageAgent::resourceData(Frame* frame, const KURL& url, String* textEncodingName)
    103 {
    104     FrameLoader* frameLoader = frame->loader();
    105     DocumentLoader* loader = frameLoader->documentLoader();
    106     if (equalIgnoringFragmentIdentifier(url, loader->url())) {
    107         *textEncodingName = frame->document()->inputEncoding();
    108         return frameLoader->documentLoader()->mainResourceData();
    109     }
    110 
    111     CachedResource* cachedResource = InspectorPageAgent::cachedResource(frame, url);
     74        return true;
     75    }
     76    return false;
     77}
     78
     79static bool prepareCachedResourceBuffer(CachedResource* cachedResource, bool* hasZeroSize)
     80{
     81    *hasZeroSize = false;
    11282    if (!cachedResource)
    113         return 0;
    114 
    115     // Zero-sized resources don't have data at all -- so fake the empty buffer, insted of indicating error by returning 0.
    116     if (!cachedResource->encodedSize())
    117         return SharedBuffer::create();
     83        return false;
     84
     85    // Zero-sized resources don't have data at all -- so fake the empty buffer, instead of indicating error by returning 0.
     86    if (!cachedResource->encodedSize()) {
     87        *hasZeroSize = true;
     88        return true;
     89    }
    11890
    11991    if (cachedResource->isPurgeable()) {
     
    12597        // inspector?
    12698        if (!cachedResource->makePurgeable(false))
    127             return 0;
    128     }
     99            return false;
     100    }
     101
     102    return true;
     103}
     104
     105static bool decodeCachedResource(CachedResource* cachedResource, String* result)
     106{
     107    bool hasZeroSize;
     108    bool prepared = prepareCachedResourceBuffer(cachedResource, &hasZeroSize);
     109    if (!prepared)
     110        return false;
     111
     112    if (cachedResource) {
     113        switch (cachedResource->type()) {
     114        case CachedResource::CSSStyleSheet:
     115            *result = static_cast<CachedCSSStyleSheet*>(cachedResource)->sheetText();
     116            return true;
     117        case CachedResource::Script:
     118            *result = static_cast<CachedScript*>(cachedResource)->script();
     119            return true;
     120        default:
     121            if (hasZeroSize) {
     122                *result = "";
     123                return true;
     124            }
     125            return decodeSharedBuffer(cachedResource->data(), cachedResource->encoding(), result);
     126        }
     127    }
     128    return false;
     129}
     130
     131PassOwnPtr<InspectorPageAgent> InspectorPageAgent::create(InstrumentingAgents* instrumentingAgents, Page* page, InjectedScriptManager* injectedScriptManager)
     132{
     133    return adoptPtr(new InspectorPageAgent(instrumentingAgents, page, injectedScriptManager));
     134}
     135
     136void InspectorPageAgent::resourceContent(ErrorString* errorString, Frame* frame, const KURL& url, String* result)
     137{
     138    if (!frame) {
     139        *errorString = "No frame to get resource content for";
     140        return;
     141    }
     142
     143    FrameLoader* frameLoader = frame->loader();
     144    DocumentLoader* loader = frameLoader->documentLoader();
     145    RefPtr<SharedBuffer> buffer;
     146    bool success = false;
     147    if (equalIgnoringFragmentIdentifier(url, loader->url())) {
     148        String textEncodingName = frame->document()->inputEncoding();
     149        buffer = frameLoader->documentLoader()->mainResourceData();
     150        success = decodeSharedBuffer(buffer, textEncodingName, result);
     151    }
     152    if (!success)
     153        success = decodeCachedResource(cachedResource(frame, url), result);
     154
     155    if (!success)
     156        *errorString = "No resource with given URL found";
     157}
     158
     159void InspectorPageAgent::resourceContentBase64(ErrorString* errorString, Frame* frame, const KURL& url, String* result)
     160{
     161    String textEncodingName;
     162    RefPtr<SharedBuffer> data = InspectorPageAgent::resourceData(frame, url, &textEncodingName);
     163    if (!data) {
     164        *result = String();
     165        *errorString = "No resource with given URL found";
     166        return;
     167    }
     168
     169    *result = base64Encode(data->data(), data->size());
     170}
     171
     172PassRefPtr<SharedBuffer> InspectorPageAgent::resourceData(Frame* frame, const KURL& url, String* textEncodingName)
     173{
     174    RefPtr<SharedBuffer> buffer;
     175    FrameLoader* frameLoader = frame->loader();
     176    DocumentLoader* loader = frameLoader->documentLoader();
     177    if (equalIgnoringFragmentIdentifier(url, loader->url())) {
     178        *textEncodingName = frame->document()->inputEncoding();
     179        buffer = frameLoader->documentLoader()->mainResourceData();
     180        if (buffer)
     181            return buffer;
     182    }
     183
     184    CachedResource* cachedResource = InspectorPageAgent::cachedResource(frame, url);
     185    if (!cachedResource)
     186        return 0;
     187
     188    bool hasZeroSize;
     189    bool prepared = prepareCachedResourceBuffer(cachedResource, &hasZeroSize);
     190    if (!prepared)
     191        return 0;
    129192
    130193    *textEncodingName = cachedResource->encoding();
    131     return cachedResource->data();
     194    return hasZeroSize ? SharedBuffer::create() : cachedResource->data();
    132195}
    133196
  • trunk/Source/WebCore/inspector/InspectorStyleSheet.cpp

    r85162 r85172  
    4646#include "Node.h"
    4747#include "StyleSheetList.h"
    48 #include "TextResourceDecoder.h"
    4948#include "WebKitCSSKeyframesRule.h"
    5049
     
    10561055bool InspectorStyleSheet::originalStyleSheetText(String* result) const
    10571056{
    1058     String rawText;
    1059     bool success = inlineStyleSheetText(&rawText);
     1057    bool success = inlineStyleSheetText(result);
    10601058    if (!success)
    1061         success = resourceStyleSheetText(&rawText);
    1062     if (success) {
    1063         CString cString = rawText.utf8();
    1064         RefPtr<TextResourceDecoder> decoder = TextResourceDecoder::create("text/css");
    1065         String sheetText = decoder->decode(cString.data(), cString.length());
    1066         sheetText += decoder->flush();
    1067         *result = sheetText;
    1068     }
     1059        success = resourceStyleSheetText(result);
    10691060    return success;
    10701061}
Note: See TracChangeset for help on using the changeset viewer.