file_reader.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2009 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 CHROME_BROWSER_EXTENSIONS_FILE_READER_H_
6#define CHROME_BROWSER_EXTENSIONS_FILE_READER_H_
7#pragma once
8
9#include <string>
10
11#include "base/callback.h"
12#include "base/ref_counted.h"
13#include "chrome/common/extensions/extension_resource.h"
14
15class MessageLoop;
16
17// This file defines an interface for reading a file asynchronously on a
18// background thread.
19// Consider abstracting out a FilePathProvider (ExtensionResource) and moving
20// back to chrome/browser/net if other subsystems want to use it.
21class FileReader : public base::RefCountedThreadSafe<FileReader> {
22 public:
23  // Reports success or failure and the data of the file upon success.
24  typedef Callback2<bool, const std::string&>::Type Callback;
25
26  FileReader(const ExtensionResource& resource, Callback* callback);
27
28  // Called to start reading the file on a background thread.  Upon completion,
29  // the callback will be notified of the results.
30  void Start();
31
32 private:
33  friend class base::RefCountedThreadSafe<FileReader>;
34
35  ~FileReader() {}
36
37  void ReadFileOnBackgroundThread();
38  void RunCallback(bool success, const std::string& data);
39
40  ExtensionResource resource_;
41  Callback* callback_;
42  MessageLoop* origin_loop_;
43};
44
45#endif  // CHROME_BROWSER_EXTENSIONS_FILE_READER_H_
46