blob: 236749c3663f643c60947a80365e3a91d7ba0fa7 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]1683aedf2009-09-29 23:06:132// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]1683aedf2009-09-29 23:06:134
[email protected]993da5e2013-03-23 21:25:165#ifndef EXTENSIONS_BROWSER_FILE_READER_H_
6#define EXTENSIONS_BROWSER_FILE_READER_H_
[email protected]1683aedf2009-09-29 23:06:137
8#include <string>
9
[email protected]1ec27eb52011-11-03 21:59:0910#include "base/callback.h"
[email protected]3b63f8f42011-03-28 01:54:1511#include "base/memory/ref_counted.h"
fdoray4cfe32a2016-06-29 19:45:5212#include "base/single_thread_task_runner.h"
[email protected]993da5e2013-03-23 21:25:1613#include "extensions/common/extension_resource.h"
[email protected]1683aedf2009-09-29 23:06:1314
[email protected]ecabe6ee2009-10-07 22:49:1015// This file defines an interface for reading a file asynchronously on a
Devlin Cronin06ba0812017-08-03 00:23:3316// background sequence.
[email protected]ecabe6ee2009-10-07 22:49:1017// Consider abstracting out a FilePathProvider (ExtensionResource) and moving
18// back to chrome/browser/net if other subsystems want to use it.
[email protected]1683aedf2009-09-29 23:06:1319class FileReader : public base::RefCountedThreadSafe<FileReader> {
20 public:
21 // Reports success or failure and the data of the file upon success.
Istiaque Ahmed8ceaa4d2018-02-16 20:00:4922 using DoneCallback =
23 base::OnceCallback<void(bool, std::unique_ptr<std::string>)>;
lazyboyb19fcfe2016-09-15 22:47:5724 // 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 Ahmed8ceaa4d2018-02-16 20:00:4927 using OptionalFileSequenceTask = base::OnceCallback<void(std::string*)>;
[email protected]1683aedf2009-09-29 23:06:1328
[email protected]993da5e2013-03-23 21:25:1629 FileReader(const extensions::ExtensionResource& resource,
Istiaque Ahmed8ceaa4d2018-02-16 20:00:4930 OptionalFileSequenceTask file_sequence_task,
31 DoneCallback done_callback);
[email protected]1683aedf2009-09-29 23:06:1332
Devlin Cronin06ba0812017-08-03 00:23:3333 // Called to start reading the file on a background sequence. Upon completion,
[email protected]1683aedf2009-09-29 23:06:1334 // the callback will be notified of the results.
35 void Start();
36
37 private:
[email protected]8de85a62009-11-06 08:32:1738 friend class base::RefCountedThreadSafe<FileReader>;
39
Devlin Cronin06ba0812017-08-03 00:23:3340 ~FileReader();
[email protected]8de85a62009-11-06 08:32:1741
Devlin Cronin06ba0812017-08-03 00:23:3342 void ReadFileOnFileSequence();
[email protected]1683aedf2009-09-29 23:06:1343
[email protected]993da5e2013-03-23 21:25:1644 extensions::ExtensionResource resource_;
Devlin Cronin06ba0812017-08-03 00:23:3345 OptionalFileSequenceTask optional_file_sequence_task_;
lazyboyb19fcfe2016-09-15 22:47:5746 DoneCallback done_callback_;
fdoray4cfe32a2016-06-29 19:45:5247 const scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
Devlin Cronin06ba0812017-08-03 00:23:3348
49 DISALLOW_COPY_AND_ASSIGN(FileReader);
[email protected]1683aedf2009-09-29 23:06:1350};
51
[email protected]993da5e2013-03-23 21:25:1652#endif // EXTENSIONS_BROWSER_FILE_READER_H_