OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "chrome/browser/extensions/user_script_listener.h" | 5 #include "chrome/browser/extensions/user_script_listener.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "chrome/browser/chrome_thread.h" |
8 #include "chrome/browser/extensions/extensions_service.h" | 8 #include "chrome/browser/extensions/extensions_service.h" |
9 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" | 9 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" |
10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
11 #include "chrome/common/notification_service.h" | 11 #include "chrome/common/notification_service.h" |
12 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
13 | 13 |
14 UserScriptListener::UserScriptListener(MessageLoop* ui_loop, | 14 UserScriptListener::UserScriptListener(ResourceDispatcherHost* rdh) |
15 MessageLoop* io_loop, | 15 : resource_dispatcher_host_(rdh), |
16 ResourceDispatcherHost* rdh) | |
17 : ui_loop_(ui_loop), | |
18 io_loop_(io_loop), | |
19 resource_dispatcher_host_(rdh), | |
20 user_scripts_ready_(false) { | 16 user_scripts_ready_(false) { |
21 DCHECK(ui_loop_); | |
22 DCHECK_EQ(ui_loop_, MessageLoop::current()); | |
23 DCHECK(resource_dispatcher_host_); | 17 DCHECK(resource_dispatcher_host_); |
24 | 18 |
25 // IO loop can be NULL in unit tests. | |
26 if (!io_loop_) | |
27 io_loop_ = ui_loop; | |
28 | |
29 registrar_.Add(this, NotificationType::EXTENSION_LOADED, | 19 registrar_.Add(this, NotificationType::EXTENSION_LOADED, |
30 NotificationService::AllSources()); | 20 NotificationService::AllSources()); |
31 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, | 21 registrar_.Add(this, NotificationType::EXTENSION_UNLOADED, |
32 NotificationService::AllSources()); | 22 NotificationService::AllSources()); |
33 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, | 23 registrar_.Add(this, NotificationType::USER_SCRIPTS_UPDATED, |
34 NotificationService::AllSources()); | 24 NotificationService::AllSources()); |
35 } | 25 } |
36 | 26 |
37 bool UserScriptListener::ShouldStartRequest(URLRequest* request) { | 27 bool UserScriptListener::ShouldStartRequest(URLRequest* request) { |
38 DCHECK_EQ(io_loop_, MessageLoop::current()); | 28 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
39 | 29 |
40 // If it's a frame load, then we need to check the URL against the list of | 30 // If it's a frame load, then we need to check the URL against the list of |
41 // user scripts to see if we need to wait. | 31 // user scripts to see if we need to wait. |
42 ResourceDispatcherHostRequestInfo* info = | 32 ResourceDispatcherHostRequestInfo* info = |
43 ResourceDispatcherHost::InfoForRequest(request); | 33 ResourceDispatcherHost::InfoForRequest(request); |
44 DCHECK(info); | 34 DCHECK(info); |
45 | 35 |
46 if (info->resource_type() != ResourceType::MAIN_FRAME && | 36 if (info->resource_type() != ResourceType::MAIN_FRAME && |
47 info->resource_type() != ResourceType::SUB_FRAME) { | 37 info->resource_type() != ResourceType::SUB_FRAME) { |
48 return true; | 38 return true; |
(...skipping 16 matching lines...) Expand all Loading... |
65 if (!found_match) | 55 if (!found_match) |
66 return true; | 56 return true; |
67 | 57 |
68 // Queue this request up. | 58 // Queue this request up. |
69 delayed_request_ids_.push_front(ResourceDispatcherHost::GlobalRequestID( | 59 delayed_request_ids_.push_front(ResourceDispatcherHost::GlobalRequestID( |
70 info->child_id(), info->request_id())); | 60 info->child_id(), info->request_id())); |
71 return false; | 61 return false; |
72 } | 62 } |
73 | 63 |
74 void UserScriptListener::StartDelayedRequests() { | 64 void UserScriptListener::StartDelayedRequests() { |
75 DCHECK_EQ(io_loop_, MessageLoop::current()); | 65 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
76 | 66 |
77 user_scripts_ready_ = true; | 67 user_scripts_ready_ = true; |
78 | 68 |
79 if (resource_dispatcher_host_) { | 69 if (resource_dispatcher_host_) { |
80 for (DelayedRequests::iterator it = delayed_request_ids_.begin(); | 70 for (DelayedRequests::iterator it = delayed_request_ids_.begin(); |
81 it != delayed_request_ids_.end(); ++it) { | 71 it != delayed_request_ids_.end(); ++it) { |
82 URLRequest* request = resource_dispatcher_host_->GetURLRequest(*it); | 72 URLRequest* request = resource_dispatcher_host_->GetURLRequest(*it); |
83 if (request) { | 73 if (request) { |
84 // The request shouldn't have started (SUCCESS is the initial state). | 74 // The request shouldn't have started (SUCCESS is the initial state). |
85 DCHECK(request->status().status() == URLRequestStatus::SUCCESS); | 75 DCHECK(request->status().status() == URLRequestStatus::SUCCESS); |
86 request->Start(); | 76 request->Start(); |
87 } | 77 } |
88 } | 78 } |
89 } | 79 } |
90 | 80 |
91 delayed_request_ids_.clear(); | 81 delayed_request_ids_.clear(); |
92 } | 82 } |
93 | 83 |
94 void UserScriptListener::AppendNewURLPatterns(const URLPatterns& new_patterns) { | 84 void UserScriptListener::AppendNewURLPatterns(const URLPatterns& new_patterns) { |
95 DCHECK_EQ(io_loop_, MessageLoop::current()); | 85 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
96 | 86 |
97 user_scripts_ready_ = false; | 87 user_scripts_ready_ = false; |
98 url_patterns_.insert(url_patterns_.end(), | 88 url_patterns_.insert(url_patterns_.end(), |
99 new_patterns.begin(), new_patterns.end()); | 89 new_patterns.begin(), new_patterns.end()); |
100 } | 90 } |
101 | 91 |
102 void UserScriptListener::ReplaceURLPatterns(const URLPatterns& patterns) { | 92 void UserScriptListener::ReplaceURLPatterns(const URLPatterns& patterns) { |
103 DCHECK_EQ(io_loop_, MessageLoop::current()); | 93 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
104 url_patterns_ = patterns; | 94 url_patterns_ = patterns; |
105 } | 95 } |
106 | 96 |
107 void UserScriptListener::CollectURLPatterns(Extension* extension, | 97 void UserScriptListener::CollectURLPatterns(Extension* extension, |
108 URLPatterns* patterns) { | 98 URLPatterns* patterns) { |
109 DCHECK_EQ(ui_loop_, MessageLoop::current()); | 99 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
110 | 100 |
111 const UserScriptList& scripts = extension->content_scripts(); | 101 const UserScriptList& scripts = extension->content_scripts(); |
112 for (UserScriptList::const_iterator iter = scripts.begin(); | 102 for (UserScriptList::const_iterator iter = scripts.begin(); |
113 iter != scripts.end(); ++iter) { | 103 iter != scripts.end(); ++iter) { |
114 patterns->insert(patterns->end(), | 104 patterns->insert(patterns->end(), |
115 (*iter).url_patterns().begin(), | 105 (*iter).url_patterns().begin(), |
116 (*iter).url_patterns().end()); | 106 (*iter).url_patterns().end()); |
117 } | 107 } |
118 } | 108 } |
119 | 109 |
120 void UserScriptListener::Observe(NotificationType type, | 110 void UserScriptListener::Observe(NotificationType type, |
121 const NotificationSource& source, | 111 const NotificationSource& source, |
122 const NotificationDetails& details) { | 112 const NotificationDetails& details) { |
123 DCHECK_EQ(ui_loop_, MessageLoop::current()); | 113 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); |
124 | 114 |
125 switch (type.value) { | 115 switch (type.value) { |
126 case NotificationType::EXTENSION_LOADED: { | 116 case NotificationType::EXTENSION_LOADED: { |
127 Extension* extension = Details<Extension>(details).ptr(); | 117 Extension* extension = Details<Extension>(details).ptr(); |
128 if (extension->content_scripts().empty()) | 118 if (extension->content_scripts().empty()) |
129 return; // no new patterns from this extension. | 119 return; // no new patterns from this extension. |
130 | 120 |
131 URLPatterns new_patterns; | 121 URLPatterns new_patterns; |
132 CollectURLPatterns(Details<Extension>(details).ptr(), &new_patterns); | 122 CollectURLPatterns(Details<Extension>(details).ptr(), &new_patterns); |
133 if (!new_patterns.empty()) { | 123 if (!new_patterns.empty()) { |
134 io_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, | 124 ChromeThread::PostTask( |
135 &UserScriptListener::AppendNewURLPatterns, new_patterns)); | 125 ChromeThread::IO, FROM_HERE, |
| 126 NewRunnableMethod( |
| 127 this, &UserScriptListener::AppendNewURLPatterns, new_patterns)); |
136 } | 128 } |
137 break; | 129 break; |
138 } | 130 } |
139 | 131 |
140 case NotificationType::EXTENSION_UNLOADED: { | 132 case NotificationType::EXTENSION_UNLOADED: { |
141 Extension* unloaded_extension = Details<Extension>(details).ptr(); | 133 Extension* unloaded_extension = Details<Extension>(details).ptr(); |
142 if (unloaded_extension->content_scripts().empty()) | 134 if (unloaded_extension->content_scripts().empty()) |
143 return; // no patterns to delete for this extension. | 135 return; // no patterns to delete for this extension. |
144 | 136 |
145 // Clear all our patterns and reregister all the still-loaded extensions. | 137 // Clear all our patterns and reregister all the still-loaded extensions. |
146 URLPatterns new_patterns; | 138 URLPatterns new_patterns; |
147 ExtensionsService* service = Source<ExtensionsService>(source).ptr(); | 139 ExtensionsService* service = Source<ExtensionsService>(source).ptr(); |
148 for (ExtensionList::const_iterator it = service->extensions()->begin(); | 140 for (ExtensionList::const_iterator it = service->extensions()->begin(); |
149 it != service->extensions()->end(); ++it) { | 141 it != service->extensions()->end(); ++it) { |
150 if (*it != unloaded_extension) | 142 if (*it != unloaded_extension) |
151 CollectURLPatterns(*it, &new_patterns); | 143 CollectURLPatterns(*it, &new_patterns); |
152 } | 144 } |
153 io_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, | 145 ChromeThread::PostTask( |
154 &UserScriptListener::ReplaceURLPatterns, new_patterns)); | 146 ChromeThread::IO, FROM_HERE, |
| 147 NewRunnableMethod( |
| 148 this, &UserScriptListener::ReplaceURLPatterns, new_patterns)); |
155 break; | 149 break; |
156 } | 150 } |
157 | 151 |
158 case NotificationType::USER_SCRIPTS_UPDATED: { | 152 case NotificationType::USER_SCRIPTS_UPDATED: { |
159 io_loop_->PostTask(FROM_HERE, | 153 ChromeThread::PostTask( |
| 154 ChromeThread::IO, FROM_HERE, |
160 NewRunnableMethod(this, &UserScriptListener::StartDelayedRequests)); | 155 NewRunnableMethod(this, &UserScriptListener::StartDelayedRequests)); |
161 break; | 156 break; |
162 } | 157 } |
163 | 158 |
164 default: | 159 default: |
165 NOTREACHED(); | 160 NOTREACHED(); |
166 } | 161 } |
167 } | 162 } |
OLD | NEW |