[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 4 | |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 5 | #ifndef EXTENSIONS_BROWSER_FILE_READER_H_ |
6 | #define EXTENSIONS_BROWSER_FILE_READER_H_ | ||||
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 7 | |
8 | #include <string> | ||||
9 | |||||
[email protected] | 1ec27eb5 | 2011-11-03 21:59:09 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
fdoray | 4cfe32a | 2016-06-29 19:45:52 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 13 | #include "extensions/common/extension_resource.h" |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 14 | |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 15 | // This file defines an interface for reading a file asynchronously on a |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 16 | // background sequence. |
[email protected] | ecabe6ee | 2009-10-07 22:49:10 | [diff] [blame] | 17 | // Consider abstracting out a FilePathProvider (ExtensionResource) and moving |
18 | // back to chrome/browser/net if other subsystems want to use it. | ||||
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 19 | class FileReader : public base::RefCountedThreadSafe<FileReader> { |
20 | public: | ||||
21 | // Reports success or failure and the data of the file upon success. | ||||
Istiaque Ahmed | 8ceaa4d | 2018-02-16 20:00:49 | [diff] [blame] | 22 | using DoneCallback = |
23 | base::OnceCallback<void(bool, std::unique_ptr<std::string>)>; | ||||
lazyboy | b19fcfe | 2016-09-15 22:47:57 | [diff] [blame] | 24 | // Lets the caller accomplish tasks on the file data, after the file content |
25 | // has been read. | ||||
26 | // If the file reading doesn't succeed, this will be ignored. | ||||
Istiaque Ahmed | 8ceaa4d | 2018-02-16 20:00:49 | [diff] [blame] | 27 | using OptionalFileSequenceTask = base::OnceCallback<void(std::string*)>; |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 28 | |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 29 | FileReader(const extensions::ExtensionResource& resource, |
Istiaque Ahmed | 8ceaa4d | 2018-02-16 20:00:49 | [diff] [blame] | 30 | OptionalFileSequenceTask file_sequence_task, |
31 | DoneCallback done_callback); | ||||
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 32 | |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 33 | // Called to start reading the file on a background sequence. Upon completion, |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 34 | // the callback will be notified of the results. |
35 | void Start(); | ||||
36 | |||||
37 | private: | ||||
[email protected] | 8de85a6 | 2009-11-06 08:32:17 | [diff] [blame] | 38 | friend class base::RefCountedThreadSafe<FileReader>; |
39 | |||||
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 40 | ~FileReader(); |
[email protected] | 8de85a6 | 2009-11-06 08:32:17 | [diff] [blame] | 41 | |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 42 | void ReadFileOnFileSequence(); |
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 43 | |
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 44 | extensions::ExtensionResource resource_; |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 45 | OptionalFileSequenceTask optional_file_sequence_task_; |
lazyboy | b19fcfe | 2016-09-15 22:47:57 | [diff] [blame] | 46 | DoneCallback done_callback_; |
fdoray | 4cfe32a | 2016-06-29 19:45:52 | [diff] [blame] | 47 | const scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
Devlin Cronin | 06ba081 | 2017-08-03 00:23:33 | [diff] [blame] | 48 | |
49 | DISALLOW_COPY_AND_ASSIGN(FileReader); | ||||
[email protected] | 1683aedf | 2009-09-29 23:06:13 | [diff] [blame] | 50 | }; |
51 | |||||
[email protected] | 993da5e | 2013-03-23 21:25:16 | [diff] [blame] | 52 | #endif // EXTENSIONS_BROWSER_FILE_READER_H_ |