| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 "chrome/browser/chromeos/accessibility/accessibility_extension_loader.h
" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/callback_helpers.h" |
| 9 #include "base/path_service.h" |
| 10 #include "chrome/browser/chromeos/login/lock/screen_locker.h" |
| 11 #include "chrome/browser/chromeos/login/ui/login_display_host.h" |
| 12 #include "chrome/browser/chromeos/login/ui/webui_login_view.h" |
| 13 #include "chrome/browser/chromeos/profiles/profile_helper.h" |
| 14 #include "chrome/browser/extensions/component_loader.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/extensions/tab_helper.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/common/extensions/extension_constants.h" |
| 19 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" |
| 20 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/render_view_host.h" |
| 22 #include "content/public/browser/web_contents.h" |
| 23 #include "extensions/browser/extension_api_frame_id_map.h" |
| 24 #include "extensions/browser/extension_registry.h" |
| 25 #include "extensions/browser/extension_system.h" |
| 26 #include "extensions/browser/file_reader.h" |
| 27 #include "extensions/browser/script_executor.h" |
| 28 #include "extensions/common/extension.h" |
| 29 #include "extensions/common/extension_messages.h" |
| 30 #include "extensions/common/extension_resource.h" |
| 31 |
| 32 namespace chromeos { |
| 33 |
| 34 namespace { |
| 35 |
| 36 // Uses the ScriptExecutor associated with the given |render_view_host| to |
| 37 // execute the given |code|. |
| 38 void ExecuteScriptHelper(content::RenderViewHost* render_view_host, |
| 39 const std::string& code, |
| 40 const std::string& extension_id) { |
| 41 content::WebContents* web_contents = |
| 42 content::WebContents::FromRenderViewHost(render_view_host); |
| 43 if (!web_contents) |
| 44 return; |
| 45 if (!extensions::TabHelper::FromWebContents(web_contents)) |
| 46 extensions::TabHelper::CreateForWebContents(web_contents); |
| 47 extensions::TabHelper::FromWebContents(web_contents) |
| 48 ->script_executor() |
| 49 ->ExecuteScript(HostID(HostID::EXTENSIONS, extension_id), |
| 50 extensions::ScriptExecutor::JAVASCRIPT, code, |
| 51 extensions::ScriptExecutor::INCLUDE_SUB_FRAMES, |
| 52 extensions::ExtensionApiFrameIdMap::kTopFrameId, |
| 53 extensions::ScriptExecutor::DONT_MATCH_ABOUT_BLANK, |
| 54 extensions::UserScript::DOCUMENT_IDLE, |
| 55 extensions::ScriptExecutor::ISOLATED_WORLD, |
| 56 extensions::ScriptExecutor::DEFAULT_PROCESS, |
| 57 GURL(), // No webview src. |
| 58 GURL(), // No file url. |
| 59 false, // Not user gesture. |
| 60 extensions::ScriptExecutor::NO_RESULT, |
| 61 extensions::ScriptExecutor::ExecuteScriptCallback()); |
| 62 } |
| 63 |
| 64 // Helper class that directly loads an extension's content scripts into |
| 65 // all of the frames corresponding to a given RenderViewHost. |
| 66 class ContentScriptLoader { |
| 67 public: |
| 68 // Initialize the ContentScriptLoader with the ID of the extension |
| 69 // and the RenderViewHost where the scripts should be loaded. |
| 70 ContentScriptLoader(const std::string& extension_id, |
| 71 int render_process_id, |
| 72 int render_view_id) |
| 73 : extension_id_(extension_id), |
| 74 render_process_id_(render_process_id), |
| 75 render_view_id_(render_view_id) {} |
| 76 |
| 77 // Call this once with the ExtensionResource corresponding to each |
| 78 // content script to be loaded. |
| 79 void AppendScript(extensions::ExtensionResource resource) { |
| 80 resources_.push(resource); |
| 81 } |
| 82 |
| 83 // Finally, call this method once to fetch all of the resources and |
| 84 // load them. This method will delete this object when done. |
| 85 void Run() { |
| 86 if (resources_.empty()) { |
| 87 delete this; |
| 88 return; |
| 89 } |
| 90 |
| 91 extensions::ExtensionResource resource = resources_.front(); |
| 92 resources_.pop(); |
| 93 scoped_refptr<FileReader> reader( |
| 94 new FileReader(resource, base::Bind(&ContentScriptLoader::OnFileLoaded, |
| 95 base::Unretained(this)))); |
| 96 reader->Start(); |
| 97 } |
| 98 |
| 99 private: |
| 100 void OnFileLoaded(bool success, std::unique_ptr<std::string> data) { |
| 101 if (success) { |
| 102 content::RenderViewHost* render_view_host = |
| 103 content::RenderViewHost::FromID(render_process_id_, render_view_id_); |
| 104 if (render_view_host) |
| 105 ExecuteScriptHelper(render_view_host, *data, extension_id_); |
| 106 } |
| 107 Run(); |
| 108 } |
| 109 |
| 110 std::string extension_id_; |
| 111 int render_process_id_; |
| 112 int render_view_id_; |
| 113 std::queue<extensions::ExtensionResource> resources_; |
| 114 }; |
| 115 |
| 116 } // namespace |
| 117 |
| 118 AccessibilityExtensionLoader::AccessibilityExtensionLoader( |
| 119 const std::string& extension_id, |
| 120 const base::FilePath& extension_path, |
| 121 const base::Closure& unload_callback) |
| 122 : profile_(nullptr), |
| 123 extension_id_(extension_id), |
| 124 extension_path_(extension_path), |
| 125 loaded_on_lock_screen_(false), |
| 126 loaded_on_user_screen_(false), |
| 127 unload_callback_(unload_callback), |
| 128 weak_ptr_factory_(this) {} |
| 129 |
| 130 AccessibilityExtensionLoader::~AccessibilityExtensionLoader() {} |
| 131 |
| 132 void AccessibilityExtensionLoader::SetProfile(Profile* profile) { |
| 133 profile_ = profile; |
| 134 |
| 135 if (!loaded_on_user_screen_ && !loaded_on_lock_screen_) |
| 136 return; |
| 137 |
| 138 // If the extension was already enabled, but not for this profile, add it |
| 139 // to this profile. |
| 140 auto* extension_service = |
| 141 extensions::ExtensionSystem::Get(profile_)->extension_service(); |
| 142 auto* component_loader = extension_service->component_loader(); |
| 143 if (!component_loader->Exists(extension_id_)) |
| 144 LoadExtension(profile_, nullptr, base::Closure()); |
| 145 } |
| 146 |
| 147 void AccessibilityExtensionLoader::Load(Profile* profile, |
| 148 const std::string& init_script_str, |
| 149 const base::Closure& done_cb) { |
| 150 profile_ = profile; |
| 151 init_script_str_ = init_script_str; |
| 152 ScreenLocker* screen_locker = ScreenLocker::default_screen_locker(); |
| 153 if (screen_locker && screen_locker->locked()) { |
| 154 // If on the lock screen, loads only to the lock screen as for |
| 155 // now. On unlock, it will be loaded to the user screen. |
| 156 // (see. AccessibilityExtensionLoader::Observe()) |
| 157 LoadToLockScreen(done_cb); |
| 158 } else { |
| 159 LoadToUserScreen(done_cb); |
| 160 } |
| 161 } |
| 162 |
| 163 void AccessibilityExtensionLoader::Unload() { |
| 164 if (loaded_on_lock_screen_) |
| 165 UnloadFromLockScreen(); |
| 166 |
| 167 if (loaded_on_user_screen_) { |
| 168 UnloadExtensionFromProfile(profile_); |
| 169 loaded_on_user_screen_ = false; |
| 170 } |
| 171 |
| 172 profile_ = nullptr; |
| 173 |
| 174 if (unload_callback_) |
| 175 unload_callback_.Run(); |
| 176 } |
| 177 |
| 178 void AccessibilityExtensionLoader::LoadToUserScreen( |
| 179 const base::Closure& done_cb) { |
| 180 if (loaded_on_user_screen_) |
| 181 return; |
| 182 |
| 183 // Determine whether an OOBE screen is currently being shown. If so, |
| 184 // the extension will be injected directly into that screen. |
| 185 content::WebUI* login_web_ui = nullptr; |
| 186 |
| 187 if (ProfileHelper::IsSigninProfile(profile_)) { |
| 188 LoginDisplayHost* login_display_host = LoginDisplayHost::default_host(); |
| 189 if (login_display_host) { |
| 190 WebUILoginView* web_ui_login_view = |
| 191 login_display_host->GetWebUILoginView(); |
| 192 if (web_ui_login_view) |
| 193 login_web_ui = web_ui_login_view->GetWebUI(); |
| 194 } |
| 195 |
| 196 // Lock screen uses the signin progile. |
| 197 loaded_on_lock_screen_ = true; |
| 198 } |
| 199 |
| 200 loaded_on_user_screen_ = true; |
| 201 LoadExtension(profile_, |
| 202 login_web_ui |
| 203 ? login_web_ui->GetWebContents()->GetRenderViewHost() |
| 204 : nullptr, |
| 205 done_cb); |
| 206 } |
| 207 |
| 208 void AccessibilityExtensionLoader::LoadToLockScreen( |
| 209 const base::Closure& done_cb) { |
| 210 if (loaded_on_lock_screen_) |
| 211 return; |
| 212 |
| 213 ScreenLocker* screen_locker = ScreenLocker::default_screen_locker(); |
| 214 if (screen_locker && screen_locker->locked()) { |
| 215 content::WebUI* lock_web_ui = screen_locker->GetAssociatedWebUI(); |
| 216 if (lock_web_ui) { |
| 217 Profile* profile = Profile::FromWebUI(lock_web_ui); |
| 218 loaded_on_lock_screen_ = true; |
| 219 LoadExtension(profile, lock_web_ui->GetWebContents()->GetRenderViewHost(), |
| 220 done_cb); |
| 221 } |
| 222 } |
| 223 } |
| 224 |
| 225 // |
| 226 // private |
| 227 // |
| 228 |
| 229 void AccessibilityExtensionLoader::UnloadExtensionFromProfile( |
| 230 Profile* profile) { |
| 231 ExtensionService* extension_service = |
| 232 extensions::ExtensionSystem::Get(profile)->extension_service(); |
| 233 extension_service->component_loader()->Remove(extension_path_); |
| 234 } |
| 235 |
| 236 void AccessibilityExtensionLoader::UnloadFromLockScreen() { |
| 237 // Lock screen uses the signin progile. |
| 238 Profile* signin_profile = ProfileHelper::GetSigninProfile(); |
| 239 UnloadExtensionFromProfile(signin_profile); |
| 240 loaded_on_lock_screen_ = false; |
| 241 } |
| 242 |
| 243 void AccessibilityExtensionLoader::InjectContentScriptAndCallback( |
| 244 ExtensionService* extension_service, |
| 245 int render_process_id, |
| 246 int render_view_id, |
| 247 const base::Closure& done_cb) { |
| 248 // Make sure to always run |done_cb|. The extension was loaded even |
| 249 // if we end up not injecting into this particular render view. |
| 250 base::ScopedClosureRunner done_runner(done_cb); |
| 251 content::RenderViewHost* render_view_host = |
| 252 content::RenderViewHost::FromID(render_process_id, render_view_id); |
| 253 if (!render_view_host) |
| 254 return; |
| 255 const content::WebContents* web_contents = |
| 256 content::WebContents::FromRenderViewHost(render_view_host); |
| 257 GURL content_url; |
| 258 if (web_contents) |
| 259 content_url = web_contents->GetLastCommittedURL(); |
| 260 const extensions::Extension* extension = |
| 261 extensions::ExtensionRegistry::Get(extension_service->profile()) |
| 262 ->enabled_extensions() |
| 263 .GetByID(extension_id_); |
| 264 |
| 265 if (!init_script_str_.empty()) { |
| 266 ExecuteScriptHelper(render_view_host, init_script_str_, extension->id()); |
| 267 } |
| 268 |
| 269 // Inject the content scripts. |
| 270 ContentScriptLoader* loader = new ContentScriptLoader( |
| 271 extension->id(), render_view_host->GetProcess()->GetID(), |
| 272 render_view_host->GetRoutingID()); |
| 273 |
| 274 const extensions::UserScriptList& content_scripts = |
| 275 extensions::ContentScriptsInfo::GetContentScripts(extension); |
| 276 for (const std::unique_ptr<extensions::UserScript>& script : |
| 277 content_scripts) { |
| 278 if (web_contents && !script->MatchesURL(content_url)) |
| 279 continue; |
| 280 for (const std::unique_ptr<extensions::UserScript::File>& file : |
| 281 script->js_scripts()) { |
| 282 extensions::ExtensionResource resource = |
| 283 extension->GetResource(file->relative_path()); |
| 284 loader->AppendScript(resource); |
| 285 } |
| 286 } |
| 287 loader->Run(); // It cleans itself up when done. |
| 288 } |
| 289 |
| 290 void AccessibilityExtensionLoader::LoadExtension( |
| 291 Profile* profile, |
| 292 content::RenderViewHost* render_view_host, |
| 293 base::Closure done_cb) { |
| 294 ExtensionService* extension_service = |
| 295 extensions::ExtensionSystem::Get(profile)->extension_service(); |
| 296 if (render_view_host) { |
| 297 // Wrap the passed in callback to inject the content script. |
| 298 done_cb = base::Bind( |
| 299 &AccessibilityExtensionLoader::InjectContentScriptAndCallback, |
| 300 weak_ptr_factory_.GetWeakPtr(), extension_service, |
| 301 render_view_host->GetProcess()->GetID(), |
| 302 render_view_host->GetRoutingID(), done_cb); |
| 303 } |
| 304 |
| 305 extension_service->component_loader()->AddComponentFromDir( |
| 306 extension_path_, extension_id_.c_str(), done_cb); |
| 307 } |
| 308 |
| 309 } // namespace chromeos |
| OLD | NEW |