Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/web_ui_user_script_loader.h" | 5 #include "extensions/browser/web_ui_user_script_loader.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 41 WebUIUserScriptLoader::WebUIUserScriptLoader( | 41 WebUIUserScriptLoader::WebUIUserScriptLoader( |
| 42 content::BrowserContext* browser_context, | 42 content::BrowserContext* browser_context, |
| 43 const HostID& host_id) | 43 const HostID& host_id) |
| 44 : UserScriptLoader(browser_context, host_id), complete_fetchers_(0) { | 44 : UserScriptLoader(browser_context, host_id), complete_fetchers_(0) { |
| 45 SetReady(true); | 45 SetReady(true); |
| 46 } | 46 } |
| 47 | 47 |
| 48 WebUIUserScriptLoader::~WebUIUserScriptLoader() { | 48 WebUIUserScriptLoader::~WebUIUserScriptLoader() { |
| 49 } | 49 } |
| 50 | 50 |
| 51 void WebUIUserScriptLoader::AddScripts( | 51 void WebUIUserScriptLoader::AddScripts(extensions::UserScriptList& scripts, |
| 52 const extensions::UserScriptList& scripts, | 52 int render_process_id, |
| 53 int render_process_id, | 53 int render_frame_id) { |
| 54 int render_frame_id) { | |
| 55 UserScriptRenderInfo info(render_process_id, render_frame_id); | 54 UserScriptRenderInfo info(render_process_id, render_frame_id); |
| 56 for (const extensions::UserScript& script : scripts) { | 55 for (const std::unique_ptr<extensions::UserScript>& script : scripts) { |
| 57 script_render_info_map_.insert( | 56 script_render_info_map_.insert( |
| 58 std::pair<int, UserScriptRenderInfo>(script.id(), info)); | 57 std::pair<int, UserScriptRenderInfo>(script->id(), info)); |
| 59 } | 58 } |
| 60 | 59 |
| 61 extensions::UserScriptLoader::AddScripts(scripts); | 60 extensions::UserScriptLoader::AddScripts(scripts); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void WebUIUserScriptLoader::LoadScripts( | 63 void WebUIUserScriptLoader::LoadScripts( |
| 65 std::unique_ptr<extensions::UserScriptList> user_scripts, | 64 std::unique_ptr<extensions::UserScriptList> user_scripts, |
| 66 const std::set<HostID>& changed_hosts, | 65 const std::set<HostID>& changed_hosts, |
| 67 const std::set<int>& added_script_ids, | 66 const std::set<int>& added_script_ids, |
| 68 LoadScriptsCallback callback) { | 67 LoadScriptsCallback callback) { |
| 69 user_scripts_cache_.swap(user_scripts); | 68 user_scripts_cache_.swap(user_scripts); |
| 70 scripts_loaded_callback_ = callback; | 69 scripts_loaded_callback_ = callback; |
| 71 | 70 |
| 72 // The total number of the tasks is used to trace whether all the fetches | 71 // The total number of the tasks is used to trace whether all the fetches |
| 73 // are complete. Therefore, we store all the fetcher pointers in |fetchers_| | 72 // are complete. Therefore, we store all the fetcher pointers in |fetchers_| |
| 74 // before we get theis number. Once we get the total number, start each | 73 // before we get theis number. Once we get the total number, start each |
| 75 // fetch tasks. | 74 // fetch tasks. |
| 76 DCHECK_EQ(0u, complete_fetchers_); | 75 DCHECK_EQ(0u, complete_fetchers_); |
| 77 | 76 |
| 78 for (extensions::UserScript& script : *user_scripts_cache_) { | 77 for (const std::unique_ptr<extensions::UserScript>& script : |
| 79 if (added_script_ids.count(script.id()) == 0) | 78 *user_scripts_cache_) { |
| 79 if (added_script_ids.count(script->id()) == 0) | |
| 80 continue; | 80 continue; |
| 81 | 81 |
| 82 auto iter = script_render_info_map_.find(script.id()); | 82 auto iter = script_render_info_map_.find(script->id()); |
| 83 DCHECK(iter != script_render_info_map_.end()); | 83 DCHECK(iter != script_render_info_map_.end()); |
| 84 int render_process_id = iter->second.render_process_id; | 84 int render_process_id = iter->second.render_process_id; |
| 85 int render_frame_id = iter->second.render_frame_id; | 85 int render_frame_id = iter->second.render_frame_id; |
| 86 | 86 |
| 87 content::BrowserContext* browser_context = | 87 content::BrowserContext* browser_context = |
| 88 content::RenderProcessHost::FromID(render_process_id) | 88 content::RenderProcessHost::FromID(render_process_id) |
| 89 ->GetBrowserContext(); | 89 ->GetBrowserContext(); |
| 90 | 90 |
| 91 CreateWebUIURLFetchers(&script.js_scripts(), browser_context, | 91 CreateWebUIURLFetchers(&script->js_scripts(), browser_context, |
| 92 render_process_id, render_frame_id); | 92 render_process_id, render_frame_id); |
| 93 CreateWebUIURLFetchers(&script.css_scripts(), browser_context, | 93 CreateWebUIURLFetchers(&script->css_scripts(), browser_context, |
| 94 render_process_id, render_frame_id); | 94 render_process_id, render_frame_id); |
| 95 | 95 |
| 96 script_render_info_map_.erase(script.id()); | 96 script_render_info_map_.erase(script->id()); |
| 97 } | 97 } |
| 98 | 98 |
| 99 // If no fetch is needed, call OnWebUIURLFetchComplete directly. | 99 // If no fetch is needed, call OnWebUIURLFetchComplete directly. |
| 100 if (fetchers_.empty()) { | 100 if (fetchers_.empty()) { |
| 101 OnWebUIURLFetchComplete(); | 101 OnWebUIURLFetchComplete(); |
| 102 return; | 102 return; |
| 103 } | 103 } |
| 104 for (const auto& fetcher : fetchers_) | 104 for (const auto& fetcher : fetchers_) |
| 105 fetcher->Start(); | 105 fetcher->Start(); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void WebUIUserScriptLoader::CreateWebUIURLFetchers( | 108 void WebUIUserScriptLoader::CreateWebUIURLFetchers( |
| 109 extensions::UserScript::FileList* script_files, | 109 extensions::UserScript::FileList* script_files, |
|
Devlin
2016/08/17 16:39:31
this should pass by const &
lazyboy
2016/08/17 18:55:52
Done.
| |
| 110 content::BrowserContext* browser_context, | 110 content::BrowserContext* browser_context, |
| 111 int render_process_id, | 111 int render_process_id, |
| 112 int render_frame_id) { | 112 int render_frame_id) { |
| 113 for (extensions::UserScript::File& file : *script_files) { | 113 for (const std::unique_ptr<extensions::UserScript::File>& script_file : |
| 114 if (file.GetContent().empty()) { | 114 *script_files) { |
| 115 if (script_file->GetContent().empty()) { | |
| 115 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the | 116 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the |
| 116 // loader is destroyed, all the fetchers will be destroyed. Therefore, | 117 // loader is destroyed, all the fetchers will be destroyed. Therefore, |
| 117 // we are sure it is safe to use base::Unretained(this) here. | 118 // we are sure it is safe to use base::Unretained(this) here. |
|
Devlin
2016/08/17 16:39:31
This explains why base::Unretained(this) is safe,
lazyboy
2016/08/17 18:55:52
Added note here.
but it'd be nice to maybe DCHECK
Devlin
2016/08/17 20:40:39
In practice, yes, I think so. But AFAIK, base::Pa
lazyboy
2016/08/17 21:34:20
LOL
| |
| 118 std::unique_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher( | 119 std::unique_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher( |
| 119 browser_context, render_process_id, render_frame_id, file.url(), | 120 browser_context, render_process_id, render_frame_id, |
| 121 script_file->url(), | |
| 120 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete, | 122 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete, |
| 121 base::Unretained(this), &file))); | 123 base::Unretained(this), script_file.get()))); |
| 122 fetchers_.push_back(std::move(fetcher)); | 124 fetchers_.push_back(std::move(fetcher)); |
| 123 } | 125 } |
| 124 } | 126 } |
| 125 } | 127 } |
| 126 | 128 |
| 127 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete( | 129 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete( |
| 128 extensions::UserScript::File* script_file, | 130 extensions::UserScript::File* script_file, |
| 129 bool success, | 131 bool success, |
| 130 const std::string& data) { | 132 const std::string& data) { |
| 131 if (success) { | 133 if (success) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 146 } | 148 } |
| 147 } | 149 } |
| 148 | 150 |
| 149 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() { | 151 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() { |
| 150 content::BrowserThread::PostTask( | 152 content::BrowserThread::PostTask( |
| 151 content::BrowserThread::FILE, FROM_HERE, | 153 content::BrowserThread::FILE, FROM_HERE, |
| 152 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_), | 154 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_), |
| 153 scripts_loaded_callback_)); | 155 scripts_loaded_callback_)); |
| 154 scripts_loaded_callback_.Reset(); | 156 scripts_loaded_callback_.Reset(); |
| 155 } | 157 } |
| OLD | NEW |