summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search
diff options
context:
space:
mode:
authorfserb <fserb@chromium.org>2015-07-16 10:14:49 -0700
committerCommit bot <commit-bot@chromium.org>2015-07-16 17:16:43 +0000
commit1c2db4c88504e33fe4c5ccfb4795e5a9b29de5ed (patch)
tree06f78c3f67870d5a29d023dd307cf86572a68bdb /chrome/browser/search
parent7355493a961bcc4d58df65b316d8860d5ecd3201 (diff)
downloadchromium_src-1c2db4c88504e33fe4c5ccfb4795e5a9b29de5ed.zip
chromium_src-1c2db4c88504e33fe4c5ccfb4795e5a9b29de5ed.tar.gz
chromium_src-1c2db4c88504e33fe4c5ccfb4795e5a9b29de5ed.tar.bz2
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}
Diffstat (limited to 'chrome/browser/search')
-rw-r--r--chrome/browser/search/local_files_ntp_source.cc69
-rw-r--r--chrome/browser/search/local_files_ntp_source.h29
-rw-r--r--chrome/browser/search/local_ntp_source.cc18
-rw-r--r--chrome/browser/search/most_visited_iframe_source.cc22
4 files changed, 137 insertions, 1 deletions
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<base::RefCountedString> 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
diff --git a/chrome/browser/search/local_files_ntp_source.h b/chrome/browser/search/local_files_ntp_source.h
new file mode 100644
index 0000000..aef34a3
--- /dev/null
+++ b/chrome/browser/search/local_files_ntp_source.h
@@ -0,0 +1,29 @@
+// Copyright 2015 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.
+
+#ifndef CHROME_BROWSER_SEARCH_LOCAL_FILES_NTP_SOURCE_H_
+#define CHROME_BROWSER_SEARCH_LOCAL_FILES_NTP_SOURCE_H_
+
+#include <string>
+#include "content/public/browser/url_data_source.h"
+
+namespace local_ntp {
+
+// Sends the content of |path| to |callback|, reading |path| as a local file.
+// This function is only used for dev builds.
+void SendLocalFileResource(
+ const std::string& path,
+ const content::URLDataSource::GotDataCallback& callback);
+
+// Sends the content of |path| to |callback|, reading |path| as a local file.
+// It also replaces the first occurrence of {{ORIGIN}} with |origin|.
+// This function is only used for dev builds.
+void SendLocalFileResourceWithOrigin(
+ const std::string& path,
+ const std::string& origin,
+ const content::URLDataSource::GotDataCallback& callback);
+
+} // namespace local_ntp
+
+#endif // CHROME_BROWSER_SEARCH_LOCAL_FILES_NTP_SOURCE_H_
diff --git a/chrome/browser/search/local_ntp_source.cc b/chrome/browser/search/local_ntp_source.cc
index 90d9fe7..fbc48d5 100644
--- a/chrome/browser/search/local_ntp_source.cc
+++ b/chrome/browser/search/local_ntp_source.cc
@@ -15,8 +15,10 @@
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/search/instant_io_context.h"
+#include "chrome/browser/search/local_files_ntp_source.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
+#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/search_engines/template_url_prepopulate_data.h"
@@ -179,17 +181,31 @@ void LocalNtpSource::StartDataRequest(
int render_process_id,
int render_frame_id,
const content::URLDataSource::GotDataCallback& callback) {
- const std::string stripped_path = StripParameters(path);
+ std::string stripped_path = StripParameters(path);
if (stripped_path == kConfigDataFilename) {
std::string config_data_js = GetConfigData(profile_);
callback.Run(base::RefCountedString::TakeString(&config_data_js));
return;
}
+
+#if !defined(OFFICIAL_BUILD)
+ base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
+ if (command_line->HasSwitch(switches::kLocalNtpReload)) {
+ if (stripped_path == "local-ntp.html" || stripped_path == "local-ntp.js" ||
+ stripped_path == "local-ntp.css") {
+ base::ReplaceChars(stripped_path, "-", "_", &stripped_path);
+ local_ntp::SendLocalFileResource(stripped_path, callback);
+ return;
+ }
+ }
+#endif
+
float scale = 1.0f;
std::string filename;
webui::ParsePathAndScale(
GURL(GetLocalNtpPath() + stripped_path), &filename, &scale);
ui::ScaleFactor scale_factor = ui::GetSupportedScaleFactor(scale);
+
for (size_t i = 0; i < arraysize(kResources); ++i) {
if (filename == kResources[i].filename) {
scoped_refptr<base::RefCountedStaticMemory> response(
diff --git a/chrome/browser/search/most_visited_iframe_source.cc b/chrome/browser/search/most_visited_iframe_source.cc
index 22236b8..126bec2 100644
--- a/chrome/browser/search/most_visited_iframe_source.cc
+++ b/chrome/browser/search/most_visited_iframe_source.cc
@@ -4,9 +4,12 @@
#include "chrome/browser/search/most_visited_iframe_source.h"
+#include "base/command_line.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
+#include "chrome/browser/search/local_files_ntp_source.h"
+#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/user_metrics.h"
#include "grit/browser_resources.h"
@@ -46,6 +49,25 @@ void MostVisitedIframeSource::StartDataRequest(
const content::URLDataSource::GotDataCallback& callback) {
GURL url(chrome::kChromeSearchMostVisitedUrl + path_and_query);
std::string path(url.path());
+
+#if !defined(OFFICIAL_BUILD)
+ base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
+ if (command_line->HasSwitch(switches::kLocalNtpReload)) {
+ std::string rel_path = "most_visited_" + path.substr(1);
+ if (path == kSingleJSPath) {
+ std::string origin;
+ if (!GetOrigin(render_process_id, render_frame_id, &origin)) {
+ callback.Run(NULL);
+ return;
+ }
+ local_ntp::SendLocalFileResourceWithOrigin(rel_path, origin, callback);
+ } else {
+ local_ntp::SendLocalFileResource(rel_path, callback);
+ }
+ return;
+ }
+#endif
+
if (path == kTitleHTMLPath) {
SendResource(IDR_MOST_VISITED_TITLE_HTML, callback);
} else if (path == kTitleCSSPath) {