OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include "extensions/browser/web_ui_user_script_loader.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/pickle.h" |
| 9 #include "content/public/browser/browser_context.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "extensions/browser/content_verifier.h" |
| 12 |
| 13 namespace { |
| 14 using LoadScriptsCallback = |
| 15 base::Callback<void(scoped_ptr<extensions::UserScriptList>, |
| 16 scoped_ptr<base::SharedMemory>)>; |
| 17 |
| 18 void SerializeOnFileThread(scoped_ptr<extensions::UserScriptList> user_scripts, |
| 19 LoadScriptsCallback callback) { |
| 20 scoped_ptr<base::SharedMemory> memory = |
| 21 extensions::UserScriptLoader::Serialize(*user_scripts); |
| 22 content::BrowserThread::PostTask( |
| 23 content::BrowserThread::UI, FROM_HERE, |
| 24 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory))); |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 WebUIUserScriptLoader::WebUIUserScriptLoader( |
| 30 content::BrowserContext* browser_context, |
| 31 const HostID& host_id) |
| 32 : UserScriptLoader(browser_context, host_id), weak_factory_(this) { |
| 33 SetReady(true); |
| 34 } |
| 35 |
| 36 WebUIUserScriptLoader::~WebUIUserScriptLoader() { |
| 37 fetcher_groups_.clear(); |
| 38 } |
| 39 |
| 40 void WebUIUserScriptLoader::LoadScripts(const std::set<HostID>& changed_hosts, |
| 41 const std::set<int>& added_script_ids) { |
| 42 ClearScripts(false); |
| 43 scoped_ptr<extensions::UserScriptList> user_scripts(user_scripts_.Pass()); |
| 44 user_scripts_.reset(nullptr); |
| 45 |
| 46 std::set<extensions::UserScript::File*> file_set; |
| 47 bool find_render_info = false; |
| 48 UserScriptRenderInfo info; |
| 49 |
| 50 for (extensions::UserScriptList::iterator script = user_scripts->begin(); |
| 51 script != user_scripts->end(); ++script) { |
| 52 if (added_script_ids.count(script->id()) == 0) |
| 53 continue; |
| 54 |
| 55 if (!find_render_info) |
| 56 find_render_info = GetScriptRenderInfo(script->id(), &info); |
| 57 |
| 58 for (size_t k = 0; k < script->js_scripts().size(); ++k) { |
| 59 extensions::UserScript::File& script_file = script->js_scripts()[k]; |
| 60 if (script_file.GetContent().empty()) { |
| 61 file_set.insert(&script_file); |
| 62 } |
| 63 } |
| 64 for (size_t k = 0; k < script->css_scripts().size(); ++k) { |
| 65 extensions::UserScript::File& script_file = script->css_scripts()[k]; |
| 66 if (script_file.GetContent().empty()) { |
| 67 file_set.insert(&script_file); |
| 68 } |
| 69 } |
| 70 |
| 71 RemoveScriptRenderInfo(script->id()); |
| 72 } |
| 73 scoped_refptr<WebUIURLFetcherGroup> fetcher_group(new WebUIURLFetcherGroup( |
| 74 browser_context(), |
| 75 base::Bind(&WebUIUserScriptLoader::OnWebUIURLFetchComplete, |
| 76 weak_factory_.GetWeakPtr(), base::Passed(&user_scripts)))); |
| 77 |
| 78 fetcher_group->Start(file_set, info.render_process_id, info.render_view_id); |
| 79 fetcher_groups_.insert(fetcher_group.get()); |
| 80 } |
| 81 |
| 82 void WebUIUserScriptLoader::OnWebUIURLFetchComplete( |
| 83 scoped_ptr<extensions::UserScriptList> user_scripts) { |
| 84 content::BrowserThread::PostTask( |
| 85 content::BrowserThread::FILE, FROM_HERE, |
| 86 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts), |
| 87 base::Bind(&WebUIUserScriptLoader::OnScriptsLoaded, |
| 88 weak_factory_.GetWeakPtr()))); |
| 89 } |
OLD | NEW |