Chromium Code Reviews| 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 "base/strings/string_util.h" | |
| 10 #include "content/public/browser/browser_context.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 #include "extensions/browser/content_verifier.h" | |
| 13 | |
| 14 namespace { | |
| 15 using LoadScriptsCallback = | |
| 16 base::Callback<void(scoped_ptr<extensions::UserScriptList>, | |
| 17 scoped_ptr<base::SharedMemory>)>; | |
| 18 | |
| 19 void SerializeOnFileThread(scoped_ptr<extensions::UserScriptList> user_scripts, | |
| 20 LoadScriptsCallback callback) { | |
| 21 scoped_ptr<base::SharedMemory> memory = | |
| 22 extensions::UserScriptLoader::Serialize(*user_scripts); | |
| 23 content::BrowserThread::PostTask( | |
| 24 content::BrowserThread::UI, FROM_HERE, | |
| 25 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory))); | |
| 26 } | |
| 27 | |
| 28 } // namespace | |
| 29 | |
| 30 struct WebUIUserScriptLoader::UserScriptRenderInfo { | |
| 31 int render_process_id; | |
| 32 int render_view_id; | |
| 33 | |
| 34 UserScriptRenderInfo() : render_process_id(-1), render_view_id(-1) {} | |
| 35 | |
| 36 UserScriptRenderInfo(int render_process_id, int render_view_id) | |
| 37 : render_process_id(render_process_id), render_view_id(render_view_id) {} | |
| 38 | |
| 39 bool operator<(const UserScriptRenderInfo& other) { | |
| 40 if (render_process_id != other.render_process_id) | |
| 41 return render_process_id < other.render_process_id; | |
| 42 | |
| 43 if (render_view_id != other.render_view_id) | |
| 44 return render_view_id < other.render_view_id; | |
| 45 | |
| 46 return false; | |
| 47 } | |
| 48 }; | |
| 49 | |
| 50 WebUIUserScriptLoader::WebUIUserScriptLoader( | |
| 51 content::BrowserContext* browser_context, | |
| 52 const HostID& host_id) | |
| 53 : UserScriptLoader(browser_context, host_id), weak_factory_(this) { | |
| 54 SetReady(true); | |
| 55 } | |
| 56 | |
| 57 WebUIUserScriptLoader::~WebUIUserScriptLoader() { | |
| 58 } | |
| 59 | |
| 60 void WebUIUserScriptLoader::AddScripts( | |
| 61 const std::set<extensions::UserScript>& scripts, | |
| 62 int render_process_id, | |
| 63 int render_view_id) { | |
| 64 UserScriptRenderInfo info(render_process_id, render_view_id); | |
| 65 for (const extensions::UserScript& script : scripts) | |
| 66 script_render_info_map_.insert( | |
| 67 std::pair<int, UserScriptRenderInfo>(script.id(), info)); | |
| 68 | |
| 69 extensions::UserScriptLoader::AddScripts(scripts); | |
| 70 } | |
| 71 | |
| 72 void WebUIUserScriptLoader::LoadScripts(const std::set<HostID>& changed_hosts, | |
|
Devlin
2015/04/20 23:48:25
See comment in other class about changing this met
Xi Han
2015/04/21 21:18:58
Done.
| |
| 73 const std::set<int>& added_script_ids) { | |
| 74 ClearScripts(false); | |
| 75 | |
| 76 std::set<extensions::UserScript::File*> file_set; | |
|
Devlin
2015/04/20 23:48:25
unless you need the uniqueness that a set offers,
Xi Han
2015/04/21 21:18:58
Done.
| |
| 77 bool find_render_info = false; | |
| 78 UserScriptRenderInfo info; | |
| 79 | |
| 80 for (extensions::UserScript& script : *user_scripts_) { | |
| 81 if (added_script_ids.count(script.id()) == 0) | |
| 82 continue; | |
| 83 | |
| 84 // Since every script in |added_script_ids| is associated with the same | |
|
Devlin
2015/04/20 23:48:25
If every script here is guaranteed to be associate
Xi Han
2015/04/21 21:18:58
Updated the comment here. After reconsidering it,
| |
| 85 // render info, so we only need to look up the map once. | |
| 86 if (!find_render_info) { | |
| 87 auto iter = script_render_info_map_.find(script.id()); | |
| 88 if (iter != script_render_info_map_.end()) { | |
| 89 info.render_process_id = iter->second.render_process_id; | |
| 90 info.render_view_id = iter->second.render_view_id; | |
| 91 } | |
| 92 find_render_info = true; | |
| 93 } | |
| 94 | |
| 95 for (size_t k = 0; k < script.js_scripts().size(); ++k) { | |
| 96 extensions::UserScript::File& script_file = script.js_scripts()[k]; | |
| 97 if (script_file.GetContent().empty()) { | |
| 98 file_set.insert(&script_file); | |
| 99 } | |
| 100 } | |
| 101 for (size_t k = 0; k < script.css_scripts().size(); ++k) { | |
| 102 extensions::UserScript::File& script_file = script.css_scripts()[k]; | |
| 103 if (script_file.GetContent().empty()) { | |
| 104 file_set.insert(&script_file); | |
| 105 } | |
| 106 } | |
| 107 script_render_info_map_.erase(script.id()); | |
| 108 } | |
| 109 // Initial URL fetches. | |
| 110 complete_fetchers_ = 0; | |
| 111 total_fetchers_ = file_set.size(); | |
| 112 | |
| 113 for (extensions::UserScript::File* file : file_set) { | |
| 114 scoped_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher( | |
| 115 browser_context(), | |
| 116 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete, | |
| 117 base::Unretained(this), file))); | |
| 118 fetcher->Start(info.render_process_id, info.render_view_id, file->url()); | |
| 119 fetchers_.push_back(fetcher.release()); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete( | |
| 124 extensions::UserScript::File* script_file, | |
| 125 bool success, | |
| 126 const std::string& data) { | |
| 127 if (success) { | |
| 128 // Remove BOM from the content. | |
| 129 std::string::size_type index = data.find(base::kUtf8ByteOrderMark); | |
| 130 if (index == 0) | |
| 131 script_file->set_content(data.substr(strlen(base::kUtf8ByteOrderMark))); | |
| 132 else | |
| 133 script_file->set_content(data); | |
| 134 } | |
| 135 | |
| 136 ++complete_fetchers_; | |
| 137 if (complete_fetchers_ == total_fetchers_) { | |
| 138 OnWebUIURLFetchComplete(); | |
| 139 fetchers_.clear(); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() { | |
| 144 content::BrowserThread::PostTask( | |
| 145 content::BrowserThread::FILE, FROM_HERE, | |
| 146 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_), | |
| 147 base::Bind(&WebUIUserScriptLoader::OnScriptsLoaded, | |
| 148 weak_factory_.GetWeakPtr()))); | |
| 149 user_scripts_.reset(nullptr); | |
| 150 } | |
| OLD | NEW |