1 // Copyright (c) 2010 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 #include "chrome/browser/extensions/file_reader.h" 6 7 #include "base/file_util.h" 8 #include "base/message_loop.h" 9 #include "content/browser/browser_thread.h" 10 #include "chrome/common/extensions/extension_resource.h" 11 12 FileReader::FileReader(const ExtensionResource& resource, Callback* callback) 13 : resource_(resource), 14 callback_(callback), 15 origin_loop_(MessageLoop::current()) { 16 DCHECK(callback_); 17 } 18 19 void FileReader::Start() { 20 BrowserThread::PostTask( 21 BrowserThread::FILE, FROM_HERE, 22 NewRunnableMethod(this, &FileReader::ReadFileOnBackgroundThread)); 23 } 24 25 FileReader::~FileReader() {} 26 27 void FileReader::ReadFileOnBackgroundThread() { 28 std::string data; 29 bool success = file_util::ReadFileToString(resource_.GetFilePath(), &data); 30 origin_loop_->PostTask(FROM_HERE, NewRunnableMethod( 31 this, &FileReader::RunCallback, success, data)); 32 } 33 34 void FileReader::RunCallback(bool success, const std::string& data) { 35 callback_->Run(success, data); 36 delete callback_; 37 } 38