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 "content/public/browser/browser_context.h" | |
10 #include "content/public/browser/browser_thread.h" | |
11 #include "extensions/browser/content_verifier.h" | |
12 | |
13 namespace { | |
14 using LoadScriptsCallback = | |
15 base::Callback<void(scoped_ptr<extensions::UserScriptList>, | |
16 scoped_ptr<base::SharedMemory>)>; | |
17 | |
18 void SerializeOnFileThread(scoped_ptr<extensions::UserScriptList> user_scripts, | |
19 LoadScriptsCallback callback) { | |
20 scoped_ptr<base::SharedMemory> memory = | |
21 extensions::UserScriptLoader::Serialize(*user_scripts); | |
22 content::BrowserThread::PostTask( | |
23 content::BrowserThread::UI, FROM_HERE, | |
24 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory))); | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 struct WebUIUserScriptLoader::UserScriptRenderInfo { | |
30 int render_process_id; | |
31 int render_view_id; | |
32 | |
33 UserScriptRenderInfo() : render_process_id(-1), render_view_id(-1) {} | |
34 | |
35 UserScriptRenderInfo(int render_process_id, int render_view_id) | |
36 : render_process_id(render_process_id), render_view_id(render_view_id) {} | |
37 | |
38 bool operator<(const UserScriptRenderInfo& other) { | |
39 if (render_process_id != other.render_process_id) | |
40 return render_process_id < other.render_process_id; | |
41 | |
42 if (render_view_id != other.render_view_id) | |
43 return render_view_id < other.render_view_id; | |
44 | |
45 return false; | |
46 } | |
47 }; | |
48 | |
49 WebUIUserScriptLoader::WebUIUserScriptLoader( | |
50 content::BrowserContext* browser_context, | |
51 const HostID& host_id) | |
52 : UserScriptLoader(browser_context, host_id), weak_factory_(this) { | |
53 SetReady(true); | |
54 } | |
55 | |
56 WebUIUserScriptLoader::~WebUIUserScriptLoader() { | |
57 fetcher_groups_.clear(); | |
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, | |
73 const std::set<int>& added_script_ids) { | |
74 ClearScripts(false); | |
75 scoped_ptr<extensions::UserScriptList> user_scripts(user_scripts_.Pass()); | |
76 user_scripts_.reset(nullptr); | |
77 | |
78 std::set<extensions::UserScript::File*> file_set; | |
79 bool find_render_info = false; | |
80 UserScriptRenderInfo info; | |
81 | |
82 for (extensions::UserScript& script : *user_scripts) { | |
83 if (added_script_ids.count(script.id()) == 0) | |
84 continue; | |
85 | |
86 // Since every script in |added_script_ids| is associated with the same | |
87 // render info, so we only need to look up the map once. | |
88 if (!find_render_info) { | |
89 auto iter = script_render_info_map_.find(script.id()); | |
90 if (iter != script_render_info_map_.end()) { | |
91 info.render_process_id = iter->second.render_process_id; | |
92 info.render_view_id = iter->second.render_view_id; | |
93 } | |
94 find_render_info = true; | |
95 } | |
96 | |
97 for (size_t k = 0; k < script.js_scripts().size(); ++k) { | |
98 extensions::UserScript::File& script_file = script.js_scripts()[k]; | |
99 if (script_file.GetContent().empty()) { | |
100 file_set.insert(&script_file); | |
101 } | |
102 } | |
103 for (size_t k = 0; k < script.css_scripts().size(); ++k) { | |
104 extensions::UserScript::File& script_file = script.css_scripts()[k]; | |
105 if (script_file.GetContent().empty()) { | |
106 file_set.insert(&script_file); | |
107 } | |
108 } | |
109 script_render_info_map_.erase(script.id()); | |
110 } | |
111 | |
112 scoped_refptr<WebUIURLFetcherGroup> fetcher_group(new WebUIURLFetcherGroup( | |
113 browser_context(), | |
114 base::Bind(&WebUIUserScriptLoader::OnWebUIURLFetchComplete, | |
115 weak_factory_.GetWeakPtr(), base::Passed(&user_scripts)))); | |
116 | |
117 fetcher_group->Start(file_set, info.render_process_id, info.render_view_id); | |
Devlin
2015/04/17 22:05:05
In what circumstance will we ever have more than o
Xi Han
2015/04/17 22:28:24
So that means we can assume there is always only o
Devlin
2015/04/17 22:40:15
SGTM.
Xi Han
2015/04/20 22:40:10
Done.
| |
118 fetcher_groups_.insert(fetcher_group.get()); | |
119 } | |
120 | |
121 void WebUIUserScriptLoader::OnWebUIURLFetchComplete( | |
122 scoped_ptr<extensions::UserScriptList> user_scripts) { | |
123 content::BrowserThread::PostTask( | |
124 content::BrowserThread::FILE, FROM_HERE, | |
125 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts), | |
126 base::Bind(&WebUIUserScriptLoader::OnScriptsLoaded, | |
127 weak_factory_.GetWeakPtr()))); | |
128 } | |
OLD | NEW |