Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / search / local_files_ntp_source.cc
blobcb017d975a5e618650ac27595448b077d998f7c5
1 // Copyright 2013 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.
5 #include "chrome/browser/search/local_files_ntp_source.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/files/file_util.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/path_service.h"
14 #include "base/strings/string_util.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "chrome/common/url_constants.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/url_data_source.h"
20 namespace {
22 const char kBasePath[] = "chrome/browser/resources/local_ntp";
24 void CallbackWithLoadedResource(
25 const std::string& origin,
26 const content::URLDataSource::GotDataCallback& callback,
27 const std::string& content) {
28 std::string output = content;
29 if (!origin.empty())
30 base::ReplaceFirstSubstringAfterOffset(&output, 0, "{{ORIGIN}}", origin);
32 scoped_refptr<base::RefCountedString> response(
33 base::RefCountedString::TakeString(&output));
34 callback.Run(response.get());
37 // Read a file to a string and return.
38 std::string ReadFileAndReturn(const base::FilePath& path) {
39 std::string data;
40 // This call can fail, but it doesn't matter for our purposes. If it fails,
41 // we simply return an empty string.
42 base::ReadFileToString(path, &data);
43 return data;
46 } // namespace
48 namespace local_ntp {
50 void SendLocalFileResource(
51 const std::string& path,
52 const content::URLDataSource::GotDataCallback& callback) {
53 SendLocalFileResourceWithOrigin(path, std::string(), callback);
56 void SendLocalFileResourceWithOrigin(
57 const std::string& path,
58 const std::string& origin,
59 const content::URLDataSource::GotDataCallback& callback) {
60 base::FilePath fullpath;
61 PathService::Get(base::DIR_SOURCE_ROOT, &fullpath);
62 fullpath = fullpath.AppendASCII(kBasePath).AppendASCII(path);
63 base::PostTaskAndReplyWithResult(
64 content::BrowserThread::GetBlockingPool(), FROM_HERE,
65 base::Bind(&ReadFileAndReturn, fullpath),
66 base::Bind(&CallbackWithLoadedResource, origin, callback));
69 } // namespace local_ntp