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 | |
25 class InspectorResourceContentLoader::ResourceClient FINAL : private StyleSheetR esourceClient { | |
apavlov
2014/06/18 14:33:59
IRCL::CSSResourceClient for clarity?
vsevik
2014/06/18 14:49:56
It is going to be used for document resource as we
| |
26 public: | |
27 ResourceClient(InspectorResourceContentLoader*); | |
28 | |
29 void waitForResource(Resource*); | |
30 private: | |
apavlov
2014/06/18 14:33:59
blank line above
vsevik
2014/06/18 14:49:56
Done.
| |
31 InspectorResourceContentLoader* m_loader; | |
32 | |
33 virtual void setCSSStyleSheet(const String&, const KURL&, const String&, con st CSSStyleSheetResource*) OVERRIDE; | |
34 | |
35 friend class InspectorResourceContentLoader; | |
36 }; | |
37 | |
38 InspectorResourceContentLoader::ResourceClient::ResourceClient(InspectorResource ContentLoader* loader) | |
apavlov
2014/06/18 14:33:59
This could be inlined into the class declaration
vsevik
2014/06/18 14:49:56
Done.
| |
39 : m_loader(loader) | |
40 { | |
41 } | |
42 | |
43 void InspectorResourceContentLoader::ResourceClient::waitForResource(Resource* r esource) | |
apavlov
2014/06/18 14:33:59
ditto - it is trivial
vsevik
2014/06/18 14:49:56
Done.
| |
44 { | |
45 resource->addClient(this); | |
46 } | |
47 | |
48 void InspectorResourceContentLoader::ResourceClient::setCSSStyleSheet(const Stri ng&, const KURL& url, const String&, const CSSStyleSheetResource* resource) | |
49 { | |
50 if (m_loader) | |
51 m_loader->resourceFinished(this); | |
52 const_cast<CSSStyleSheetResource*>(resource)->removeClient(this); | |
53 delete this; | |
54 } | |
55 | |
56 InspectorResourceContentLoader::InspectorResourceContentLoader(Page* page) | |
57 : m_allRequestsStarted(false) | |
58 { | |
59 Vector<Document*> documents; | |
60 for (Frame* frame = page->mainFrame(); frame; frame = frame->tree().traverse Next()) { | |
61 if (!frame->isLocalFrame()) | |
62 continue; | |
63 LocalFrame* localFrame = toLocalFrame(frame); | |
64 documents.append(localFrame->document()); | |
65 documents.appendVector(InspectorPageAgent::importsForFrame(localFrame)); | |
66 } | |
67 for (Vector<Document*>::const_iterator it = documents.begin(); it != documen ts.end(); ++it) { | |
apavlov
2014/06/18 14:33:59
docIt?
vsevik
2014/06/18 14:49:56
Done.
| |
68 Document* document = *it; | |
69 | |
70 HashSet<String> urlsToFetch; | |
71 Vector<CSSStyleSheet*> styleSheets; | |
72 InspectorCSSAgent::collectAllDocumentStyleSheets(document, styleSheets); | |
73 for (Vector<CSSStyleSheet*>::const_iterator it2 = styleSheets.begin(); i t2 != styleSheets.end(); ++it2) { | |
apavlov
2014/06/18 14:33:59
cssIt?
vsevik
2014/06/18 14:49:56
Done.
| |
74 CSSStyleSheet* styleSheet = *it2; | |
75 if (styleSheet->isInline() || !styleSheet->contents()->loadCompleted ()) | |
76 continue; | |
77 String url = styleSheet->contents()->baseURL().string(); | |
apavlov
2014/06/18 14:33:59
styleSheet->baseURL().string() for brevity
vsevik
2014/06/18 14:49:56
Done.
| |
78 if (url.isEmpty() || urlsToFetch.contains(url)) | |
79 continue; | |
80 urlsToFetch.add(url); | |
81 FetchRequest request(ResourceRequest(url), FetchInitiatorTypeNames:: internal); | |
82 ResourcePtr<Resource> resource = document->fetcher()->fetchCSSStyleS heet(request); | |
83 // Prevent garbage collection by holding a reference to this resourc e. | |
84 m_resources.append(resource.get()); | |
apavlov
2014/06/18 14:33:59
This is not going to hold a reference to the resou
vsevik
2014/06/18 14:49:56
This will call an implicit ResourcePtr constructor
| |
85 ResourceClient* resourceClient = new ResourceClient(this); | |
86 m_pendingResourceClients.add(resourceClient); | |
87 resourceClient->waitForResource(resource.get()); | |
88 } | |
89 } | |
90 | |
91 m_allRequestsStarted = true; | |
92 checkDone(); | |
93 } | |
94 | |
95 void InspectorResourceContentLoader::addListener(PassOwnPtr<VoidCallback> callba ck) | |
96 { | |
97 m_callbacks.append(callback); | |
98 checkDone(); | |
99 } | |
100 | |
101 InspectorResourceContentLoader::~InspectorResourceContentLoader() | |
102 { | |
103 stop(); | |
104 } | |
105 | |
106 void InspectorResourceContentLoader::stop() | |
107 { | |
108 HashSet<ResourceClient*> pendingResourceClients; | |
109 m_pendingResourceClients.swap(pendingResourceClients); | |
110 for (HashSet<ResourceClient*>::const_iterator it = pendingResourceClients.be gin(); it != pendingResourceClients.end(); ++it) | |
111 (*it)->m_loader = 0; | |
112 m_resources.clear(); | |
113 // Make sure all callbacks are called to prevent infinite waiting time. | |
114 checkDone(); | |
115 } | |
116 | |
117 bool InspectorResourceContentLoader::hasFinished() | |
118 { | |
119 return m_allRequestsStarted && m_pendingResourceClients.size() == 0; | |
120 } | |
121 | |
122 void InspectorResourceContentLoader::checkDone() | |
123 { | |
124 if (!hasFinished()) | |
125 return; | |
126 Vector<OwnPtr<VoidCallback> > callbacks; | |
127 callbacks.swap(m_callbacks); | |
128 for (Vector<OwnPtr<VoidCallback> >::const_iterator it = callbacks.begin(); i t != callbacks.end(); ++it) | |
129 (*it)->handleEvent(); | |
130 } | |
131 | |
132 void InspectorResourceContentLoader::resourceFinished(ResourceClient* client) | |
133 { | |
134 m_pendingResourceClients.remove(client); | |
135 checkDone(); | |
136 } | |
137 | |
138 } // namespace WebCore | |
OLD | NEW |