summaryrefslogtreecommitdiffstats
path: root/extensions/browser/web_ui_user_script_loader.cc
diff options
context:
space:
mode:
authorhanxi <hanxi@chromium.org>2015-04-24 12:21:40 -0700
committerCommit bot <commit-bot@chromium.org>2015-04-24 19:21:56 +0000
commitfeb6a64f153422f71952ec3a7cd4fc3c1a90e272 (patch)
treec76f11036f15c16c49895f37991828799982449f /extensions/browser/web_ui_user_script_loader.cc
parent79a9a66e3152beef0545632aa30315456f2ebe74 (diff)
downloadchromium_src-feb6a64f153422f71952ec3a7cd4fc3c1a90e272.zip
chromium_src-feb6a64f153422f71952ec3a7cd4fc3c1a90e272.tar.gz
chromium_src-feb6a64f153422f71952ec3a7cd4fc3c1a90e272.tar.bz2
Implement <webview>.addContentScript/removeContentScript API [2]
This patch includes the changes that enables <webview>.addContentScript/removeContentScript API work on WebUI. -- Introduce WebUIURLFetcherGroup to manage a set of WebUIURLFetcher and response after all fetches are done. -- Move WebUIURLFetcher from web_view_internal_api.cc to a separate file. -- A refactoring is done in UserScriptLoader and ExtensionUserScriptLoader. Since webUI has different ways to load user script as extension does, so I revert some old refactoring in ExtensionUserScriptLoader and move all the loading related code from UserScriptLoader to ExtensionUserScriptLoader. This is the second patch in a series of patches: 1) Implement <webview>.addContentScript/removeContentScript API [1] (https://codereview.chromium.org/959413003) 2) Implement <webview>.addContentScript/removeContentScript API [2] (https://codereview.chromium.org/1056533002) 3) Implement <webview>.addContentScript/removeContentScript API [3] (https://codereview.chromium.org/1058113002) BUG=461052 Review URL: https://codereview.chromium.org/1056533002 Cr-Commit-Position: refs/heads/master@{#326854}
Diffstat (limited to 'extensions/browser/web_ui_user_script_loader.cc')
-rw-r--r--extensions/browser/web_ui_user_script_loader.cc145
1 files changed, 145 insertions, 0 deletions
diff --git a/extensions/browser/web_ui_user_script_loader.cc b/extensions/browser/web_ui_user_script_loader.cc
new file mode 100644
index 0000000..817834b
--- /dev/null
+++ b/extensions/browser/web_ui_user_script_loader.cc
@@ -0,0 +1,145 @@
+// 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.
+
+#include "extensions/browser/web_ui_user_script_loader.h"
+
+#include "base/bind.h"
+#include "base/strings/string_util.h"
+#include "content/public/browser/browser_context.h"
+#include "content/public/browser/browser_thread.h"
+#include "extensions/browser/guest_view/web_view/web_ui/web_ui_url_fetcher.h"
+
+namespace {
+
+void SerializeOnFileThread(
+ scoped_ptr<extensions::UserScriptList> user_scripts,
+ extensions::UserScriptLoader::LoadScriptsCallback callback) {
+ scoped_ptr<base::SharedMemory> memory =
+ extensions::UserScriptLoader::Serialize(*user_scripts);
+ content::BrowserThread::PostTask(
+ content::BrowserThread::UI, FROM_HERE,
+ base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory)));
+}
+
+} // namespace
+
+struct WebUIUserScriptLoader::UserScriptRenderInfo {
+ int render_process_id;
+ int render_view_id;
+
+ UserScriptRenderInfo() : render_process_id(-1), render_view_id(-1) {}
+
+ UserScriptRenderInfo(int render_process_id, int render_view_id)
+ : render_process_id(render_process_id), render_view_id(render_view_id) {}
+};
+
+WebUIUserScriptLoader::WebUIUserScriptLoader(
+ content::BrowserContext* browser_context,
+ const HostID& host_id)
+ : UserScriptLoader(browser_context, host_id), complete_fetchers_(0) {
+ SetReady(true);
+}
+
+WebUIUserScriptLoader::~WebUIUserScriptLoader() {
+}
+
+void WebUIUserScriptLoader::AddScripts(
+ const std::set<extensions::UserScript>& scripts,
+ int render_process_id,
+ int render_view_id) {
+ UserScriptRenderInfo info(render_process_id, render_view_id);
+ for (const extensions::UserScript& script : scripts) {
+ script_render_info_map_.insert(
+ std::pair<int, UserScriptRenderInfo>(script.id(), info));
+ }
+
+ extensions::UserScriptLoader::AddScripts(scripts);
+}
+
+void WebUIUserScriptLoader::LoadScripts(
+ scoped_ptr<extensions::UserScriptList> user_scripts,
+ const std::set<HostID>& changed_hosts,
+ const std::set<int>& added_script_ids,
+ LoadScriptsCallback callback) {
+ user_scripts_cache_.swap(user_scripts);
+ scripts_loaded_callback_ = callback;
+
+ // The total number of the tasks is used to trace whether all the fetches
+ // are complete. Therefore, we store all the fetcher pointers in |fetchers_|
+ // before we get theis number. Once we get the total number, start each
+ // fetch tasks.
+ DCHECK_EQ(0u, complete_fetchers_);
+
+ for (extensions::UserScript& script : *user_scripts_cache_) {
+ if (added_script_ids.count(script.id()) == 0)
+ continue;
+
+ auto iter = script_render_info_map_.find(script.id());
+ DCHECK(iter != script_render_info_map_.end());
+ int render_process_id = iter->second.render_process_id;
+ int render_view_id = iter->second.render_view_id;
+
+ CreateWebUIURLFetchers(&script.js_scripts(), render_process_id,
+ render_view_id);
+ CreateWebUIURLFetchers(&script.css_scripts(), render_process_id,
+ render_view_id);
+
+ script_render_info_map_.erase(script.id());
+ }
+
+ // If no fetch is needed, call OnWebUIURLFetchComplete directly.
+ if (fetchers_.empty()) {
+ OnWebUIURLFetchComplete();
+ return;
+ }
+ for (auto fetcher : fetchers_)
+ fetcher->Start();
+}
+
+void WebUIUserScriptLoader::CreateWebUIURLFetchers(
+ extensions::UserScript::FileList* script_files,
+ int render_process_id,
+ int render_view_id) {
+ for (extensions::UserScript::File& file : *script_files) {
+ if (file.GetContent().empty()) {
+ // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the
+ // loader is destroyed, all the fetchers will be destroyed. Therefore,
+ // we are sure it is safe to use base::Unretained(this) here.
+ scoped_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher(
+ browser_context(), render_process_id, render_view_id, file.url(),
+ base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete,
+ base::Unretained(this), &file)));
+ fetchers_.push_back(fetcher.release());
+ }
+ }
+}
+
+void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete(
+ extensions::UserScript::File* script_file,
+ bool success,
+ const std::string& data) {
+ if (success) {
+ // Remove BOM from the content.
+ std::string::size_type index = data.find(base::kUtf8ByteOrderMark);
+ if (index == 0)
+ script_file->set_content(data.substr(strlen(base::kUtf8ByteOrderMark)));
+ else
+ script_file->set_content(data);
+ }
+
+ ++complete_fetchers_;
+ if (complete_fetchers_ == fetchers_.size()) {
+ complete_fetchers_ = 0;
+ OnWebUIURLFetchComplete();
+ fetchers_.clear();
+ }
+}
+
+void WebUIUserScriptLoader::OnWebUIURLFetchComplete() {
+ content::BrowserThread::PostTask(
+ content::BrowserThread::FILE, FROM_HERE,
+ base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_),
+ scripts_loaded_callback_));
+ scripts_loaded_callback_.Reset();
+}