From 1c2db4c88504e33fe4c5ccfb4795e5a9b29de5ed Mon Sep 17 00:00:00 2001 From: fserb Date: Thu, 16 Jul 2015 10:14:49 -0700 Subject: Adds support for live reload localNTP assets Adds a new chrome flag to support local NTP file auto reload This is used only during NTP development cycle, to allow live reload of NTP resources (HTML, JS, CSS) without recompiling, therefore the code is only enabled for dev builds. BUG=508617 Review URL: https://codereview.chromium.org/1228123003 Cr-Commit-Position: refs/heads/master@{#339060} --- chrome/browser/search/local_files_ntp_source.cc | 69 +++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 chrome/browser/search/local_files_ntp_source.cc (limited to 'chrome/browser/search/local_files_ntp_source.cc') diff --git a/chrome/browser/search/local_files_ntp_source.cc b/chrome/browser/search/local_files_ntp_source.cc new file mode 100644 index 0000000..cb017d9 --- /dev/null +++ b/chrome/browser/search/local_files_ntp_source.cc @@ -0,0 +1,69 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/search/local_files_ntp_source.h" + +#include "base/bind.h" +#include "base/files/file_path.h" +#include "base/files/file_util.h" +#include "base/location.h" +#include "base/memory/ref_counted_memory.h" +#include "base/memory/scoped_ptr.h" +#include "base/path_service.h" +#include "base/strings/string_util.h" +#include "base/threading/thread_restrictions.h" +#include "chrome/common/url_constants.h" +#include "content/public/browser/browser_thread.h" +#include "content/public/browser/url_data_source.h" + +namespace { + +const char kBasePath[] = "chrome/browser/resources/local_ntp"; + +void CallbackWithLoadedResource( + const std::string& origin, + const content::URLDataSource::GotDataCallback& callback, + const std::string& content) { + std::string output = content; + if (!origin.empty()) + base::ReplaceFirstSubstringAfterOffset(&output, 0, "{{ORIGIN}}", origin); + + scoped_refptr response( + base::RefCountedString::TakeString(&output)); + callback.Run(response.get()); +} + +// Read a file to a string and return. +std::string ReadFileAndReturn(const base::FilePath& path) { + std::string data; + // This call can fail, but it doesn't matter for our purposes. If it fails, + // we simply return an empty string. + base::ReadFileToString(path, &data); + return data; +} + +} // namespace + +namespace local_ntp { + +void SendLocalFileResource( + const std::string& path, + const content::URLDataSource::GotDataCallback& callback) { + SendLocalFileResourceWithOrigin(path, std::string(), callback); +} + +void SendLocalFileResourceWithOrigin( + const std::string& path, + const std::string& origin, + const content::URLDataSource::GotDataCallback& callback) { + base::FilePath fullpath; + PathService::Get(base::DIR_SOURCE_ROOT, &fullpath); + fullpath = fullpath.AppendASCII(kBasePath).AppendASCII(path); + base::PostTaskAndReplyWithResult( + content::BrowserThread::GetBlockingPool(), FROM_HERE, + base::Bind(&ReadFileAndReturn, fullpath), + base::Bind(&CallbackWithLoadedResource, origin, callback)); +} + +} // namespace local_ntp -- cgit v1.1