Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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/declarative_user_script_master.h" | |
| 6 | |
| 7 #include "chrome/browser/chrome_notification_types.h" | |
| 8 #include "chrome/browser/extensions/extension_util.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" | |
| 11 #include "content/public/browser/notification_service.h" | |
| 12 #include "content/public/browser/render_process_host.h" | |
| 13 #include "extensions/browser/content_verifier.h" | |
| 14 #include "extensions/browser/extension_system.h" | |
| 15 | |
| 16 namespace extensions { | |
| 17 | |
| 18 DeclarativeUserScriptMaster::DeclarativeUserScriptMaster( | |
| 19 Profile* profile, | |
| 20 const ExtensionId& extension_id) | |
| 21 : UserScriptMaster(profile) { | |
| 22 owner_extension_id_ = extension_id; | |
|
Devlin
2014/07/30 21:20:38
init this in the initialization list.
Mark Dittmer
2014/07/31 18:37:33
Done.
| |
| 23 registrar_.Add(this, | |
| 24 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | |
| 25 content::NotificationService::AllBrowserContextsAndSources()); | |
| 26 } | |
| 27 | |
| 28 DeclarativeUserScriptMaster::~DeclarativeUserScriptMaster() { | |
| 29 } | |
| 30 | |
| 31 void DeclarativeUserScriptMaster::Observe( | |
| 32 int type, | |
| 33 const content::NotificationSource& source, | |
| 34 const content::NotificationDetails& details) { | |
| 35 switch (type) { | |
|
Devlin
2014/07/30 21:20:38
If you only expect one notification, it's cleaner
Mark Dittmer
2014/07/31 18:37:33
Done.
| |
| 36 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { | |
| 37 content::RenderProcessHost* process = | |
| 38 content::Source<content::RenderProcessHost>(source).ptr(); | |
| 39 Profile* profile = | |
| 40 Profile::FromBrowserContext(process->GetBrowserContext()); | |
| 41 if (!profile_->IsSameProfile(profile)) | |
| 42 return; | |
| 43 if (ScriptsReady()) { | |
| 44 std::set<ExtensionId> changed_extensions; | |
| 45 changed_extensions.insert(owner_extension_id_); | |
| 46 SendUpdate(process, GetSharedMemory(), changed_extensions); | |
| 47 } | |
| 48 break; | |
| 49 } | |
| 50 default: | |
| 51 DCHECK(false); | |
|
Devlin
2014/07/30 21:20:38
We usually spell this NOTREACHED(). (though moot w
Mark Dittmer
2014/07/31 18:37:33
Acknowledged.
| |
| 52 } | |
| 53 } | |
| 54 | |
| 55 void DeclarativeUserScriptMaster::AddScript(const UserScript& script) { | |
| 56 scripts_to_add_.insert(script); | |
| 57 if (is_loading()) { | |
|
Devlin
2014/07/30 21:20:38
if (IsSingleLineIfStatement())
RemoveBrackets();
| |
| 58 ResetPendingLoad(); | |
| 59 } else { | |
| 60 StartLoad(); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void DeclarativeUserScriptMaster::RemoveScript(const UserScript& script) { | |
| 65 if (scripts_to_add_.find(script) != scripts_to_add_.end()) { | |
|
Devlin
2014/07/30 21:20:38
brackets
Devlin
2014/07/30 21:20:38
For sets, prefer:
if (scripts_to_add_.count(script
Mark Dittmer
2014/07/31 18:37:33
Done.
| |
| 66 scripts_to_add_.erase(script); | |
| 67 } else { | |
| 68 scripts_to_remove_.insert(script); | |
| 69 } | |
| 70 if (is_loading()) { | |
| 71 ResetPendingLoad(); | |
| 72 } else { | |
| 73 StartLoad(); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 void DeclarativeUserScriptMaster::ClearScripts() { | |
| 78 clear_scripts_ = true; | |
| 79 scripts_to_add_.clear(); | |
| 80 scripts_to_remove_.clear(); | |
| 81 if (is_loading()) { | |
| 82 ResetPendingLoad(); | |
| 83 } else { | |
| 84 StartLoad(); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void DeclarativeUserScriptMaster::StartLoad() { | |
| 89 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 90 DCHECK(!is_loading()); | |
| 91 | |
| 92 if (clear_scripts_) { | |
| 93 user_scripts_->clear(); | |
| 94 } | |
| 95 bool incognito_enabled = | |
| 96 util::IsIncognitoEnabled(owner_extension_id_, profile_); | |
| 97 std::set<UserScript> new_user_scripts; | |
| 98 new_user_scripts.insert(user_scripts_->begin(), user_scripts_->end()); | |
| 99 new_user_scripts.erase(scripts_to_remove_.begin(), scripts_to_remove_.end()); | |
|
Mark Dittmer
2014/07/31 18:37:32
This is wrong because std::set::erase cannot take
| |
| 100 for (std::set<UserScript>::const_iterator it = scripts_to_add_.begin(); | |
| 101 it != scripts_to_add_.end(); | |
| 102 ++it) { | |
| 103 UserScript script = *it; | |
| 104 script.set_incognito_enabled(incognito_enabled); | |
| 105 new_user_scripts.insert(script); | |
| 106 } | |
| 107 user_scripts_->assign(new_user_scripts.begin(), new_user_scripts.end()); | |
| 108 | |
| 109 content::BrowserThread::PostTask( | |
| 110 content::BrowserThread::FILE, | |
| 111 FROM_HERE, | |
| 112 base::Bind(&UserScriptMaster::LoadScriptsOnFileThread, | |
| 113 base::Passed(&user_scripts_), | |
| 114 extensions_info_, | |
| 115 added_extensions_, | |
| 116 make_scoped_refptr( | |
| 117 ExtensionSystem::Get(profile_)->content_verifier()), | |
| 118 base::Bind(&UserScriptMaster::OnScriptsLoaded, | |
| 119 weak_factory_.GetWeakPtr()))); | |
| 120 | |
| 121 // File thread handler has ownership of user_scripts_ until it is given back | |
| 122 // in UserScriptMaster::OnScriptsLoaded(). | |
| 123 user_scripts_.reset(NULL); | |
| 124 | |
| 125 ResetPendingLoadData(); | |
| 126 } | |
| 127 | |
| 128 } // namespace extensions | |
| OLD | NEW |