OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 #include "core/inspector/InspectorResourceContentLoader.h" | |
7 | |
8 #include "FetchInitiatorTypeNames.h" | |
9 #include "core/css/CSSStyleSheet.h" | |
10 #include "core/css/StyleSheetContents.h" | |
11 #include "core/fetch/CSSStyleSheetResource.h" | |
12 #include "core/fetch/Resource.h" | |
13 #include "core/fetch/ResourceFetcher.h" | |
14 #include "core/fetch/ResourcePtr.h" | |
15 #include "core/fetch/StyleSheetResourceClient.h" | |
16 #include "core/frame/LocalFrame.h" | |
17 #include "core/html/VoidCallback.h" | |
18 #include "core/inspector/InspectorCSSAgent.h" | |
19 #include "core/inspector/InspectorPageAgent.h" | |
20 #include "core/page/Page.h" | |
21 | |
22 namespace WebCore { | |
23 | |
24 InspectorResourceContentLoader::InspectorResourceContentLoader(Page* page, PassO wnPtr<VoidCallback> callback) | |
25 : m_callback(callback) | |
26 , m_pendingResourcesCount(1) | |
27 { | |
28 Vector<Document*> documents; | |
29 for (LocalFrame* frame = page->mainFrame(); frame; frame = frame->tree().tra verseNext()) { | |
30 documents.append(frame->document()); | |
31 documents.appendVector(InspectorPageAgent::importsForFrame(frame)); | |
32 } | |
33 for (Vector<Document*>::const_iterator it = documents.begin(); it != documen ts.end(); ++it) { | |
34 HashSet<String> urlsToFetch; | |
35 Document* document = *it; | |
36 ResourceRequest resourceRequest; | |
37 | |
38 if (!document->frame() || !document->frame()->loader().documentResourceR equestFromCurrentHistoryItemForInspector(ReturnCacheDataDontLoad, &resourceReque st)) { | |
pfeldman
2014/05/20 19:50:49
It is unclear why we use this for document, but no
| |
39 resourceRequest = document->url(); | |
40 resourceRequest.setCachePolicy(ReturnCacheDataDontLoad); | |
41 } | |
42 | |
43 if (!resourceRequest.url().string().isEmpty()) { | |
44 urlsToFetch.add(resourceRequest.url().string()); | |
45 ++m_pendingResourcesCount; | |
46 FetchRequest request(resourceRequest, FetchInitiatorTypeNames::inter nal); | |
47 ResourcePtr<Resource> resource = document->fetcher()->fetchRawResour ce(request); | |
48 // Prevent garbage collection by holding a reference to this resourc e. | |
49 m_pendingResources.add(resource.get()); | |
50 m_resources.append(resource.get()); | |
51 resource->addClient(static_cast<RawResourceClient*>(this)); | |
52 } | |
53 | |
54 Vector<CSSStyleSheet*> styleSheets; | |
55 InspectorCSSAgent::collectAllDocumentStyleSheets(document, styleSheets); | |
56 for (Vector<CSSStyleSheet*>::const_iterator it2 = styleSheets.begin(); i t2 != styleSheets.end(); ++it2) { | |
57 CSSStyleSheet* styleSheet = *it2; | |
58 if (styleSheet->isInline() || !styleSheet->contents()->loadCompleted ()) | |
59 continue; | |
60 String url = styleSheet->contents()->baseURL().string(); | |
61 if (url.isEmpty() || urlsToFetch.contains(url)) | |
62 continue; | |
63 urlsToFetch.add(url); | |
64 ++m_pendingResourcesCount; | |
65 FetchRequest request(ResourceRequest(url), FetchInitiatorTypeNames:: internal); | |
66 ResourcePtr<Resource> resource = document->fetcher()->fetchCSSStyleS heet(request); | |
67 // Prevent garbage collection by holding a reference to this resourc e. | |
68 m_pendingResources.add(resource.get()); | |
69 m_resources.append(resource.get()); | |
70 resource->addClient(static_cast<StyleSheetResourceClient*>(this)); | |
71 } | |
72 } | |
73 | |
74 // m_pendingResourcesCount is set to 1 initially to prevent calling back bef ore all request were sent in case they finish synchronously. | |
75 worked(); | |
76 } | |
77 | |
78 InspectorResourceContentLoader::~InspectorResourceContentLoader() | |
79 { | |
80 for (HashSet<Resource*>::const_iterator it = m_pendingResources.begin(); it != m_pendingResources.end(); ++it) | |
81 removeAsClientFromResource(*it); | |
82 m_pendingResources.clear(); | |
83 } | |
84 | |
85 void InspectorResourceContentLoader::removeAsClientFromResource(Resource* resour ce) | |
86 { | |
87 if (resource->type() == Resource::Raw) | |
88 resource->removeClient(static_cast<RawResourceClient*>(this)); | |
89 else | |
90 resource->removeClient(static_cast<StyleSheetResourceClient*>(this)); | |
91 } | |
92 | |
93 bool InspectorResourceContentLoader::hasFinished() | |
94 { | |
95 return m_pendingResourcesCount == 0; | |
96 } | |
97 | |
98 void InspectorResourceContentLoader::worked() | |
99 { | |
100 --m_pendingResourcesCount; | |
101 if (!hasFinished()) | |
102 return; | |
103 OwnPtr<VoidCallback> callback; | |
104 callback = m_callback.release(); | |
105 return callback->handleEvent(); | |
106 } | |
107 | |
108 void InspectorResourceContentLoader::notifyFinished(Resource* resource) | |
109 { | |
110 if (resource->type() == Resource::CSSStyleSheet) | |
111 return; | |
112 resourceFinished(resource); | |
113 } | |
114 | |
115 void InspectorResourceContentLoader::setCSSStyleSheet(const String&, const KURL& , const String&, const CSSStyleSheetResource* resource) | |
116 { | |
117 resourceFinished(const_cast<CSSStyleSheetResource*>(resource)); | |
118 } | |
119 | |
120 void InspectorResourceContentLoader::resourceFinished(Resource* resource) | |
121 { | |
122 m_pendingResources.remove(resource); | |
123 removeAsClientFromResource(resource); | |
124 worked(); | |
125 } | |
126 | |
127 } // namespace WebCore | |
OLD | NEW |