Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(351)

Side by Side Diff: extensions/browser/web_ui_user_script_loader.cc

Issue 2227193002: Make UserScript non-copyable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: address comments from Devlin Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(
52 const extensions::UserScriptList& scripts, 52 std::unique_ptr<extensions::UserScriptList> scripts,
53 int render_process_id, 53 int render_process_id,
54 int render_frame_id) { 54 int render_frame_id) {
55 UserScriptRenderInfo info(render_process_id, render_frame_id); 55 UserScriptRenderInfo info(render_process_id, render_frame_id);
56 for (const extensions::UserScript& script : scripts) { 56 for (const std::unique_ptr<extensions::UserScript>& script : *scripts) {
57 script_render_info_map_.insert( 57 script_render_info_map_.insert(
58 std::pair<int, UserScriptRenderInfo>(script.id(), info)); 58 std::pair<int, UserScriptRenderInfo>(script->id(), info));
59 } 59 }
60 60
61 extensions::UserScriptLoader::AddScripts(scripts); 61 extensions::UserScriptLoader::AddScripts(std::move(scripts));
62 } 62 }
63 63
64 void WebUIUserScriptLoader::LoadScripts( 64 void WebUIUserScriptLoader::LoadScripts(
65 std::unique_ptr<extensions::UserScriptList> user_scripts, 65 std::unique_ptr<extensions::UserScriptList> user_scripts,
66 const std::set<HostID>& changed_hosts, 66 const std::set<HostID>& changed_hosts,
67 const std::set<int>& added_script_ids, 67 const std::set<int>& added_script_ids,
68 LoadScriptsCallback callback) { 68 LoadScriptsCallback callback) {
69 DCHECK(!user_scripts_cache_) << "Loading scripts in flight.";
69 user_scripts_cache_.swap(user_scripts); 70 user_scripts_cache_.swap(user_scripts);
70 scripts_loaded_callback_ = callback; 71 scripts_loaded_callback_ = callback;
71 72
72 // The total number of the tasks is used to trace whether all the fetches 73 // 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_| 74 // 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 75 // before we get theis number. Once we get the total number, start each
75 // fetch tasks. 76 // fetch tasks.
76 DCHECK_EQ(0u, complete_fetchers_); 77 DCHECK_EQ(0u, complete_fetchers_);
77 78
78 for (extensions::UserScript& script : *user_scripts_cache_) { 79 for (const std::unique_ptr<extensions::UserScript>& script :
79 if (added_script_ids.count(script.id()) == 0) 80 *user_scripts_cache_) {
81 if (added_script_ids.count(script->id()) == 0)
80 continue; 82 continue;
81 83
82 auto iter = script_render_info_map_.find(script.id()); 84 auto iter = script_render_info_map_.find(script->id());
83 DCHECK(iter != script_render_info_map_.end()); 85 DCHECK(iter != script_render_info_map_.end());
84 int render_process_id = iter->second.render_process_id; 86 int render_process_id = iter->second.render_process_id;
85 int render_frame_id = iter->second.render_frame_id; 87 int render_frame_id = iter->second.render_frame_id;
86 88
87 content::BrowserContext* browser_context = 89 content::BrowserContext* browser_context =
88 content::RenderProcessHost::FromID(render_process_id) 90 content::RenderProcessHost::FromID(render_process_id)
89 ->GetBrowserContext(); 91 ->GetBrowserContext();
90 92
91 CreateWebUIURLFetchers(&script.js_scripts(), browser_context, 93 CreateWebUIURLFetchers(script->js_scripts(), browser_context,
92 render_process_id, render_frame_id); 94 render_process_id, render_frame_id);
93 CreateWebUIURLFetchers(&script.css_scripts(), browser_context, 95 CreateWebUIURLFetchers(script->css_scripts(), browser_context,
94 render_process_id, render_frame_id); 96 render_process_id, render_frame_id);
95 97
96 script_render_info_map_.erase(script.id()); 98 script_render_info_map_.erase(script->id());
97 } 99 }
98 100
99 // If no fetch is needed, call OnWebUIURLFetchComplete directly. 101 // If no fetch is needed, call OnWebUIURLFetchComplete directly.
100 if (fetchers_.empty()) { 102 if (fetchers_.empty()) {
101 OnWebUIURLFetchComplete(); 103 OnWebUIURLFetchComplete();
102 return; 104 return;
103 } 105 }
104 for (const auto& fetcher : fetchers_) 106 for (const auto& fetcher : fetchers_)
105 fetcher->Start(); 107 fetcher->Start();
106 } 108 }
107 109
108 void WebUIUserScriptLoader::CreateWebUIURLFetchers( 110 void WebUIUserScriptLoader::CreateWebUIURLFetchers(
109 extensions::UserScript::FileList* script_files, 111 const extensions::UserScript::FileList& script_files,
110 content::BrowserContext* browser_context, 112 content::BrowserContext* browser_context,
111 int render_process_id, 113 int render_process_id,
112 int render_frame_id) { 114 int render_frame_id) {
113 for (extensions::UserScript::File& file : *script_files) { 115 for (const std::unique_ptr<extensions::UserScript::File>& script_file :
114 if (file.GetContent().empty()) { 116 script_files) {
117 if (script_file->GetContent().empty()) {
115 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the 118 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the
116 // loader is destroyed, all the fetchers will be destroyed. Therefore, 119 // loader is destroyed, all the fetchers will be destroyed. Therefore,
117 // we are sure it is safe to use base::Unretained(this) here. 120 // we are sure it is safe to use base::Unretained(this) here.
121 // |user_scripts_cache_| retains ownership of the scripts while they are
122 // being loaded, so passing raw pointer to |script_file| below to
Devlin 2016/08/17 20:40:39 nit: "...*a* raw pointer..."
lazyboy 2016/08/17 21:34:21 Done.
123 // WebUIUserScriptLoader is also safe.
118 std::unique_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher( 124 std::unique_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher(
119 browser_context, render_process_id, render_frame_id, file.url(), 125 browser_context, render_process_id, render_frame_id,
126 script_file->url(),
120 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete, 127 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete,
121 base::Unretained(this), &file))); 128 base::Unretained(this), script_file.get())));
122 fetchers_.push_back(std::move(fetcher)); 129 fetchers_.push_back(std::move(fetcher));
123 } 130 }
124 } 131 }
125 } 132 }
126 133
127 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete( 134 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete(
128 extensions::UserScript::File* script_file, 135 extensions::UserScript::File* script_file,
129 bool success, 136 bool success,
130 const std::string& data) { 137 const std::string& data) {
131 if (success) { 138 if (success) {
(...skipping 14 matching lines...) Expand all
146 } 153 }
147 } 154 }
148 155
149 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() { 156 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() {
150 content::BrowserThread::PostTask( 157 content::BrowserThread::PostTask(
151 content::BrowserThread::FILE, FROM_HERE, 158 content::BrowserThread::FILE, FROM_HERE,
152 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_), 159 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_),
153 scripts_loaded_callback_)); 160 scripts_loaded_callback_));
154 scripts_loaded_callback_.Reset(); 161 scripts_loaded_callback_.Reset();
155 } 162 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698