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

Side by Side Diff: content/test/test_file_error_injector.h

Issue 9426029: Test file errors in downloads. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merged with trunk. Created 8 years, 9 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
« no previous file with comments | « content/public/browser/download_id.h ('k') | content/test/test_file_error_injector.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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.
4
5 #ifndef CONTENT_TEST_TEST_FILE_ERROR_INJECTOR_H_
6 #define CONTENT_TEST_TEST_FILE_ERROR_INJECTOR_H_
7 #pragma once
8
9 #include <map>
10 #include <string>
11
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "net/base/net_errors.h"
15
16 class GURL;
17
18 namespace content {
19
20 class DownloadId;
21 class DownloadFileWithErrorsFactory;
22
23 // Test helper for injecting errors into download file operations.
24 // All errors for a download must be injected before it starts.
25 // This class needs to be |RefCountedThreadSafe| because the implementation
26 // is referenced by other classes that live past the time when the user is
27 // nominally done with it. These classes are internal to content/.
28 //
29 // NOTE: No more than one download with the same URL can be in progress at
30 // the same time. You can have multiple simultaneous downloads as long as the
31 // URLs are different, as the URLs are used as keys to get information about
32 // the download.
33 //
34 // Example:
35 //
36 // FileErrorInfo a = { url1, ... };
37 // FileErrorInfo b = { url2, ... };
38 //
39 // scoped_refptr<TestFileErrorInjector> injector =
40 // TestFileErrorInjector::Create();
41 //
42 // injector->AddError(a);
43 // injector->AddError(b);
44 // injector->InjectErrors();
45 //
46 // download_manager->DownloadUrl(url1, ...);
47 // download_manager->DownloadUrl(url2, ...);
48 // ... wait for downloads to finish or get an injected error ...
49 class TestFileErrorInjector
50 : public base::RefCountedThreadSafe<TestFileErrorInjector> {
51 public:
52 enum FileOperationCode {
53 FILE_OPERATION_INITIALIZE,
54 FILE_OPERATION_WRITE,
55 FILE_OPERATION_RENAME
56 };
57
58 // Structure that encapsulates the information needed to inject a file error.
59 struct FileErrorInfo {
60 std::string url; // Full URL of the download. Identifies the download.
61 FileOperationCode code; // Operation to affect.
62 int operation_instance; // 0-based count of operation calls, for each code.
63 net::Error net_error; // Error to inject.
64 };
65
66 typedef std::map<std::string, FileErrorInfo> ErrorMap;
67
68 // Creates an instance. May only be called once.
69 // Lives until all callbacks (in the implementation) are complete and the
70 // creator goes out of scope.
71 static scoped_refptr<TestFileErrorInjector> Create();
72
73 // Adds an error.
74 // Must be called before |InjectErrors()| for a particular download file.
75 // It is an error to call |AddError()| more than once for the same file
76 // (URL), unless you call |ClearErrors()| in between them.
77 bool AddError(const FileErrorInfo& error_info);
78
79 // Clears all errors.
80 // Only affects files created after the next call to InjectErrors().
81 void ClearErrors();
82
83 // Injects the errors such that new download files will be affected.
84 // The download system must already be initialized before calling this.
85 // Multiple calls are allowed, but only useful if the errors have changed.
86 // Replaces the injected error list.
87 bool InjectErrors();
88
89 // Tells how many files are currently open.
90 size_t CurrentFileCount() const;
91
92 // Tells how many files have ever been open (since construction or the
93 // last call to |ClearFoundFiles()|).
94 size_t TotalFileCount() const;
95
96 // Returns whether or not a file matching |url| has been created.
97 bool HadFile(const GURL& url) const;
98
99 // Gets the download ID associated with the file matching |url|.
100 const content::DownloadId GetId(const GURL& url) const;
101
102 // Resets the found file list.
103 void ClearFoundFiles();
104
105 static std::string DebugString(FileOperationCode code);
106
107 private:
108 friend class base::RefCountedThreadSafe<TestFileErrorInjector>;
109
110 typedef std::map<GURL, content::DownloadId> FileMap;
111
112 TestFileErrorInjector();
113
114 virtual ~TestFileErrorInjector();
115
116 void AddFactory(scoped_ptr<DownloadFileWithErrorsFactory> factory);
117
118 void InjectErrorsOnFileThread(ErrorMap map,
119 DownloadFileWithErrorsFactory* factory);
120
121 // Callbacks from the download file, to record lifetimes.
122 // These may be called on any thread.
123 void RecordDownloadFileConstruction(const GURL& url, content::DownloadId id);
124 void RecordDownloadFileDestruction(const GURL& url);
125
126 // These run on the UI thread.
127 void DownloadFileCreated(GURL url, content::DownloadId id);
128 void DestroyingDownloadFile(GURL url);
129
130 // All the data is used on the UI thread.
131 // Our injected error list, mapped by URL. One per file.
132 ErrorMap injected_errors_;
133
134 // Keep track of active DownloadFiles.
135 FileMap files_;
136
137 // Keep track of found DownloadFiles.
138 FileMap found_files_;
139
140 // The factory we created. May outlive this class.
141 DownloadFileWithErrorsFactory* created_factory_;
142
143 DISALLOW_COPY_AND_ASSIGN(TestFileErrorInjector);
144 };
145
146 } // namespace content
147
148 #endif // CONTENT_TEST_TEST_FILE_ERROR_INJECTOR_H_
OLDNEW
« no previous file with comments | « content/public/browser/download_id.h ('k') | content/test/test_file_error_injector.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698