OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/public/test/test_file_error_injector.h" | 5 #include "content/public/test/test_file_error_injector.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 const content::TestFileErrorInjector::FileErrorInfo& error_info, | 48 const content::TestFileErrorInjector::FileErrorInfo& error_info, |
49 const ConstructionCallback& ctor_callback, | 49 const ConstructionCallback& ctor_callback, |
50 const DestructionCallback& dtor_callback); | 50 const DestructionCallback& dtor_callback); |
51 | 51 |
52 ~DownloadFileWithErrors(); | 52 ~DownloadFileWithErrors(); |
53 | 53 |
54 // DownloadFile interface. | 54 // DownloadFile interface. |
55 virtual content::DownloadInterruptReason Initialize() OVERRIDE; | 55 virtual content::DownloadInterruptReason Initialize() OVERRIDE; |
56 virtual content::DownloadInterruptReason AppendDataToFile( | 56 virtual content::DownloadInterruptReason AppendDataToFile( |
57 const char* data, size_t data_len) OVERRIDE; | 57 const char* data, size_t data_len) OVERRIDE; |
58 virtual void Rename(const FilePath& full_path, | 58 virtual content::DownloadInterruptReason Rename( |
59 bool overwrite_existing_file, | 59 const FilePath& full_path) OVERRIDE; |
60 const RenameCompletionCallback& callback) OVERRIDE; | |
61 | 60 |
62 private: | 61 private: |
63 // Error generating helper. | 62 // Error generating helper. |
64 content::DownloadInterruptReason ShouldReturnError( | 63 content::DownloadInterruptReason ShouldReturnError( |
65 content::TestFileErrorInjector::FileOperationCode code, | 64 content::TestFileErrorInjector::FileOperationCode code, |
66 content::DownloadInterruptReason original_error); | 65 content::DownloadInterruptReason original_error); |
67 | 66 |
68 // Used in place of original rename callback to intercept with | |
69 // ShouldReturnError. | |
70 void RenameErrorCallback( | |
71 const RenameCompletionCallback& original_callback, | |
72 content::DownloadInterruptReason original_error, | |
73 const FilePath& path_result); | |
74 | |
75 // Source URL for the file being downloaded. | 67 // Source URL for the file being downloaded. |
76 GURL source_url_; | 68 GURL source_url_; |
77 | 69 |
78 // Our injected error. Only one per file. | 70 // Our injected error. Only one per file. |
79 content::TestFileErrorInjector::FileErrorInfo error_info_; | 71 content::TestFileErrorInjector::FileErrorInfo error_info_; |
80 | 72 |
81 // Count per operation. 0-based. | 73 // Count per operation. 0-based. |
82 std::map<content::TestFileErrorInjector::FileOperationCode, int> | 74 std::map<content::TestFileErrorInjector::FileOperationCode, int> |
83 operation_counter_; | 75 operation_counter_; |
84 | 76 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 DownloadFileImpl::Initialize()); | 111 DownloadFileImpl::Initialize()); |
120 } | 112 } |
121 | 113 |
122 content::DownloadInterruptReason DownloadFileWithErrors::AppendDataToFile( | 114 content::DownloadInterruptReason DownloadFileWithErrors::AppendDataToFile( |
123 const char* data, size_t data_len) { | 115 const char* data, size_t data_len) { |
124 return ShouldReturnError( | 116 return ShouldReturnError( |
125 content::TestFileErrorInjector::FILE_OPERATION_WRITE, | 117 content::TestFileErrorInjector::FILE_OPERATION_WRITE, |
126 DownloadFileImpl::AppendDataToFile(data, data_len)); | 118 DownloadFileImpl::AppendDataToFile(data, data_len)); |
127 } | 119 } |
128 | 120 |
129 void DownloadFileWithErrors::Rename( | 121 content::DownloadInterruptReason DownloadFileWithErrors::Rename( |
130 const FilePath& full_path, | 122 const FilePath& full_path) { |
131 bool overwrite_existing_file, | 123 return ShouldReturnError( |
132 const RenameCompletionCallback& callback) { | 124 content::TestFileErrorInjector::FILE_OPERATION_RENAME, |
133 DownloadFileImpl::Rename( | 125 DownloadFileImpl::Rename(full_path)); |
134 full_path, overwrite_existing_file, | |
135 base::Bind(&DownloadFileWithErrors::RenameErrorCallback, | |
136 // Unretained since this'll only be called from | |
137 // the DownloadFileImpl slice of the same object. | |
138 base::Unretained(this), callback)); | |
139 } | 126 } |
140 | 127 |
141 content::DownloadInterruptReason DownloadFileWithErrors::ShouldReturnError( | 128 content::DownloadInterruptReason DownloadFileWithErrors::ShouldReturnError( |
142 content::TestFileErrorInjector::FileOperationCode code, | 129 content::TestFileErrorInjector::FileOperationCode code, |
143 content::DownloadInterruptReason original_error) { | 130 content::DownloadInterruptReason original_error) { |
144 int counter = operation_counter_[code]; | 131 int counter = operation_counter_[code]; |
145 ++operation_counter_[code]; | 132 ++operation_counter_[code]; |
146 | 133 |
147 if (code != error_info_.code) | 134 if (code != error_info_.code) |
148 return original_error; | 135 return original_error; |
149 | 136 |
150 if (counter != error_info_.operation_instance) | 137 if (counter != error_info_.operation_instance) |
151 return original_error; | 138 return original_error; |
152 | 139 |
153 VLOG(1) << " " << __FUNCTION__ << "()" | 140 VLOG(1) << " " << __FUNCTION__ << "()" |
154 << " url = '" << source_url_.spec() << "'" | 141 << " url = '" << source_url_.spec() << "'" |
155 << " code = " << content::TestFileErrorInjector::DebugString(code) | 142 << " code = " << content::TestFileErrorInjector::DebugString(code) |
156 << " (" << code << ")" | 143 << " (" << code << ")" |
157 << " counter = " << counter | 144 << " counter = " << counter |
158 << " original_error = " | 145 << " original_error = " |
159 << content::InterruptReasonDebugString(original_error) | 146 << content::InterruptReasonDebugString(original_error) |
160 << " (" << original_error << ")" | 147 << " (" << original_error << ")" |
161 << " new error = " | 148 << " new error = " |
162 << content::InterruptReasonDebugString(error_info_.error) | 149 << content::InterruptReasonDebugString(error_info_.error) |
163 << " (" << error_info_.error << ")"; | 150 << " (" << error_info_.error << ")"; |
164 | 151 |
165 return error_info_.error; | 152 return error_info_.error; |
166 } | 153 } |
167 | 154 |
168 void DownloadFileWithErrors::RenameErrorCallback( | |
169 const RenameCompletionCallback& original_callback, | |
170 content::DownloadInterruptReason original_error, | |
171 const FilePath& path_result) { | |
172 original_callback.Run(ShouldReturnError( | |
173 content::TestFileErrorInjector::FILE_OPERATION_RENAME, | |
174 original_error), path_result); | |
175 } | |
176 | |
177 } // namespace | 155 } // namespace |
178 | 156 |
179 namespace content { | 157 namespace content { |
180 | 158 |
181 // A factory for constructing DownloadFiles that inject errors. | 159 // A factory for constructing DownloadFiles that inject errors. |
182 class DownloadFileWithErrorsFactory | 160 class DownloadFileWithErrorsFactory |
183 : public DownloadFileManager::DownloadFileFactory { | 161 : public DownloadFileManager::DownloadFileFactory { |
184 public: | 162 public: |
185 | 163 |
186 DownloadFileWithErrorsFactory( | 164 DownloadFileWithErrorsFactory( |
187 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, | 165 const DownloadFileWithErrors::ConstructionCallback& ctor_callback, |
188 const DownloadFileWithErrors::DestructionCallback& dtor_callback); | 166 const DownloadFileWithErrors::DestructionCallback& dtor_callback); |
189 virtual ~DownloadFileWithErrorsFactory(); | 167 virtual ~DownloadFileWithErrorsFactory(); |
190 | 168 |
191 // DownloadFileFactory interface. | 169 // DownloadFileFactory interface. |
192 virtual DownloadFile* CreateFile( | 170 virtual content::DownloadFile* CreateFile( |
193 DownloadCreateInfo* info, | 171 DownloadCreateInfo* info, |
194 scoped_ptr<content::ByteStreamReader> stream, | 172 scoped_ptr<content::ByteStreamReader> stream, |
195 content::DownloadManager* download_manager, | 173 content::DownloadManager* download_manager, |
196 bool calculate_hash, | 174 bool calculate_hash, |
197 const net::BoundNetLog& bound_net_log); | 175 const net::BoundNetLog& bound_net_log); |
198 | 176 |
199 bool AddError( | 177 bool AddError( |
200 const TestFileErrorInjector::FileErrorInfo& error_info); | 178 const TestFileErrorInjector::FileErrorInfo& error_info); |
201 | 179 |
202 void ClearErrors(); | 180 void ClearErrors(); |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
448 case FILE_OPERATION_RENAME: | 426 case FILE_OPERATION_RENAME: |
449 return "RENAME"; | 427 return "RENAME"; |
450 default: | 428 default: |
451 break; | 429 break; |
452 } | 430 } |
453 | 431 |
454 return "Unknown"; | 432 return "Unknown"; |
455 } | 433 } |
456 | 434 |
457 } // namespace content | 435 } // namespace content |
OLD | NEW |