summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/inspector/inspector_resource_content_loader.cc
blob: 10a6e6b3c178fc170445f4c3c96fef11b940c884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/inspector/inspector_resource_content_loader.h"

#include "third_party/blink/public/mojom/fetch/fetch_api_request.mojom-blink.h"
#include "third_party/blink/public/platform/web_url_request.h"
#include "third_party/blink/renderer/core/css/css_style_sheet.h"
#include "third_party/blink/renderer/core/css/style_sheet_contents.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/frame/local_frame.h"
#include "third_party/blink/renderer/core/inspector/inspected_frames.h"
#include "third_party/blink/renderer/core/inspector/inspector_css_agent.h"
#include "third_party/blink/renderer/core/inspector/inspector_page_agent.h"
#include "third_party/blink/renderer/core/loader/document_loader.h"
#include "third_party/blink/renderer/core/loader/resource/css_style_sheet_resource.h"
#include "third_party/blink/renderer/core/page/page.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_initiator_type_names.h"
#include "third_party/blink/renderer/platform/loader/fetch/raw_resource.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_loader_options.h"

namespace blink {

// NOTE: While this is a RawResourceClient, it loads both raw and css stylesheet
// resources. Stylesheets can only safely use a RawResourceClient because it has
// no custom interface and simply uses the base ResourceClient.
class InspectorResourceContentLoader::ResourceClient final
    : public GarbageCollected<InspectorResourceContentLoader::ResourceClient>,
      private RawResourceClient {
  USING_GARBAGE_COLLECTED_MIXIN(ResourceClient);

 public:
  explicit ResourceClient(InspectorResourceContentLoader* loader)
      : loader_(loader) {}

  void Trace(Visitor* visitor) override {
    visitor->Trace(loader_);
    RawResourceClient::Trace(visitor);
  }

 private:
  Member<InspectorResourceContentLoader> loader_;

  void NotifyFinished(Resource* resource) override {
    if (loader_)
      loader_->ResourceFinished(this);
    ClearResource();
  }

  String DebugName() const override {
    return "InspectorResourceContentLoader::ResourceClient";
  }

  friend class InspectorResourceContentLoader;
};

InspectorResourceContentLoader::InspectorResourceContentLoader(
    LocalFrame* inspected_frame)
    : all_requests_started_(false),
      started_(false),
      inspected_frame_(inspected_frame),
      last_client_id_(0) {}

void InspectorResourceContentLoader::Start() {
  started_ = true;
  HeapVector<Member<Document>> documents;
  InspectedFrames* inspected_frames =
      MakeGarbageCollected<InspectedFrames>(inspected_frame_);
  for (LocalFrame* frame : *inspected_frames) {
    documents.push_back(frame->GetDocument());
    documents.AppendVector(InspectorPageAgent::ImportsForFrame(frame));
  }
  for (Document* document : documents) {
    HashSet<String> urls_to_fetch;

    ResourceRequest resource_request;
    HistoryItem* item =
        document->Loader() ? document->Loader()->GetHistoryItem() : nullptr;
    if (item) {
      resource_request =
          item->GenerateResourceRequest(mojom::FetchCacheMode::kOnlyIfCached);
    } else {
      resource_request = ResourceRequest(document->Url());
      resource_request.SetCacheMode(mojom::FetchCacheMode::kOnlyIfCached);
    }
    resource_request.SetRequestContext(mojom::RequestContextType::INTERNAL);
    if (document->Loader() &&
        document->Loader()->GetResponse().WasFetchedViaServiceWorker()) {
      resource_request.SetCacheMode(mojom::FetchCacheMode::kDefault);
    }

    ResourceFetcher* fetcher = document->Fetcher();
    if (document->ImportsController()) {
      // For @imports from HTML imported Documents, we use the
      // context document for getting origin and ResourceFetcher to use the
      // main Document's origin, while using the element document for
      // CompleteURL() to use imported Documents' base URLs.
      fetcher = document->GetExecutionContext()->Fetcher();
    }
    if (!resource_request.Url().GetString().IsEmpty()) {
      urls_to_fetch.insert(resource_request.Url().GetString());
      ResourceLoaderOptions options;
      options.initiator_info.name = fetch_initiator_type_names::kInternal;
      FetchParameters params(std::move(resource_request), options);
      ResourceClient* resource_client =
          MakeGarbageCollected<ResourceClient>(this);
      // Prevent garbage collection by holding a reference to this resource.
      resources_.push_back(
          RawResource::Fetch(params, fetcher, resource_client));
      pending_resource_clients_.insert(resource_client);
    }

    HeapVector<Member<CSSStyleSheet>> style_sheets;
    InspectorCSSAgent::CollectAllDocumentStyleSheets(document, style_sheets);
    for (CSSStyleSheet* style_sheet : style_sheets) {
      if (style_sheet->IsInline() || !style_sheet->Contents()->LoadCompleted())
        continue;
      String url = style_sheet->href();
      if (url.IsEmpty() || urls_to_fetch.Contains(url))
        continue;
      urls_to_fetch.insert(url);
      ResourceRequest resource_request(url);
      resource_request.SetRequestContext(mojom::RequestContextType::INTERNAL);
      ResourceLoaderOptions options;
      options.initiator_info.name = fetch_initiator_type_names::kInternal;
      FetchParameters params(std::move(resource_request), options);
      ResourceClient* resource_client =
          MakeGarbageCollected<ResourceClient>(this);
      // Prevent garbage collection by holding a reference to this resource.
      resources_.push_back(
          CSSStyleSheetResource::Fetch(params, fetcher, resource_client));
      // A cache hit for a css stylesheet will complete synchronously. Don't
      // mark the client as pending if it already finished.
      if (resource_client->GetResource())
        pending_resource_clients_.insert(resource_client);
    }
  }

  all_requests_started_ = true;
  CheckDone();
}

int InspectorResourceContentLoader::CreateClientId() {
  return ++last_client_id_;
}

void InspectorResourceContentLoader::EnsureResourcesContentLoaded(
    int client_id,
    base::OnceClosure callback) {
  if (!started_)
    Start();
  callbacks_.insert(client_id, Callbacks())
      .stored_value->value.push_back(std::move(callback));
  CheckDone();
}

void InspectorResourceContentLoader::Cancel(int client_id) {
  callbacks_.erase(client_id);
}

InspectorResourceContentLoader::~InspectorResourceContentLoader() {
  DCHECK(resources_.IsEmpty());
}

void InspectorResourceContentLoader::Trace(Visitor* visitor) {
  visitor->Trace(inspected_frame_);
  visitor->Trace(pending_resource_clients_);
  visitor->Trace(resources_);
}

void InspectorResourceContentLoader::DidCommitLoadForLocalFrame(
    LocalFrame* frame) {
  if (frame == inspected_frame_)
    Stop();
}

Resource* InspectorResourceContentLoader::ResourceForURL(const KURL& url) {
  for (const auto& resource : resources_) {
    if (resource->Url() == url)
      return resource;
  }
  return nullptr;
}

void InspectorResourceContentLoader::Dispose() {
  Stop();
}

void InspectorResourceContentLoader::Stop() {
  HeapHashSet<Member<ResourceClient>> pending_resource_clients;
  pending_resource_clients_.swap(pending_resource_clients);
  for (const auto& client : pending_resource_clients)
    client->loader_ = nullptr;
  resources_.clear();
  // Make sure all callbacks are called to prevent infinite waiting time.
  CheckDone();
  all_requests_started_ = false;
  started_ = false;
}

bool InspectorResourceContentLoader::HasFinished() {
  return all_requests_started_ && pending_resource_clients_.size() == 0;
}

void InspectorResourceContentLoader::CheckDone() {
  if (!HasFinished())
    return;
  HashMap<int, Callbacks> callbacks;
  callbacks.swap(callbacks_);
  for (auto& key_value : callbacks) {
    for (auto& callback : key_value.value)
      std::move(callback).Run();
  }
}

void InspectorResourceContentLoader::ResourceFinished(ResourceClient* client) {
  pending_resource_clients_.erase(client);
  CheckDone();
}

}  // namespace blink