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

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: uplaod with base 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"
11 #include "content/public/browser/browser_context.h" 11 #include "content/public/browser/browser_context.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/render_process_host.h" 13 #include "content/public/browser/render_process_host.h"
14 #include "extensions/browser/guest_view/web_view/web_ui/web_ui_url_fetcher.h" 14 #include "extensions/browser/guest_view/web_view/web_ui/web_ui_url_fetcher.h"
15 15
16 namespace { 16 namespace {
17 17
18 void SerializeOnFileThread( 18 void SerializeOnFileThread(
19 std::unique_ptr<extensions::UserScriptList> user_scripts, 19 std::unique_ptr<extensions::BrowserUserScriptList> user_scripts,
20 extensions::UserScriptLoader::LoadScriptsCallback callback) { 20 extensions::UserScriptLoader::LoadScriptsCallback callback) {
21 std::unique_ptr<base::SharedMemory> memory = 21 std::unique_ptr<base::SharedMemory> memory =
22 extensions::UserScriptLoader::Serialize(*user_scripts); 22 extensions::UserScriptLoader::Serialize(*user_scripts);
23 content::BrowserThread::PostTask( 23 content::BrowserThread::PostTask(
24 content::BrowserThread::UI, FROM_HERE, 24 content::BrowserThread::UI, FROM_HERE,
25 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory))); 25 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory)));
26 } 26 }
27 27
28 } // namespace 28 } // namespace
29 29
(...skipping 11 matching lines...) Expand all
41 content::BrowserContext* browser_context, 41 content::BrowserContext* browser_context,
42 const HostID& host_id) 42 const HostID& host_id)
43 : UserScriptLoader(browser_context, host_id), complete_fetchers_(0) { 43 : UserScriptLoader(browser_context, host_id), complete_fetchers_(0) {
44 SetReady(true); 44 SetReady(true);
45 } 45 }
46 46
47 WebUIUserScriptLoader::~WebUIUserScriptLoader() { 47 WebUIUserScriptLoader::~WebUIUserScriptLoader() {
48 } 48 }
49 49
50 void WebUIUserScriptLoader::AddScripts( 50 void WebUIUserScriptLoader::AddScripts(
51 const extensions::UserScriptList& scripts, 51 extensions::BrowserUserScriptList& scripts,
52 int render_process_id, 52 int render_process_id,
53 int render_view_id) { 53 int render_view_id) {
54 UserScriptRenderInfo info(render_process_id, render_view_id); 54 UserScriptRenderInfo info(render_process_id, render_view_id);
55 for (const extensions::UserScript& script : scripts) { 55 for (const std::unique_ptr<extensions::BrowserUserScript>& script : scripts) {
56 script_render_info_map_.insert( 56 script_render_info_map_.insert(
57 std::pair<int, UserScriptRenderInfo>(script.id(), info)); 57 std::pair<int, UserScriptRenderInfo>(script->id(), info));
58 } 58 }
59 59
60 extensions::UserScriptLoader::AddScripts(scripts); 60 extensions::UserScriptLoader::AddScripts(scripts);
61 } 61 }
62 62
63 void WebUIUserScriptLoader::LoadScripts( 63 void WebUIUserScriptLoader::LoadScripts(
64 std::unique_ptr<extensions::UserScriptList> user_scripts, 64 std::unique_ptr<extensions::BrowserUserScriptList> user_scripts,
65 const std::set<HostID>& changed_hosts, 65 const std::set<HostID>& changed_hosts,
66 const std::set<int>& added_script_ids, 66 const std::set<int>& added_script_ids,
67 LoadScriptsCallback callback) { 67 LoadScriptsCallback callback) {
68 user_scripts_cache_.swap(user_scripts); 68 user_scripts_cache_.swap(user_scripts);
69 scripts_loaded_callback_ = callback; 69 scripts_loaded_callback_ = callback;
70 70
71 // 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
72 // are complete. Therefore, we store all the fetcher pointers in |fetchers_| 72 // are complete. Therefore, we store all the fetcher pointers in |fetchers_|
73 // 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
74 // fetch tasks. 74 // fetch tasks.
75 DCHECK_EQ(0u, complete_fetchers_); 75 DCHECK_EQ(0u, complete_fetchers_);
76 76
77 for (extensions::UserScript& script : *user_scripts_cache_) { 77 for (const std::unique_ptr<extensions::BrowserUserScript>& script :
78 if (added_script_ids.count(script.id()) == 0) 78 *user_scripts_cache_) {
79 if (added_script_ids.count(script->id()) == 0)
79 continue; 80 continue;
80 81
81 auto iter = script_render_info_map_.find(script.id()); 82 auto iter = script_render_info_map_.find(script->id());
82 DCHECK(iter != script_render_info_map_.end()); 83 DCHECK(iter != script_render_info_map_.end());
83 int render_process_id = iter->second.render_process_id; 84 int render_process_id = iter->second.render_process_id;
84 int render_view_id = iter->second.render_view_id; 85 int render_view_id = iter->second.render_view_id;
85 86
86 content::BrowserContext* browser_context = 87 content::BrowserContext* browser_context =
87 content::RenderProcessHost::FromID(render_process_id) 88 content::RenderProcessHost::FromID(render_process_id)
88 ->GetBrowserContext(); 89 ->GetBrowserContext();
89 90
90 CreateWebUIURLFetchers(&script.js_scripts(), browser_context, 91 CreateWebUIURLFetchers(&script->js_scripts(), browser_context,
91 render_process_id, render_view_id); 92 render_process_id, render_view_id);
92 CreateWebUIURLFetchers(&script.css_scripts(), browser_context, 93 CreateWebUIURLFetchers(&script->css_scripts(), browser_context,
93 render_process_id, render_view_id); 94 render_process_id, render_view_id);
94 95
95 script_render_info_map_.erase(script.id()); 96 script_render_info_map_.erase(script->id());
96 } 97 }
97 98
98 // If no fetch is needed, call OnWebUIURLFetchComplete directly. 99 // If no fetch is needed, call OnWebUIURLFetchComplete directly.
99 if (fetchers_.empty()) { 100 if (fetchers_.empty()) {
100 OnWebUIURLFetchComplete(); 101 OnWebUIURLFetchComplete();
101 return; 102 return;
102 } 103 }
103 for (const auto& fetcher : fetchers_) 104 for (const auto& fetcher : fetchers_)
104 fetcher->Start(); 105 fetcher->Start();
105 } 106 }
106 107
107 void WebUIUserScriptLoader::CreateWebUIURLFetchers( 108 void WebUIUserScriptLoader::CreateWebUIURLFetchers(
108 extensions::UserScript::FileList* script_files, 109 extensions::BrowserScriptFileList* script_files,
109 content::BrowserContext* browser_context, 110 content::BrowserContext* browser_context,
110 int render_process_id, 111 int render_process_id,
111 int render_view_id) { 112 int render_view_id) {
112 for (extensions::UserScript::File& file : *script_files) { 113 for (const std::unique_ptr<extensions::BrowserScriptFile>& file :
113 if (file.GetContent().empty()) { 114 *script_files) {
115 if (file->GetContent().empty()) {
114 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the 116 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the
115 // loader is destroyed, all the fetchers will be destroyed. Therefore, 117 // loader is destroyed, all the fetchers will be destroyed. Therefore,
116 // 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.
117 std::unique_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher( 119 std::unique_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher(
118 browser_context, render_process_id, render_view_id, file.url(), 120 browser_context, render_process_id, render_view_id, file->url(),
119 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete, 121 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete,
120 base::Unretained(this), &file))); 122 base::Unretained(this), file.get())));
121 fetchers_.push_back(std::move(fetcher)); 123 fetchers_.push_back(std::move(fetcher));
122 } 124 }
123 } 125 }
124 } 126 }
125 127
126 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete( 128 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete(
127 extensions::UserScript::File* script_file, 129 extensions::BrowserScriptFile* script_file,
128 bool success, 130 bool success,
129 const std::string& data) { 131 const std::string& data) {
130 if (success) { 132 if (success) {
131 // Remove BOM from |data|. 133 // Remove BOM from |data|.
132 if (base::StartsWith(data, base::kUtf8ByteOrderMark, 134 if (base::StartsWith(data, base::kUtf8ByteOrderMark,
133 base::CompareCase::SENSITIVE)) { 135 base::CompareCase::SENSITIVE)) {
134 script_file->set_content(data.substr(strlen(base::kUtf8ByteOrderMark))); 136 script_file->set_content(data.substr(strlen(base::kUtf8ByteOrderMark)));
135 } else { 137 } else {
136 script_file->set_content(data); 138 script_file->set_content(data);
137 } 139 }
138 } 140 }
139 141
140 ++complete_fetchers_; 142 ++complete_fetchers_;
141 if (complete_fetchers_ == fetchers_.size()) { 143 if (complete_fetchers_ == fetchers_.size()) {
142 complete_fetchers_ = 0; 144 complete_fetchers_ = 0;
143 OnWebUIURLFetchComplete(); 145 OnWebUIURLFetchComplete();
144 fetchers_.clear(); 146 fetchers_.clear();
145 } 147 }
146 } 148 }
147 149
148 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() { 150 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() {
149 content::BrowserThread::PostTask( 151 content::BrowserThread::PostTask(
150 content::BrowserThread::FILE, FROM_HERE, 152 content::BrowserThread::FILE, FROM_HERE,
151 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_), 153 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_),
152 scripts_loaded_callback_)); 154 scripts_loaded_callback_));
153 scripts_loaded_callback_.Reset(); 155 scripts_loaded_callback_.Reset();
154 } 156 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698