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) | |
25 : m_allRequestsStarted(false) | |
26 { | |
27 Vector<Document*> documents; | |
28 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverse
Next()) { | |
29 if (!frame->isLocalFrame()) | |
30 continue; | |
31 LocalFrame* localFrame = toLocalFrame(frame); | |
32 documents.append(localFrame->document()); | |
33 documents.appendVector(InspectorPageAgent::importsForFrame(localFrame)); | |
34 } | |
35 for (Vector<Document*>::const_iterator it = documents.begin(); it != documen
ts.end(); ++it) { | |
36 HashSet<String> urlsToFetch; | |
37 Document* document = *it; | |
38 ResourceRequest resourceRequest; | |
39 | |
40 if (!document->frame() || !document->frame()->loader().requestFromHistor
yForInspector(ReturnCacheDataDontLoad, &resourceRequest)) { | |
41 resourceRequest = document->url(); | |
42 resourceRequest.setCachePolicy(ReturnCacheDataDontLoad); | |
43 } | |
44 | |
45 if (!resourceRequest.url().string().isEmpty()) { | |
46 urlsToFetch.add(resourceRequest.url().string()); | |
47 FetchRequest request(resourceRequest, FetchInitiatorTypeNames::inter
nal); | |
48 ResourcePtr<Resource> resource = document->fetcher()->fetchRawResour
ce(request); | |
49 // Prevent garbage collection by holding a reference to this resourc
e. | |
50 m_pendingResources.add(resource.get()); | |
51 m_resources.append(resource.get()); | |
52 resource->addClient(static_cast<RawResourceClient*>(this)); | |
53 } | |
54 | |
55 Vector<CSSStyleSheet*> styleSheets; | |
56 InspectorCSSAgent::collectAllDocumentStyleSheets(document, styleSheets); | |
57 for (Vector<CSSStyleSheet*>::const_iterator it2 = styleSheets.begin(); i
t2 != styleSheets.end(); ++it2) { | |
58 CSSStyleSheet* styleSheet = *it2; | |
59 if (styleSheet->isInline() || !styleSheet->contents()->loadCompleted
()) | |
60 continue; | |
61 String url = styleSheet->contents()->baseURL().string(); | |
62 if (url.isEmpty() || urlsToFetch.contains(url)) | |
63 continue; | |
64 urlsToFetch.add(url); | |
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 if (m_pendingResources.contains(resource.get())) | |
69 continue; | |
70 m_pendingResources.add(resource.get()); | |
71 m_resources.append(resource.get()); | |
72 resource->addClient(static_cast<StyleSheetResourceClient*>(this)); | |
73 } | |
74 } | |
75 | |
76 m_allRequestsStarted = true; | |
77 checkDone(); | |
78 } | |
79 | |
80 void InspectorResourceContentLoader::addListener(PassOwnPtr<VoidCallback> callba
ck) | |
81 { | |
82 m_callbacks.append(callback); | |
83 checkDone(); | |
84 } | |
85 | |
86 InspectorResourceContentLoader::~InspectorResourceContentLoader() | |
87 { | |
88 stop(); | |
89 } | |
90 | |
91 void InspectorResourceContentLoader::stop() | |
92 { | |
93 HashSet<Resource*> pendingResources; | |
94 m_pendingResources.swap(pendingResources); | |
95 for (HashSet<Resource*>::const_iterator it = pendingResources.begin(); it !=
pendingResources.end(); ++it) | |
96 removeAsClientFromResource(*it); | |
97 // Make sure all callbacks are called to prevent infinite waiting time. | |
98 checkDone(); | |
99 } | |
100 | |
101 void InspectorResourceContentLoader::removeAsClientFromResource(Resource* resour
ce) | |
102 { | |
103 if (resource->type() == Resource::Raw) | |
104 resource->removeClient(static_cast<RawResourceClient*>(this)); | |
105 else | |
106 resource->removeClient(static_cast<StyleSheetResourceClient*>(this)); | |
107 } | |
108 | |
109 bool InspectorResourceContentLoader::hasFinished() | |
110 { | |
111 return m_allRequestsStarted && m_pendingResources.size() == 0; | |
112 } | |
113 | |
114 void InspectorResourceContentLoader::checkDone() | |
115 { | |
116 if (!hasFinished()) | |
117 return; | |
118 Vector<OwnPtr<VoidCallback> > callbacks; | |
119 callbacks.swap(m_callbacks); | |
120 for (Vector<OwnPtr<VoidCallback> >::const_iterator it = callbacks.begin(); i
t != callbacks.end(); ++it) | |
121 (*it)->handleEvent(); | |
122 } | |
123 | |
124 void InspectorResourceContentLoader::notifyFinished(Resource* resource) | |
125 { | |
126 if (resource->type() == Resource::CSSStyleSheet) | |
127 return; | |
128 resourceFinished(resource); | |
129 } | |
130 | |
131 void InspectorResourceContentLoader::setCSSStyleSheet(const String&, const KURL&
, const String&, const CSSStyleSheetResource* resource) | |
132 { | |
133 resourceFinished(const_cast<CSSStyleSheetResource*>(resource)); | |
134 } | |
135 | |
136 void InspectorResourceContentLoader::resourceFinished(Resource* resource) | |
137 { | |
138 m_pendingResources.remove(resource); | |
139 removeAsClientFromResource(resource); | |
140 checkDone(); | |
141 } | |
142 | |
143 } // namespace WebCore | |
OLD | NEW |