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) { | |
Devlin
2015/04/22 21:15:33
Why do we need this?
Xi Han
2015/04/22 22:34:00
We store the info objects in a map, so it requires
Devlin
2015/04/22 23:20:50
Only the key type of a std::map requires the opera
Xi Han
2015/04/23 15:01:55
You are right, removed.
| |
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), | |
54 complete_fetchers_(0), | |
55 user_scripts_cache_(nullptr), | |
56 weak_factory_(this) { | |
57 SetReady(true); | |
58 } | |
59 | |
60 WebUIUserScriptLoader::~WebUIUserScriptLoader() { | |
61 } | |
62 | |
63 void WebUIUserScriptLoader::AddScripts( | |
64 const std::set<extensions::UserScript>& scripts, | |
65 int render_process_id, | |
66 int render_view_id) { | |
67 UserScriptRenderInfo info(render_process_id, render_view_id); | |
68 for (const extensions::UserScript& script : scripts) { | |
69 script_render_info_map_.insert( | |
70 std::pair<int, UserScriptRenderInfo>(script.id(), info)); | |
71 } | |
72 | |
73 extensions::UserScriptLoader::AddScripts(scripts); | |
74 } | |
75 | |
76 void WebUIUserScriptLoader::LoadScripts( | |
77 scoped_ptr<extensions::UserScriptList> user_scripts, | |
78 const std::set<HostID>& changed_hosts, | |
79 const std::set<int>& added_script_ids) { | |
Devlin
2015/04/22 21:15:33
Let's make this LoadScriptsAndSerialize, and I sti
Xi Han
2015/04/22 22:34:00
A compromise is we have to declare the callback ty
Devlin
2015/04/22 23:20:50
I don't understand. Why?
LoadUserScriptsAndSeria
Xi Han
2015/04/23 15:01:55
I removed the declaration of LoadScriptsCallback f
Devlin
2015/04/23 16:34:38
Yeah, there's nothing wrong with having the typede
| |
80 user_scripts_cache_.swap(user_scripts); | |
81 | |
82 // The total number of the tasks is used to trace whether all the fetches | |
83 // are complete. Therefore, we store all the fetcher pointers in |fetchers_| | |
84 // before we get theis number. Once we get the total number, start each | |
85 // fetch tasks. | |
86 complete_fetchers_ = 0; | |
87 | |
88 for (extensions::UserScript& script : *user_scripts_cache_) { | |
89 if (added_script_ids.count(script.id()) == 0) | |
90 continue; | |
91 | |
92 auto iter = script_render_info_map_.find(script.id()); | |
93 DCHECK(iter != script_render_info_map_.end()); | |
94 int render_process_id = iter->second.render_process_id; | |
95 int render_view_id = iter->second.render_view_id; | |
96 | |
97 CreateWebUIURLFetchers(&script.js_scripts(), render_process_id, | |
Devlin
2015/04/22 21:15:33
Why not pass by const &?
Xi Han
2015/04/22 22:34:00
Once the fetch is done, we will modify the file by
| |
98 render_view_id); | |
99 CreateWebUIURLFetchers(&script.css_scripts(), render_process_id, | |
100 render_view_id); | |
101 | |
102 script_render_info_map_.erase(script.id()); | |
103 } | |
104 | |
105 // If no fetch is needed, call OnWebUIURLFetchComplete directly. | |
106 if (fetchers_.empty()) { | |
107 OnWebUIURLFetchComplete(); | |
108 return; | |
109 } | |
110 for (auto fetcher : fetchers_) | |
111 fetcher->Start(); | |
112 } | |
113 | |
114 void WebUIUserScriptLoader::CreateWebUIURLFetchers( | |
115 extensions::UserScript::FileList* script_files, | |
116 int render_process_id, | |
117 int render_view_id) { | |
118 for (extensions::UserScript::File& file : *script_files) { | |
119 if (file.GetContent().empty()) { | |
120 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the | |
121 // loader is destroyed, all the fetchers will be destroyed. Therefore, | |
122 // we are sure it is safe to use base::Unretained(this) here. | |
123 scoped_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher( | |
124 browser_context(), render_process_id, render_view_id, file.url(), | |
125 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete, | |
126 base::Unretained(this), &file))); | |
127 fetchers_.push_back(fetcher.release()); | |
128 } | |
129 } | |
130 } | |
131 | |
132 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete( | |
133 extensions::UserScript::File* script_file, | |
134 bool success, | |
135 const std::string& data) { | |
136 if (success) { | |
137 // Remove BOM from the content. | |
138 std::string::size_type index = data.find(base::kUtf8ByteOrderMark); | |
139 if (index == 0) | |
140 script_file->set_content(data.substr(strlen(base::kUtf8ByteOrderMark))); | |
141 else | |
142 script_file->set_content(data); | |
143 } | |
144 | |
145 ++complete_fetchers_; | |
146 if (complete_fetchers_ == fetchers_.size()) { | |
147 complete_fetchers_ = 0; | |
148 OnWebUIURLFetchComplete(); | |
149 fetchers_.clear(); | |
150 } | |
151 } | |
152 | |
153 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() { | |
154 content::BrowserThread::PostTask( | |
155 content::BrowserThread::FILE, FROM_HERE, | |
156 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_), | |
157 base::Bind(&WebUIUserScriptLoader::OnScriptsLoaded, | |
158 weak_factory_.GetWeakPtr()))); | |
159 } | |
OLD | NEW |