Chromium Code Reviews| Index: extensions/browser/web_ui_user_script_loader.cc |
| 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 0000000000000000000000000000000000000000..16df945096163c7ff5365db2b62fd1fbcf31811a |
| --- /dev/null |
| +++ b/extensions/browser/web_ui_user_script_loader.cc |
| @@ -0,0 +1,128 @@ |
| +// 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/pickle.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "extensions/browser/content_verifier.h" |
| + |
| +namespace { |
| +using LoadScriptsCallback = |
| + base::Callback<void(scoped_ptr<extensions::UserScriptList>, |
| + scoped_ptr<base::SharedMemory>)>; |
| + |
| +void SerializeOnFileThread(scoped_ptr<extensions::UserScriptList> user_scripts, |
| + 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) {} |
| + |
| + bool operator<(const UserScriptRenderInfo& other) { |
| + if (render_process_id != other.render_process_id) |
| + return render_process_id < other.render_process_id; |
| + |
| + if (render_view_id != other.render_view_id) |
| + return render_view_id < other.render_view_id; |
| + |
| + return false; |
| + } |
| +}; |
| + |
| +WebUIUserScriptLoader::WebUIUserScriptLoader( |
| + content::BrowserContext* browser_context, |
| + const HostID& host_id) |
| + : UserScriptLoader(browser_context, host_id), weak_factory_(this) { |
| + SetReady(true); |
| +} |
| + |
| +WebUIUserScriptLoader::~WebUIUserScriptLoader() { |
| + fetcher_groups_.clear(); |
| +} |
| + |
| +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(const std::set<HostID>& changed_hosts, |
| + const std::set<int>& added_script_ids) { |
| + ClearScripts(false); |
| + scoped_ptr<extensions::UserScriptList> user_scripts(user_scripts_.Pass()); |
| + user_scripts_.reset(nullptr); |
| + |
| + std::set<extensions::UserScript::File*> file_set; |
| + bool find_render_info = false; |
| + UserScriptRenderInfo info; |
| + |
| + for (extensions::UserScript& script : *user_scripts) { |
| + if (added_script_ids.count(script.id()) == 0) |
| + continue; |
| + |
| + // Since every script in |added_script_ids| is associated with the same |
| + // render info, so we only need to look up the map once. |
| + if (!find_render_info) { |
| + auto iter = script_render_info_map_.find(script.id()); |
| + if (iter != script_render_info_map_.end()) { |
| + info.render_process_id = iter->second.render_process_id; |
| + info.render_view_id = iter->second.render_view_id; |
| + } |
| + find_render_info = true; |
| + } |
| + |
| + for (size_t k = 0; k < script.js_scripts().size(); ++k) { |
| + extensions::UserScript::File& script_file = script.js_scripts()[k]; |
| + if (script_file.GetContent().empty()) { |
| + file_set.insert(&script_file); |
| + } |
| + } |
| + for (size_t k = 0; k < script.css_scripts().size(); ++k) { |
| + extensions::UserScript::File& script_file = script.css_scripts()[k]; |
| + if (script_file.GetContent().empty()) { |
| + file_set.insert(&script_file); |
| + } |
| + } |
| + script_render_info_map_.erase(script.id()); |
| + } |
| + |
| + scoped_refptr<WebUIURLFetcherGroup> fetcher_group(new WebUIURLFetcherGroup( |
| + browser_context(), |
| + base::Bind(&WebUIUserScriptLoader::OnWebUIURLFetchComplete, |
| + weak_factory_.GetWeakPtr(), base::Passed(&user_scripts)))); |
| + |
| + fetcher_group->Start(file_set, info.render_process_id, info.render_view_id); |
|
Devlin
2015/04/17 22:05:05
In what circumstance will we ever have more than o
Xi Han
2015/04/17 22:28:24
So that means we can assume there is always only o
Devlin
2015/04/17 22:40:15
SGTM.
Xi Han
2015/04/20 22:40:10
Done.
|
| + fetcher_groups_.insert(fetcher_group.get()); |
| +} |
| + |
| +void WebUIUserScriptLoader::OnWebUIURLFetchComplete( |
| + scoped_ptr<extensions::UserScriptList> user_scripts) { |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::FILE, FROM_HERE, |
| + base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts), |
| + base::Bind(&WebUIUserScriptLoader::OnScriptsLoaded, |
| + weak_factory_.GetWeakPtr()))); |
| +} |