Add entry to histograms.xml for OverlyLargeOriginLength.
[chromium-blink-merge.git] / webkit / browser / fileapi / file_system_dir_url_request_job.cc
blob7589b3732a0b6b704e927caea3934571b63f59ec
1 // Copyright (c) 2012 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.
5 #include "webkit/browser/fileapi/file_system_dir_url_request_job.h"
7 #include <algorithm>
9 #include "base/bind.h"
10 #include "base/compiler_specific.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/platform_file.h"
13 #include "base/strings/sys_string_conversions.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/time/time.h"
16 #include "build/build_config.h"
17 #include "net/base/io_buffer.h"
18 #include "net/base/net_errors.h"
19 #include "net/base/net_util.h"
20 #include "net/url_request/url_request.h"
21 #include "url/gurl.h"
22 #include "webkit/browser/fileapi/file_system_context.h"
23 #include "webkit/browser/fileapi/file_system_operation_runner.h"
24 #include "webkit/browser/fileapi/file_system_url.h"
25 #include "webkit/common/fileapi/directory_entry.h"
26 #include "webkit/common/fileapi/file_system_util.h"
28 using net::NetworkDelegate;
29 using net::URLRequest;
30 using net::URLRequestJob;
31 using net::URLRequestStatus;
33 namespace fileapi {
35 FileSystemDirURLRequestJob::FileSystemDirURLRequestJob(
36 URLRequest* request,
37 NetworkDelegate* network_delegate,
38 FileSystemContext* file_system_context)
39 : URLRequestJob(request, network_delegate),
40 file_system_context_(file_system_context),
41 weak_factory_(this) {
44 FileSystemDirURLRequestJob::~FileSystemDirURLRequestJob() {
47 bool FileSystemDirURLRequestJob::ReadRawData(net::IOBuffer* dest, int dest_size,
48 int *bytes_read) {
49 int count = std::min(dest_size, static_cast<int>(data_.size()));
50 if (count > 0) {
51 memcpy(dest->data(), data_.data(), count);
52 data_.erase(0, count);
54 *bytes_read = count;
55 return true;
58 void FileSystemDirURLRequestJob::Start() {
59 base::MessageLoop::current()->PostTask(
60 FROM_HERE,
61 base::Bind(&FileSystemDirURLRequestJob::StartAsync,
62 weak_factory_.GetWeakPtr()));
65 void FileSystemDirURLRequestJob::Kill() {
66 URLRequestJob::Kill();
67 weak_factory_.InvalidateWeakPtrs();
70 bool FileSystemDirURLRequestJob::GetMimeType(std::string* mime_type) const {
71 *mime_type = "text/html";
72 return true;
75 bool FileSystemDirURLRequestJob::GetCharset(std::string* charset) {
76 *charset = "utf-8";
77 return true;
80 void FileSystemDirURLRequestJob::StartAsync() {
81 if (!request_)
82 return;
83 url_ = file_system_context_->CrackURL(request_->url());
84 if (!file_system_context_->CanServeURLRequest(url_)) {
85 // In incognito mode the API is not usable and there should be no data.
86 if (url_.is_valid() && VirtualPath::IsRootPath(url_.virtual_path())) {
87 // Return an empty directory if the filesystem root is queried.
88 DidReadDirectory(base::PLATFORM_FILE_OK,
89 std::vector<DirectoryEntry>(),
90 false);
91 return;
93 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
94 net::ERR_FILE_NOT_FOUND));
95 return;
97 file_system_context_->operation_runner()->ReadDirectory(
98 url_,
99 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this));
102 void FileSystemDirURLRequestJob::DidReadDirectory(
103 base::PlatformFileError result,
104 const std::vector<DirectoryEntry>& entries,
105 bool has_more) {
106 if (result != base::PLATFORM_FILE_OK) {
107 int rv = net::ERR_FILE_NOT_FOUND;
108 if (result == base::PLATFORM_FILE_ERROR_INVALID_URL)
109 rv = net::ERR_INVALID_URL;
110 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, rv));
111 return;
114 if (!request_)
115 return;
117 if (data_.empty()) {
118 base::FilePath relative_path = url_.path();
119 #if defined(OS_POSIX)
120 relative_path =
121 base::FilePath(FILE_PATH_LITERAL("/") + relative_path.value());
122 #endif
123 const base::string16& title = relative_path.LossyDisplayName();
124 data_.append(net::GetDirectoryListingHeader(title));
127 typedef std::vector<DirectoryEntry>::const_iterator EntryIterator;
128 for (EntryIterator it = entries.begin(); it != entries.end(); ++it) {
129 const base::string16& name = base::FilePath(it->name).LossyDisplayName();
130 data_.append(net::GetDirectoryListingEntry(
131 name, std::string(), it->is_directory, it->size,
132 it->last_modified_time));
135 if (has_more) {
136 file_system_context_->operation_runner()->ReadDirectory(
137 url_,
138 base::Bind(&FileSystemDirURLRequestJob::DidReadDirectory, this));
139 } else {
140 set_expected_content_size(data_.size());
141 NotifyHeadersComplete();
145 } // namespace fileapi