Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2007, 2008 Apple Inc. All rights reserved.
      3  * Copyright (C) 2008 Matt Lilek <webkit (at) 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 #ifndef InspectorResource_h
     32 #define InspectorResource_h
     33 
     34 #include "HTTPHeaderMap.h"
     35 #include "KURL.h"
     36 #include "ScriptObject.h"
     37 #include "ScriptState.h"
     38 #include "ScriptString.h"
     39 
     40 #include <wtf/CurrentTime.h>
     41 #include <wtf/OwnPtr.h>
     42 #include <wtf/PassRefPtr.h>
     43 #include <wtf/RefCounted.h>
     44 #include <wtf/RefPtr.h>
     45 
     46 namespace WebCore {
     47 
     48     class CachedResource;
     49     class DocumentLoader;
     50     class InspectorFrontend;
     51     class Frame;
     52     class ResourceResponse;
     53 
     54     class ResourceRequest;
     55 
     56     class InspectorResource : public RefCounted<InspectorResource> {
     57     public:
     58 
     59         // Keep these in sync with WebInspector.Resource.Type
     60         enum Type {
     61             Doc,
     62             Stylesheet,
     63             Image,
     64             Font,
     65             Script,
     66             XHR,
     67             Media,
     68             Other
     69         };
     70 
     71         static PassRefPtr<InspectorResource> create(unsigned long identifier, DocumentLoader* loader, const KURL& requestURL)
     72         {
     73             return adoptRef(new InspectorResource(identifier, loader, requestURL));
     74         }
     75 
     76         static PassRefPtr<InspectorResource> createCached(unsigned long identifier, DocumentLoader*, const CachedResource*);
     77 
     78         ~InspectorResource();
     79 
     80         PassRefPtr<InspectorResource> appendRedirect(unsigned long identifier, const KURL& redirectURL);
     81         void updateScriptObject(InspectorFrontend* frontend);
     82         void releaseScriptObject(InspectorFrontend* frontend, bool callRemoveResource);
     83 
     84         void updateRequest(const ResourceRequest&);
     85         void updateResponse(const ResourceResponse&);
     86 
     87         void setXMLHttpResponseText(const ScriptString& data);
     88 
     89         String sourceString() const;
     90         PassRefPtr<SharedBuffer> resourceData(String* textEncodingName) const;
     91 
     92         bool isSameLoader(DocumentLoader* loader) const { return loader == m_loader; }
     93         void markMainResource() { m_isMainResource = true; }
     94         unsigned long identifier() const { return m_identifier; }
     95         KURL requestURL() const { return m_requestURL; }
     96         Frame* frame() const { return m_frame.get(); }
     97         const String& mimeType() const { return m_mimeType; }
     98         const HTTPHeaderMap& requestHeaderFields() const { return m_requestHeaderFields; }
     99         const HTTPHeaderMap& responseHeaderFields() const { return m_responseHeaderFields; }
    100         int responseStatusCode() const { return m_responseStatusCode; }
    101         String requestMethod() const { return m_requestMethod; }
    102         String requestFormData() const { return m_requestFormData; }
    103 
    104         void startTiming();
    105         void markResponseReceivedTime();
    106         void markLoadEventTime();
    107         void markDOMContentEventTime();
    108         void endTiming();
    109 
    110         void markFailed();
    111         void addLength(int lengthReceived);
    112 
    113     private:
    114         enum ChangeType {
    115             NoChange = 0,
    116             RequestChange = 1,
    117             ResponseChange = 2,
    118             TypeChange = 4,
    119             LengthChange = 8,
    120             CompletionChange = 16,
    121             TimingChange = 32,
    122             RedirectsChange = 64
    123         };
    124 
    125         class Changes {
    126         public:
    127             Changes() : m_change(NoChange) {}
    128 
    129             inline bool hasChange(ChangeType change)
    130             {
    131                 return m_change & change || (m_change == NoChange && change == NoChange);
    132             }
    133             inline void set(ChangeType change)
    134             {
    135                 m_change = static_cast<ChangeType>(static_cast<unsigned>(m_change) | static_cast<unsigned>(change));
    136             }
    137             inline void clear(ChangeType change)
    138             {
    139                 m_change = static_cast<ChangeType>(static_cast<unsigned>(m_change) & ~static_cast<unsigned>(change));
    140             }
    141 
    142             inline void setAll() { m_change = static_cast<ChangeType>(127); }
    143             inline void clearAll() { m_change = NoChange; }
    144 
    145         private:
    146             ChangeType m_change;
    147         };
    148 
    149         InspectorResource(unsigned long identifier, DocumentLoader*, const KURL& requestURL);
    150         Type type() const;
    151 
    152         Type cachedResourceType() const;
    153         CachedResource* cachedResource() const;
    154 
    155         unsigned long m_identifier;
    156         RefPtr<DocumentLoader> m_loader;
    157         RefPtr<Frame> m_frame;
    158         KURL m_requestURL;
    159         HTTPHeaderMap m_requestHeaderFields;
    160         HTTPHeaderMap m_responseHeaderFields;
    161         String m_mimeType;
    162         String m_suggestedFilename;
    163         long long m_expectedContentLength;
    164         bool m_cached;
    165         bool m_finished;
    166         bool m_failed;
    167         int m_length;
    168         int m_responseStatusCode;
    169         double m_startTime;
    170         double m_responseReceivedTime;
    171         double m_endTime;
    172         double m_loadEventTime;
    173         double m_domContentEventTime;
    174         ScriptString m_xmlHttpResponseText;
    175         Changes m_changes;
    176         bool m_isMainResource;
    177         String m_requestMethod;
    178         String m_requestFormData;
    179         Vector<RefPtr<InspectorResource> > m_redirects;
    180     };
    181 
    182 } // namespace WebCore
    183 
    184 #endif // InspectorResource_h
    185