| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 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/extensions/shared_user_script_master.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/memory/shared_memory.h" |
| 9 #include "chrome/browser/chrome_notification_types.h" |
| 10 #include "chrome/browser/extensions/extension_util.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" |
| 13 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" |
| 14 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/browser/render_process_host.h" |
| 16 #include "extensions/browser/content_verifier.h" |
| 17 #include "extensions/browser/extension_system.h" |
| 18 #include "extensions/common/one_shot_event.h" |
| 19 |
| 20 namespace extensions { |
| 21 |
| 22 SharedUserScriptMaster::SharedUserScriptMaster(Profile* profile) |
| 23 : UserScriptMaster(profile, "" /* owner_extension_id */), |
| 24 user_scripts_(new UserScriptList()), |
| 25 weak_factory_(this), |
| 26 extension_registry_observer_(this) { |
| 27 extension_registry_observer_.Add(ExtensionRegistry::Get(this->profile())); |
| 28 ExtensionSystem::Get(this->profile())->ready().Post( |
| 29 FROM_HERE, |
| 30 base::Bind(&SharedUserScriptMaster::OnExtensionSystemReady, |
| 31 weak_factory_.GetWeakPtr())); |
| 32 } |
| 33 |
| 34 SharedUserScriptMaster::~SharedUserScriptMaster() { |
| 35 } |
| 36 |
| 37 void SharedUserScriptMaster::OnExtensionLoaded( |
| 38 content::BrowserContext* browser_context, |
| 39 const Extension* extension) { |
| 40 added_extensions_.insert(extension->id()); |
| 41 removed_extensions_.erase(extension->id()); |
| 42 extensions_info_[extension->id()] = |
| 43 ExtensionSet::ExtensionPathAndDefaultLocale( |
| 44 extension->path(), LocaleInfo::GetDefaultLocale(extension)); |
| 45 if (extensions_service_ready_) |
| 46 changed_extensions_.insert(extension->id()); |
| 47 } |
| 48 |
| 49 void SharedUserScriptMaster::OnExtensionUnloaded( |
| 50 content::BrowserContext* browser_context, |
| 51 const Extension* extension, |
| 52 UnloadedExtensionInfo::Reason reason) { |
| 53 removed_extensions_.insert(extension->id()); |
| 54 added_extensions_.erase(extension->id()); |
| 55 // Remove any content scripts. |
| 56 extensions_info_.erase(extension->id()); |
| 57 changed_extensions_.insert(extension->id()); |
| 58 AttemptLoad(); |
| 59 } |
| 60 |
| 61 void SharedUserScriptMaster::StartLoad() { |
| 62 // Remove any user scripts belonging to any extension that was updated or |
| 63 // removed. |
| 64 for (UserScriptList::iterator iter = user_scripts_->begin(); |
| 65 iter != user_scripts_->end();) { |
| 66 if (removed_extensions_.count(iter->extension_id()) > 0 || |
| 67 added_extensions_.count(iter->extension_id()) > 0) { |
| 68 iter = user_scripts_->erase(iter); |
| 69 } else { |
| 70 ++iter; |
| 71 } |
| 72 } |
| 73 |
| 74 // Add any content scripts for extensions that were recently loaded. |
| 75 const ExtensionSet& enabled_extensions = |
| 76 ExtensionRegistry::Get(profile())->enabled_extensions(); |
| 77 for (std::set<ExtensionId>::const_iterator iter = added_extensions_.begin(); |
| 78 iter != added_extensions_.end(); |
| 79 ++iter) { |
| 80 const Extension* extension = enabled_extensions.GetByID(*iter); |
| 81 if (!extension) |
| 82 continue; |
| 83 bool incognito_enabled = |
| 84 util::IsIncognitoEnabled(extension->id(), profile()); |
| 85 const UserScriptList& scripts = |
| 86 ContentScriptsInfo::GetContentScripts(extension); |
| 87 for (UserScriptList::const_iterator script = scripts.begin(); |
| 88 script != scripts.end(); |
| 89 ++script) { |
| 90 user_scripts_->push_back(*script); |
| 91 user_scripts_->back().set_incognito_enabled(incognito_enabled); |
| 92 } |
| 93 } |
| 94 |
| 95 LoadScripts(user_scripts_.Pass(), extensions_info_); |
| 96 |
| 97 added_extensions_.clear(); |
| 98 removed_extensions_.clear(); |
| 99 user_scripts_.reset(NULL); |
| 100 } |
| 101 |
| 102 void SharedUserScriptMaster::AcquireUserScripts( |
| 103 scoped_ptr<UserScriptList> scripts) { |
| 104 user_scripts_.reset(scripts.release()); |
| 105 } |
| 106 |
| 107 ExtensionsInfo SharedUserScriptMaster::GetExtensionsInfo() { |
| 108 return extensions_info_; |
| 109 } |
| 110 |
| 111 std::set<ExtensionId> SharedUserScriptMaster::GetAddedExtensions() { |
| 112 return added_extensions_; |
| 113 } |
| 114 |
| 115 std::set<ExtensionId> SharedUserScriptMaster::GetAllManagedExtensions() { |
| 116 // Empty set = all extensions. |
| 117 return std::set<ExtensionId>(); |
| 118 } |
| 119 |
| 120 std::set<ExtensionId> SharedUserScriptMaster::GetChangedExtensions() { |
| 121 return changed_extensions_; |
| 122 } |
| 123 |
| 124 void SharedUserScriptMaster::ResetChangedExtensions() { |
| 125 changed_extensions_.clear(); |
| 126 } |
| 127 |
| 128 void SharedUserScriptMaster::OnExtensionSystemReady() { |
| 129 extensions_service_ready_ = true; |
| 130 AttemptLoad(); |
| 131 } |
| 132 |
| 133 void SharedUserScriptMaster::AttemptLoad() { |
| 134 if (is_loading()) |
| 135 SignalPendingLoad(); |
| 136 else |
| 137 StartLoad(); |
| 138 } |
| 139 |
| 140 } // namespace extensions |
| OLD | NEW |