Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1073)

Side by Side Diff: chrome/browser/dom_ui/chrome_url_data_manager_backend.cc

Issue 6461024: Revert 74292 - Splits ChromeURLDataManager into 2 chunks:... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 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 "chrome/browser/dom_ui/chrome_url_data_manager_backend.h"
6
7 #include "base/file_util.h"
8 #include "base/message_loop.h"
9 #include "base/path_service.h"
10 #include "base/ref_counted_memory.h"
11 #include "base/string_util.h"
12 #include "chrome/browser/appcache/view_appcache_internals_job_factory.h"
13 #include "chrome/browser/browser_thread.h"
14 #include "chrome/browser/dom_ui/shared_resources_data_source.h"
15 #include "chrome/browser/net/chrome_url_request_context.h"
16 #include "chrome/browser/net/view_blob_internals_job_factory.h"
17 #include "chrome/browser/net/view_http_cache_job_factory.h"
18 #include "chrome/common/chrome_paths.h"
19 #include "chrome/common/url_constants.h"
20 #include "googleurl/src/url_util.h"
21 #include "grit/platform_locale_settings.h"
22 #include "net/base/io_buffer.h"
23 #include "net/base/net_errors.h"
24 #include "net/url_request/url_request.h"
25 #include "net/url_request/url_request_file_job.h"
26 #include "net/url_request/url_request_job.h"
27
28 static ChromeURLDataManagerBackend* GetBackend(net::URLRequest* request) {
29 return static_cast<ChromeURLRequestContext*>(request->context())->
30 GetChromeURLDataManagerBackend();
31 }
32
33 // URLRequestChromeJob is a net::URLRequestJob that manages running
34 // chrome-internal resource requests asynchronously.
35 // It hands off URL requests to ChromeURLDataManager, which asynchronously
36 // calls back once the data is available.
37 class URLRequestChromeJob : public net::URLRequestJob {
38 public:
39 explicit URLRequestChromeJob(net::URLRequest* request);
40
41 // net::URLRequestJob implementation.
42 virtual void Start();
43 virtual void Kill();
44 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read);
45 virtual bool GetMimeType(std::string* mime_type) const;
46
47 // Called by ChromeURLDataManager to notify us that the data blob is ready
48 // for us.
49 void DataAvailable(RefCountedMemory* bytes);
50
51 void SetMimeType(const std::string& mime_type) {
52 mime_type_ = mime_type;
53 }
54
55 private:
56 virtual ~URLRequestChromeJob();
57
58 // Helper for Start(), to let us start asynchronously.
59 // (This pattern is shared by most net::URLRequestJob implementations.)
60 void StartAsync();
61
62 // Do the actual copy from data_ (the data we're serving) into |buf|.
63 // Separate from ReadRawData so we can handle async I/O.
64 void CompleteRead(net::IOBuffer* buf, int buf_size, int* bytes_read);
65
66 // The actual data we're serving. NULL until it's been fetched.
67 scoped_refptr<RefCountedMemory> data_;
68 // The current offset into the data that we're handing off to our
69 // callers via the Read interfaces.
70 int data_offset_;
71
72 // For async reads, we keep around a pointer to the buffer that
73 // we're reading into.
74 scoped_refptr<net::IOBuffer> pending_buf_;
75 int pending_buf_size_;
76 std::string mime_type_;
77
78 // The backend is owned by ChromeURLRequestContext and always outlives us.
79 ChromeURLDataManagerBackend* backend_;
80
81 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeJob);
82 };
83
84 // URLRequestChromeFileJob is a net::URLRequestJob that acts like a file:// URL
85 class URLRequestChromeFileJob : public net::URLRequestFileJob {
86 public:
87 URLRequestChromeFileJob(net::URLRequest* request, const FilePath& path);
88
89 private:
90 virtual ~URLRequestChromeFileJob();
91
92 DISALLOW_COPY_AND_ASSIGN(URLRequestChromeFileJob);
93 };
94
95 void ChromeURLDataManagerBackend::URLToRequest(const GURL& url,
96 std::string* source_name,
97 std::string* path) {
98 DCHECK(url.SchemeIs(chrome::kChromeDevToolsScheme) ||
99 url.SchemeIs(chrome::kChromeUIScheme));
100
101 if (!url.is_valid()) {
102 NOTREACHED();
103 return;
104 }
105
106 // Our input looks like: chrome://source_name/extra_bits?foo .
107 // So the url's "host" is our source, and everything after the host is
108 // the path.
109 source_name->assign(url.host());
110
111 const std::string& spec = url.possibly_invalid_spec();
112 const url_parse::Parsed& parsed = url.parsed_for_possibly_invalid_spec();
113 // + 1 to skip the slash at the beginning of the path.
114 int offset = parsed.CountCharactersBefore(url_parse::Parsed::PATH, false) + 1;
115
116 if (offset < static_cast<int>(spec.size()))
117 path->assign(spec.substr(offset));
118 }
119
120 bool ChromeURLDataManagerBackend::URLToFilePath(const GURL& url,
121 FilePath* file_path) {
122 // Parse the URL into a request for a source and path.
123 std::string source_name;
124 std::string relative_path;
125
126 // Remove Query and Ref from URL.
127 GURL stripped_url;
128 GURL::Replacements replacements;
129 replacements.ClearQuery();
130 replacements.ClearRef();
131 stripped_url = url.ReplaceComponents(replacements);
132
133 URLToRequest(stripped_url, &source_name, &relative_path);
134
135 FileSourceMap::const_iterator i(file_sources_.find(source_name));
136 if (i == file_sources_.end())
137 return false;
138
139 // Check that |relative_path| is not an absolute path (otherwise AppendASCII()
140 // will DCHECK). The awkward use of StringType is because on some systems
141 // FilePath expects a std::string, but on others a std::wstring.
142 FilePath p(FilePath::StringType(relative_path.begin(), relative_path.end()));
143 if (p.IsAbsolute())
144 return false;
145
146 *file_path = i->second.AppendASCII(relative_path);
147
148 return true;
149 }
150
151 ChromeURLDataManagerBackend::ChromeURLDataManagerBackend()
152 : next_request_id_(0) {
153 FilePath inspector_dir;
154 if (PathService::Get(chrome::DIR_INSPECTOR, &inspector_dir))
155 AddFileSource(chrome::kChromeUIDevToolsHost, inspector_dir);
156 AddDataSource(new SharedResourcesDataSource());
157 }
158
159 ChromeURLDataManagerBackend::~ChromeURLDataManagerBackend() {
160 for (DataSourceMap::iterator i = data_sources_.begin();
161 i != data_sources_.end(); ++i) {
162 i->second->backend_ = NULL;
163 }
164 data_sources_.clear();
165 }
166
167 // static
168 void ChromeURLDataManagerBackend::Register() {
169 net::URLRequest::RegisterProtocolFactory(
170 chrome::kChromeDevToolsScheme,
171 &ChromeURLDataManagerBackend::Factory);
172 net::URLRequest::RegisterProtocolFactory(
173 chrome::kChromeUIScheme,
174 &ChromeURLDataManagerBackend::Factory);
175 }
176
177 void ChromeURLDataManagerBackend::AddDataSource(
178 ChromeURLDataManager::DataSource* source) {
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
180 DataSourceMap::iterator i = data_sources_.find(source->source_name());
181 if (i != data_sources_.end())
182 i->second->backend_ = NULL;
183 data_sources_[source->source_name()] = source;
184 source->backend_ = this;
185 }
186
187 void ChromeURLDataManagerBackend::AddFileSource(const std::string& source_name,
188 const FilePath& file_path) {
189 DCHECK(file_sources_.count(source_name) == 0);
190 file_sources_[source_name] = file_path;
191 }
192
193 bool ChromeURLDataManagerBackend::HasPendingJob(
194 URLRequestChromeJob* job) const {
195 for (PendingRequestMap::const_iterator i = pending_requests_.begin();
196 i != pending_requests_.end(); ++i) {
197 if (i->second == job)
198 return true;
199 }
200 return false;
201 }
202
203 bool ChromeURLDataManagerBackend::StartRequest(const GURL& url,
204 URLRequestChromeJob* job) {
205 // Parse the URL into a request for a source and path.
206 std::string source_name;
207 std::string path;
208 URLToRequest(url, &source_name, &path);
209
210 // Look up the data source for the request.
211 DataSourceMap::iterator i = data_sources_.find(source_name);
212 if (i == data_sources_.end())
213 return false;
214
215 ChromeURLDataManager::DataSource* source = i->second;
216
217 // Save this request so we know where to send the data.
218 RequestID request_id = next_request_id_++;
219 pending_requests_.insert(std::make_pair(request_id, job));
220
221 // TODO(eroman): would be nicer if the mimetype were set at the same time
222 // as the data blob. For now do it here, since NotifyHeadersComplete() is
223 // going to get called once we return.
224 job->SetMimeType(source->GetMimeType(path));
225
226 ChromeURLRequestContext* context = static_cast<ChromeURLRequestContext*>(
227 job->request()->context());
228
229 // Forward along the request to the data source.
230 MessageLoop* target_message_loop = source->MessageLoopForRequestPath(path);
231 if (!target_message_loop) {
232 // The DataSource is agnostic to which thread StartDataRequest is called
233 // on for this path. Call directly into it from this thread, the IO
234 // thread.
235 source->StartDataRequest(path, context->is_off_the_record(), request_id);
236 } else {
237 // The DataSource wants StartDataRequest to be called on a specific thread,
238 // usually the UI thread, for this path.
239 target_message_loop->PostTask(
240 FROM_HERE,
241 NewRunnableMethod(source,
242 &ChromeURLDataManager::DataSource::StartDataRequest,
243 path, context->is_off_the_record(), request_id));
244 }
245 return true;
246 }
247
248 void ChromeURLDataManagerBackend::RemoveRequest(URLRequestChromeJob* job) {
249 // Remove the request from our list of pending requests.
250 // If/when the source sends the data that was requested, the data will just
251 // be thrown away.
252 for (PendingRequestMap::iterator i = pending_requests_.begin();
253 i != pending_requests_.end(); ++i) {
254 if (i->second == job) {
255 pending_requests_.erase(i);
256 return;
257 }
258 }
259 }
260
261 void ChromeURLDataManagerBackend::DataAvailable(RequestID request_id,
262 RefCountedMemory* bytes) {
263 // Forward this data on to the pending net::URLRequest, if it exists.
264 PendingRequestMap::iterator i = pending_requests_.find(request_id);
265 if (i != pending_requests_.end()) {
266 // We acquire a reference to the job so that it doesn't disappear under the
267 // feet of any method invoked here (we could trigger a callback).
268 scoped_refptr<URLRequestChromeJob> job(i->second);
269 pending_requests_.erase(i);
270 job->DataAvailable(bytes);
271 }
272 }
273
274 // static
275 net::URLRequestJob* ChromeURLDataManagerBackend::Factory(
276 net::URLRequest* request,
277 const std::string& scheme) {
278 // Try first with a file handler
279 FilePath path;
280 ChromeURLDataManagerBackend* backend = GetBackend(request);
281 if (backend->URLToFilePath(request->url(), &path))
282 return new URLRequestChromeFileJob(request, path);
283
284 // Next check for chrome://view-http-cache/*, which uses its own job type.
285 if (ViewHttpCacheJobFactory::IsSupportedURL(request->url()))
286 return ViewHttpCacheJobFactory::CreateJobForRequest(request);
287
288 // Next check for chrome://appcache-internals/, which uses its own job type.
289 if (ViewAppCacheInternalsJobFactory::IsSupportedURL(request->url()))
290 return ViewAppCacheInternalsJobFactory::CreateJobForRequest(request);
291
292 // Next check for chrome://blob-internals/, which uses its own job type.
293 if (ViewBlobInternalsJobFactory::IsSupportedURL(request->url()))
294 return ViewBlobInternalsJobFactory::CreateJobForRequest(request);
295
296 // Fall back to using a custom handler
297 return new URLRequestChromeJob(request);
298 }
299
300 URLRequestChromeJob::URLRequestChromeJob(net::URLRequest* request)
301 : net::URLRequestJob(request),
302 data_offset_(0),
303 pending_buf_size_(0),
304 backend_(GetBackend(request)) {
305 }
306
307 URLRequestChromeJob::~URLRequestChromeJob() {
308 CHECK(!backend_->HasPendingJob(this));
309 }
310
311 void URLRequestChromeJob::Start() {
312 // Start reading asynchronously so that all error reporting and data
313 // callbacks happen as they would for network requests.
314 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod(
315 this, &URLRequestChromeJob::StartAsync));
316 }
317
318 void URLRequestChromeJob::Kill() {
319 backend_->RemoveRequest(this);
320 }
321
322 bool URLRequestChromeJob::GetMimeType(std::string* mime_type) const {
323 *mime_type = mime_type_;
324 return !mime_type_.empty();
325 }
326
327 void URLRequestChromeJob::DataAvailable(RefCountedMemory* bytes) {
328 if (bytes) {
329 // The request completed, and we have all the data.
330 // Clear any IO pending status.
331 SetStatus(net::URLRequestStatus());
332
333 data_ = bytes;
334 int bytes_read;
335 if (pending_buf_.get()) {
336 CHECK(pending_buf_->data());
337 CompleteRead(pending_buf_, pending_buf_size_, &bytes_read);
338 pending_buf_ = NULL;
339 NotifyReadComplete(bytes_read);
340 }
341 } else {
342 // The request failed.
343 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED,
344 net::ERR_FAILED));
345 }
346 }
347
348 bool URLRequestChromeJob::ReadRawData(net::IOBuffer* buf, int buf_size,
349 int* bytes_read) {
350 if (!data_.get()) {
351 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
352 DCHECK(!pending_buf_.get());
353 CHECK(buf->data());
354 pending_buf_ = buf;
355 pending_buf_size_ = buf_size;
356 return false; // Tell the caller we're still waiting for data.
357 }
358
359 // Otherwise, the data is available.
360 CompleteRead(buf, buf_size, bytes_read);
361 return true;
362 }
363
364 void URLRequestChromeJob::CompleteRead(net::IOBuffer* buf, int buf_size,
365 int* bytes_read) {
366 int remaining = static_cast<int>(data_->size()) - data_offset_;
367 if (buf_size > remaining)
368 buf_size = remaining;
369 if (buf_size > 0) {
370 memcpy(buf->data(), data_->front() + data_offset_, buf_size);
371 data_offset_ += buf_size;
372 }
373 *bytes_read = buf_size;
374 }
375
376 void URLRequestChromeJob::StartAsync() {
377 if (!request_)
378 return;
379
380 if (backend_->StartRequest(request_->url(), this)) {
381 NotifyHeadersComplete();
382 } else {
383 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
384 net::ERR_INVALID_URL));
385 }
386 }
387
388 URLRequestChromeFileJob::URLRequestChromeFileJob(net::URLRequest* request,
389 const FilePath& path)
390 : net::URLRequestFileJob(request, path) {
391 }
392
393 URLRequestChromeFileJob::~URLRequestChromeFileJob() {}
OLDNEW
« no previous file with comments | « chrome/browser/dom_ui/chrome_url_data_manager_backend.h ('k') | chrome/browser/dom_ui/conflicts_ui.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698