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

Side by Side Diff: extensions/renderer/user_script_set_manager.cc

Issue 492133002: Renderer changes for wiring up shared memory with declarative injection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 4 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 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/renderer/user_script_set_manager.h" 5 #include "extensions/renderer/user_script_set_manager.h"
6 6
7 #include "content/public/renderer/render_thread.h" 7 #include "content/public/renderer/render_thread.h"
8 #include "extensions/common/extension_messages.h" 8 #include "extensions/common/extension_messages.h"
9 #include "extensions/renderer/dispatcher.h" 9 #include "extensions/renderer/dispatcher.h"
10 #include "extensions/renderer/user_script_set.h" 10 #include "extensions/renderer/user_script_set.h"
(...skipping 12 matching lines...) Expand all
23 } 23 }
24 24
25 void UserScriptSetManager::AddObserver(Observer* observer) { 25 void UserScriptSetManager::AddObserver(Observer* observer) {
26 observers_.AddObserver(observer); 26 observers_.AddObserver(observer);
27 } 27 }
28 28
29 void UserScriptSetManager::RemoveObserver(Observer* observer) { 29 void UserScriptSetManager::RemoveObserver(Observer* observer) {
30 observers_.RemoveObserver(observer); 30 observers_.RemoveObserver(observer);
31 } 31 }
32 32
33 scoped_ptr<ScriptInjection> UserScriptSetManager::GetInjectionForScript(
34 int64 script_id,
35 blink::WebFrame* web_frame,
36 int tab_id,
37 UserScript::RunLocation run_location,
38 const GURL& url,
39 const Extension* extension) {
40 UserScriptSet* user_script_set =
41 GetProgrammaticScriptsByExtension(extension->id());
42 if (!user_script_set)
43 return scoped_ptr<ScriptInjection>();
44
45 return user_script_set->GetInjectionByScriptID(script_id,
46 web_frame,
47 tab_id,
48 run_location,
49 url,
50 extension);
51 }
52
33 bool UserScriptSetManager::OnControlMessageReceived( 53 bool UserScriptSetManager::OnControlMessageReceived(
34 const IPC::Message& message) { 54 const IPC::Message& message) {
35 bool handled = true; 55 bool handled = true;
36 IPC_BEGIN_MESSAGE_MAP(UserScriptSetManager, message) 56 IPC_BEGIN_MESSAGE_MAP(UserScriptSetManager, message)
37 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts) 57 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts)
38 IPC_MESSAGE_UNHANDLED(handled = false) 58 IPC_MESSAGE_UNHANDLED(handled = false)
39 IPC_END_MESSAGE_MAP() 59 IPC_END_MESSAGE_MAP()
40 return handled; 60 return handled;
41 } 61 }
42 62
43 const UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByExtension(
44 const ExtensionId& extension_id) {
45 UserScriptSetMap::const_iterator it =
46 programmatic_scripts_.find(extension_id);
47 return it != programmatic_scripts_.end() ? it->second.get() : NULL;
48 }
49
50 void UserScriptSetManager::GetAllInjections( 63 void UserScriptSetManager::GetAllInjections(
51 ScopedVector<ScriptInjection>* injections, 64 ScopedVector<ScriptInjection>* injections,
52 blink::WebFrame* web_frame, 65 blink::WebFrame* web_frame,
53 int tab_id, 66 int tab_id,
54 UserScript::RunLocation run_location) { 67 UserScript::RunLocation run_location) {
55 static_scripts_.GetInjections(injections, web_frame, tab_id, run_location); 68 static_scripts_.GetInjections(injections, web_frame, tab_id, run_location);
56 for (UserScriptSetMap::iterator it = programmatic_scripts_.begin(); 69 for (UserScriptSetMap::iterator it = programmatic_scripts_.begin();
57 it != programmatic_scripts_.end(); 70 it != programmatic_scripts_.end();
58 ++it) { 71 ++it) {
59 it->second->GetInjections(injections, web_frame, tab_id, run_location); 72 it->second->GetInjections(injections, web_frame, tab_id, run_location);
60 } 73 }
61 } 74 }
62 75
63 void UserScriptSetManager::GetAllActiveExtensionIds( 76 void UserScriptSetManager::GetAllActiveExtensionIds(
64 std::set<std::string>* ids) const { 77 std::set<std::string>* ids) const {
65 DCHECK(ids); 78 DCHECK(ids);
66 static_scripts_.GetActiveExtensionIds(ids); 79 static_scripts_.GetActiveExtensionIds(ids);
67 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin(); 80 for (UserScriptSetMap::const_iterator it = programmatic_scripts_.begin();
68 it != programmatic_scripts_.end(); 81 it != programmatic_scripts_.end();
69 ++it) { 82 ++it) {
70 it->second->GetActiveExtensionIds(ids); 83 it->second->GetActiveExtensionIds(ids);
71 } 84 }
72 } 85 }
73 86
87 UserScriptSet* UserScriptSetManager::GetProgrammaticScriptsByExtension(
88 const ExtensionId& extension_id) {
89 UserScriptSetMap::const_iterator it =
90 programmatic_scripts_.find(extension_id);
91 return it != programmatic_scripts_.end() ? it->second.get() : NULL;
92 }
93
74 void UserScriptSetManager::OnUpdateUserScripts( 94 void UserScriptSetManager::OnUpdateUserScripts(
75 base::SharedMemoryHandle shared_memory, 95 base::SharedMemoryHandle shared_memory,
76 const ExtensionId& extension_id, 96 const ExtensionId& extension_id,
77 const std::set<std::string>& changed_extensions) { 97 const std::set<std::string>& changed_extensions) {
78 if (!base::SharedMemory::IsHandleValid(shared_memory)) { 98 if (!base::SharedMemory::IsHandleValid(shared_memory)) {
79 NOTREACHED() << "Bad scripts handle"; 99 NOTREACHED() << "Bad scripts handle";
80 return; 100 return;
81 } 101 }
82 102
83 for (std::set<std::string>::const_iterator iter = changed_extensions.begin(); 103 for (std::set<std::string>::const_iterator iter = changed_extensions.begin();
84 iter != changed_extensions.end(); 104 iter != changed_extensions.end();
85 ++iter) { 105 ++iter) {
86 if (!Extension::IdIsValid(*iter)) { 106 if (!Extension::IdIsValid(*iter)) {
87 NOTREACHED() << "Invalid extension id: " << *iter; 107 NOTREACHED() << "Invalid extension id: " << *iter;
88 return; 108 return;
89 } 109 }
90 } 110 }
91 111
92 UserScriptSet* scripts = NULL; 112 UserScriptSet* scripts = NULL;
93 if (!extension_id.empty()) { 113 if (!extension_id.empty()) {
94 // The expectation when there is an extensions that "owns" this shared
95 // memory region is that it will list itself as the only changed extension.
96 CHECK(changed_extensions.size() == 1 &&
97 changed_extensions.find(extension_id) != changed_extensions.end());
98 if (programmatic_scripts_.find(extension_id) == 114 if (programmatic_scripts_.find(extension_id) ==
99 programmatic_scripts_.end()) { 115 programmatic_scripts_.end()) {
100 scripts = new UserScriptSet(extensions_); 116 scripts = new UserScriptSet(extensions_);
101 programmatic_scripts_[extension_id] = make_linked_ptr(scripts); 117 programmatic_scripts_[extension_id] = make_linked_ptr(scripts);
102 } else { 118 } else {
103 scripts = programmatic_scripts_[extension_id].get(); 119 scripts = programmatic_scripts_[extension_id].get();
104 } 120 }
105 } else { 121 } else {
106 scripts = &static_scripts_; 122 scripts = &static_scripts_;
107 } 123 }
108 DCHECK(scripts); 124 DCHECK(scripts);
109 125
110 // If no extensions are included in the set, that indicates that all 126 // If no extensions are included in the set, that indicates that all
111 // extensions were updated. Add them all to the set so that observers and 127 // extensions were updated. Add them all to the set so that observers and
112 // individual UserScriptSets don't need to know this detail. 128 // individual UserScriptSets don't need to know this detail.
113 const std::set<std::string>* effective_extensions = &changed_extensions; 129 const std::set<std::string>* effective_extensions = &changed_extensions;
114 std::set<std::string> all_extensions; 130 std::set<std::string> all_extensions;
115 if (changed_extensions.empty()) { 131 if (changed_extensions.empty()) {
116 all_extensions = extensions_->GetIDs(); 132 // The meaning of "all extensions" varies, depending on whether some
133 // extension "owns" this shared memory region.
134 // No owner => all known extensions.
135 // Owner => just the owner extension.
136 if (extension_id.empty())
137 all_extensions = extensions_->GetIDs();
138 else
139 all_extensions.insert(extension_id);
117 effective_extensions = &all_extensions; 140 effective_extensions = &all_extensions;
118 } 141 }
119 142
120 if (scripts->UpdateUserScripts(shared_memory, *effective_extensions)) { 143 if (scripts->UpdateUserScripts(shared_memory, *effective_extensions)) {
121 FOR_EACH_OBSERVER( 144 FOR_EACH_OBSERVER(
122 Observer, 145 Observer,
123 observers_, 146 observers_,
124 OnUserScriptsUpdated(*effective_extensions, scripts->scripts())); 147 OnUserScriptsUpdated(*effective_extensions, scripts->scripts()));
125 } 148 }
126 } 149 }
127 150
128 } // namespace extensions 151 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698