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

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

Issue 1062963006: Make WebUI webview content script work with incognito. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webui_api_4
Patch Set: Created 5 years, 8 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
« no previous file with comments | « extensions/browser/web_ui_user_script_loader.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/bind.h" 7 #include "base/bind.h"
8 #include "base/pickle.h" 8 #include "base/pickle.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "content/public/browser/browser_context.h" 10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 #include "extensions/browser/content_verifier.h" 12 #include "extensions/browser/content_verifier.h"
13 #include "extensions/browser/extensions_browser_client.h"
13 14
14 namespace { 15 namespace {
15 using LoadScriptsCallback = 16 using LoadScriptsCallback =
16 base::Callback<void(scoped_ptr<extensions::UserScriptList>, 17 base::Callback<void(scoped_ptr<extensions::UserScriptList>,
17 scoped_ptr<base::SharedMemory>)>; 18 scoped_ptr<base::SharedMemory>)>;
18 19
19 void SerializeOnFileThread(scoped_ptr<extensions::UserScriptList> user_scripts, 20 void SerializeOnFileThread(scoped_ptr<extensions::UserScriptList> user_scripts,
20 LoadScriptsCallback callback) { 21 LoadScriptsCallback callback) {
21 scoped_ptr<base::SharedMemory> memory = 22 scoped_ptr<base::SharedMemory> memory =
22 extensions::UserScriptLoader::Serialize(*user_scripts); 23 extensions::UserScriptLoader::Serialize(*user_scripts);
23 content::BrowserThread::PostTask( 24 content::BrowserThread::PostTask(
24 content::BrowserThread::UI, FROM_HERE, 25 content::BrowserThread::UI, FROM_HERE,
25 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory))); 26 base::Bind(callback, base::Passed(&user_scripts), base::Passed(&memory)));
26 } 27 }
27 28
28 } // namespace 29 } // namespace
29 30
30 struct WebUIUserScriptLoader::UserScriptRenderInfo { 31 struct WebUIUserScriptLoader::UserScriptRenderInfo {
31 int render_process_id; 32 int render_process_id;
32 int render_view_id; 33 int render_view_id;
34 bool is_incognito;
33 35
34 UserScriptRenderInfo() : render_process_id(-1), render_view_id(-1) {} 36 UserScriptRenderInfo()
37 : render_process_id(-1),
38 render_view_id(-1),
39 is_incognito(false) {}
35 40
36 UserScriptRenderInfo(int render_process_id, int render_view_id) 41 UserScriptRenderInfo(int render_process_id,
37 : render_process_id(render_process_id), render_view_id(render_view_id) {} 42 int render_view_id,
43 bool is_incognito)
44 : render_process_id(render_process_id),
45 render_view_id(render_view_id),
46 is_incognito(is_incognito) {}
38 47
39 bool operator<(const UserScriptRenderInfo& other) { 48 bool operator<(const UserScriptRenderInfo& other) {
40 if (render_process_id != other.render_process_id) 49 if (render_process_id != other.render_process_id)
41 return render_process_id < other.render_process_id; 50 return render_process_id < other.render_process_id;
42 51
43 if (render_view_id != other.render_view_id) 52 if (render_view_id != other.render_view_id)
44 return render_view_id < other.render_view_id; 53 return render_view_id < other.render_view_id;
45 54
55 if (is_incognito != other.is_incognito)
56 return is_incognito == false;
57
46 return false; 58 return false;
47 } 59 }
48 }; 60 };
49 61
50 WebUIUserScriptLoader::WebUIUserScriptLoader( 62 WebUIUserScriptLoader::WebUIUserScriptLoader(
51 content::BrowserContext* browser_context, 63 content::BrowserContext* browser_context,
52 const HostID& host_id) 64 const HostID& host_id)
53 : UserScriptLoader(browser_context, host_id), 65 : UserScriptLoader(browser_context, host_id),
54 complete_fetchers_(0), 66 complete_fetchers_(0),
55 user_scripts_cache_(nullptr), 67 user_scripts_cache_(nullptr),
56 weak_factory_(this) { 68 weak_factory_(this) {
57 SetReady(true); 69 SetReady(true);
58 } 70 }
59 71
60 WebUIUserScriptLoader::~WebUIUserScriptLoader() { 72 WebUIUserScriptLoader::~WebUIUserScriptLoader() {
61 } 73 }
62 74
63 void WebUIUserScriptLoader::AddScripts( 75 void WebUIUserScriptLoader::AddScripts(
64 const std::set<extensions::UserScript>& scripts, 76 const std::set<extensions::UserScript>& scripts,
65 int render_process_id, 77 int render_process_id,
66 int render_view_id) { 78 int render_view_id,
67 UserScriptRenderInfo info(render_process_id, render_view_id); 79 bool is_incognito) {
80 UserScriptRenderInfo info(render_process_id, render_view_id, is_incognito);
68 for (const extensions::UserScript& script : scripts) { 81 for (const extensions::UserScript& script : scripts) {
69 script_render_info_map_.insert( 82 script_render_info_map_.insert(
70 std::pair<int, UserScriptRenderInfo>(script.id(), info)); 83 std::pair<int, UserScriptRenderInfo>(script.id(), info));
71 } 84 }
72 85
73 extensions::UserScriptLoader::AddScripts(scripts); 86 extensions::UserScriptLoader::AddScripts(scripts);
74 } 87 }
75 88
76 void WebUIUserScriptLoader::LoadScripts( 89 void WebUIUserScriptLoader::LoadScripts(
77 scoped_ptr<extensions::UserScriptList> user_scripts, 90 scoped_ptr<extensions::UserScriptList> user_scripts,
78 const std::set<HostID>& changed_hosts, 91 const std::set<HostID>& changed_hosts,
79 const std::set<int>& added_script_ids) { 92 const std::set<int>& added_script_ids) {
80 user_scripts_cache_.swap(user_scripts); 93 user_scripts_cache_.swap(user_scripts);
81 94
82 // The total number of the tasks is used to trace whether all the fetches 95 // 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_| 96 // 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 97 // before we get theis number. Once we get the total number, start each
85 // fetch tasks. 98 // fetch tasks.
86 complete_fetchers_ = 0; 99 complete_fetchers_ = 0;
87 100
88 for (extensions::UserScript& script : *user_scripts_cache_) { 101 for (extensions::UserScript& script : *user_scripts_cache_) {
89 if (added_script_ids.count(script.id()) == 0) 102 if (added_script_ids.count(script.id()) == 0)
90 continue; 103 continue;
91 104
92 auto iter = script_render_info_map_.find(script.id()); 105 auto iter = script_render_info_map_.find(script.id());
93 DCHECK(iter != script_render_info_map_.end()); 106 DCHECK(iter != script_render_info_map_.end());
94 int render_process_id = iter->second.render_process_id; 107 int render_process_id = iter->second.render_process_id;
95 int render_view_id = iter->second.render_view_id; 108 int render_view_id = iter->second.render_view_id;
96 109
97 CreateWebUIURLFetchers(&script.js_scripts(), render_process_id, 110 content::BrowserContext* browser_context = iter->second.is_incognito
Devlin 2015/04/23 17:53:47 Can't we basically replace this whole patch with c
98 render_view_id); 111 ? extensions::ExtensionsBrowserClient::Get()->GetOffTheRecordContext(
99 CreateWebUIURLFetchers(&script.css_scripts(), render_process_id, 112 UserScriptLoader::browser_context())
100 render_view_id); 113 : UserScriptLoader::browser_context();
114 DCHECK(browser_context);
115
116 CreateWebUIURLFetchers(&script.js_scripts(), browser_context,
117 render_process_id, render_view_id);
118 CreateWebUIURLFetchers(&script.css_scripts(), browser_context,
119 render_process_id, render_view_id);
101 120
102 script_render_info_map_.erase(script.id()); 121 script_render_info_map_.erase(script.id());
103 } 122 }
104 123
105 // If no fetch is needed, call OnWebUIURLFetchComplete directly. 124 // If no fetch is needed, call OnWebUIURLFetchComplete directly.
106 if (fetchers_.empty()) { 125 if (fetchers_.empty()) {
107 OnWebUIURLFetchComplete(); 126 OnWebUIURLFetchComplete();
108 return; 127 return;
109 } 128 }
110 for (auto fetcher : fetchers_) 129 for (auto fetcher : fetchers_)
111 fetcher->Start(); 130 fetcher->Start();
112 } 131 }
113 132
114 void WebUIUserScriptLoader::CreateWebUIURLFetchers( 133 void WebUIUserScriptLoader::CreateWebUIURLFetchers(
115 extensions::UserScript::FileList* script_files, 134 extensions::UserScript::FileList* script_files,
135 content::BrowserContext* browser_context,
116 int render_process_id, 136 int render_process_id,
117 int render_view_id) { 137 int render_view_id) {
118 for (extensions::UserScript::File& file : *script_files) { 138 for (extensions::UserScript::File& file : *script_files) {
119 if (file.GetContent().empty()) { 139 if (file.GetContent().empty()) {
120 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the 140 // The WebUIUserScriptLoader owns these WebUIURLFetchers. Once the
121 // loader is destroyed, all the fetchers will be destroyed. Therefore, 141 // loader is destroyed, all the fetchers will be destroyed. Therefore,
122 // we are sure it is safe to use base::Unretained(this) here. 142 // we are sure it is safe to use base::Unretained(this) here.
123 scoped_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher( 143 scoped_ptr<WebUIURLFetcher> fetcher(new WebUIURLFetcher(
124 browser_context(), render_process_id, render_view_id, file.url(), 144 browser_context, render_process_id, render_view_id, file.url(),
125 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete, 145 base::Bind(&WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete,
126 base::Unretained(this), &file))); 146 base::Unretained(this), &file)));
127 fetchers_.push_back(fetcher.release()); 147 fetchers_.push_back(fetcher.release());
128 } 148 }
129 } 149 }
130 } 150 }
131 151
132 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete( 152 void WebUIUserScriptLoader::OnSingleWebUIURLFetchComplete(
133 extensions::UserScript::File* script_file, 153 extensions::UserScript::File* script_file,
134 bool success, 154 bool success,
(...skipping 15 matching lines...) Expand all
150 } 170 }
151 } 171 }
152 172
153 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() { 173 void WebUIUserScriptLoader::OnWebUIURLFetchComplete() {
154 content::BrowserThread::PostTask( 174 content::BrowserThread::PostTask(
155 content::BrowserThread::FILE, FROM_HERE, 175 content::BrowserThread::FILE, FROM_HERE,
156 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_), 176 base::Bind(&SerializeOnFileThread, base::Passed(&user_scripts_cache_),
157 base::Bind(&WebUIUserScriptLoader::OnScriptsLoaded, 177 base::Bind(&WebUIUserScriptLoader::OnScriptsLoaded,
158 weak_factory_.GetWeakPtr()))); 178 weak_factory_.GetWeakPtr())));
159 } 179 }
OLDNEW
« no previous file with comments | « extensions/browser/web_ui_user_script_loader.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698