OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extension_function_dispatcher.h" | 5 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 using extensions::ExtensionAPI; | 38 using extensions::ExtensionAPI; |
39 using content::RenderViewHost; | 39 using content::RenderViewHost; |
40 using WebKit::WebSecurityOrigin; | 40 using WebKit::WebSecurityOrigin; |
41 | 41 |
42 namespace { | 42 namespace { |
43 | 43 |
44 const char kAccessDenied[] = "access denied"; | 44 const char kAccessDenied[] = "access denied"; |
45 const char kQuotaExceeded[] = "quota exceeded"; | 45 const char kQuotaExceeded[] = "quota exceeded"; |
46 | 46 |
47 void LogSuccess(const Extension* extension, | 47 void LogSuccess(const Extension* extension, |
48 const ExtensionHostMsg_Request_Params& params) { | 48 const ExtensionHostMsg_Request_Params& params, |
| 49 Profile* profile) { |
49 extensions::ActivityLog* activity_log = | 50 extensions::ActivityLog* activity_log = |
50 extensions::ActivityLog::GetInstance(); | 51 extensions::ActivityLog::GetInstance(profile); |
51 if (activity_log->HasObservers(extension)) { | 52 if (activity_log->HasObservers(extension)) |
52 std::string call_signature = params.name + "("; | 53 activity_log->LogManagerAction(extension, params.name, params.arguments); |
53 ListValue::const_iterator it = params.arguments.begin(); | |
54 for (; it != params.arguments.end(); ++it) { | |
55 std::string arg; | |
56 JSONStringValueSerializer serializer(&arg); | |
57 if (serializer.SerializeAndOmitBinaryValues(**it)) { | |
58 if (it != params.arguments.begin()) | |
59 call_signature += ", "; | |
60 call_signature += arg; | |
61 } | |
62 } | |
63 call_signature += ")"; | |
64 | |
65 activity_log->Log(extension, | |
66 extensions::ActivityLog::ACTIVITY_EXTENSION_API_CALL, | |
67 call_signature); | |
68 } | |
69 } | 54 } |
70 | 55 |
71 void LogFailure(const Extension* extension, | 56 void LogFailure(const Extension* extension, |
72 const std::string& func_name, | 57 const ExtensionHostMsg_Request_Params& params, |
73 const char* reason) { | 58 const char* reason, |
| 59 Profile* profile) { |
74 extensions::ActivityLog* activity_log = | 60 extensions::ActivityLog* activity_log = |
75 extensions::ActivityLog::GetInstance(); | 61 extensions::ActivityLog::GetInstance(profile); |
76 if (activity_log->HasObservers(extension)) { | 62 if (activity_log->HasObservers(extension)) |
77 activity_log->Log(extension, | 63 activity_log->LogBlockedAction(extension, |
78 extensions::ActivityLog::ACTIVITY_EXTENSION_API_BLOCK, | 64 params.name, |
79 func_name + ": " + reason); | 65 params.arguments, |
80 } | 66 reason); |
81 } | 67 } |
82 | 68 |
| 69 |
83 // Separate copy of ExtensionAPI used for IO thread extension functions. We need | 70 // Separate copy of ExtensionAPI used for IO thread extension functions. We need |
84 // this because ExtensionAPI has mutable data. It should be possible to remove | 71 // this because ExtensionAPI has mutable data. It should be possible to remove |
85 // this once all the extension APIs are updated to the feature system. | 72 // this once all the extension APIs are updated to the feature system. |
86 struct Static { | 73 struct Static { |
87 Static() | 74 Static() |
88 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) { | 75 : api(extensions::ExtensionAPI::CreateWithDefaultConfiguration()) { |
89 } | 76 } |
90 scoped_ptr<extensions::ExtensionAPI> api; | 77 scoped_ptr<extensions::ExtensionAPI> api; |
91 }; | 78 }; |
92 base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER; | 79 base::LazyInstance<Static> g_global_io_data = LAZY_INSTANCE_INITIALIZER; |
(...skipping 29 matching lines...) Expand all Loading... |
122 // static | 109 // static |
123 void ExtensionFunctionDispatcher::DispatchOnIOThread( | 110 void ExtensionFunctionDispatcher::DispatchOnIOThread( |
124 ExtensionInfoMap* extension_info_map, | 111 ExtensionInfoMap* extension_info_map, |
125 void* profile, | 112 void* profile, |
126 int render_process_id, | 113 int render_process_id, |
127 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender, | 114 base::WeakPtr<ChromeRenderMessageFilter> ipc_sender, |
128 int routing_id, | 115 int routing_id, |
129 const ExtensionHostMsg_Request_Params& params) { | 116 const ExtensionHostMsg_Request_Params& params) { |
130 const Extension* extension = | 117 const Extension* extension = |
131 extension_info_map->extensions().GetByID(params.extension_id); | 118 extension_info_map->extensions().GetByID(params.extension_id); |
132 | 119 Profile* profile_cast = static_cast<Profile*>(profile); |
133 scoped_refptr<ExtensionFunction> function( | 120 scoped_refptr<ExtensionFunction> function( |
134 CreateExtensionFunction(params, extension, render_process_id, | 121 CreateExtensionFunction(params, extension, render_process_id, |
135 extension_info_map->process_map(), | 122 extension_info_map->process_map(), |
136 g_global_io_data.Get().api.get(), | 123 g_global_io_data.Get().api.get(), |
137 profile, | 124 profile, |
138 ipc_sender, NULL, routing_id)); | 125 ipc_sender, NULL, routing_id)); |
139 if (!function) { | 126 if (!function) { |
140 LogFailure(extension, params.name, kAccessDenied); | 127 LogFailure(extension, params, kAccessDenied, profile_cast); |
141 return; | 128 return; |
142 } | 129 } |
143 | 130 |
144 IOThreadExtensionFunction* function_io = | 131 IOThreadExtensionFunction* function_io = |
145 function->AsIOThreadExtensionFunction(); | 132 function->AsIOThreadExtensionFunction(); |
146 if (!function_io) { | 133 if (!function_io) { |
147 NOTREACHED(); | 134 NOTREACHED(); |
148 return; | 135 return; |
149 } | 136 } |
150 function_io->set_ipc_sender(ipc_sender, routing_id); | 137 function_io->set_ipc_sender(ipc_sender, routing_id); |
151 function_io->set_extension_info_map(extension_info_map); | 138 function_io->set_extension_info_map(extension_info_map); |
152 function->set_include_incognito( | 139 function->set_include_incognito( |
153 extension_info_map->IsIncognitoEnabled(extension->id())); | 140 extension_info_map->IsIncognitoEnabled(extension->id())); |
154 | 141 |
155 if (!CheckPermissions(function, extension, params, ipc_sender, routing_id)) { | 142 if (!CheckPermissions(function, extension, params, ipc_sender, routing_id)) { |
156 LogFailure(extension, params.name, kAccessDenied); | 143 LogFailure(extension, params, kAccessDenied, profile_cast); |
157 return; | 144 return; |
158 } | 145 } |
159 | 146 |
160 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService(); | 147 ExtensionsQuotaService* quota = extension_info_map->GetQuotaService(); |
161 std::string violation_error = quota->Assess(extension->id(), | 148 std::string violation_error = quota->Assess(extension->id(), |
162 function, | 149 function, |
163 ¶ms.arguments, | 150 ¶ms.arguments, |
164 base::TimeTicks::Now()); | 151 base::TimeTicks::Now()); |
165 if (violation_error.empty()) { | 152 if (violation_error.empty()) { |
166 function->Run(); | 153 function->Run(); |
167 LogSuccess(extension, params); | 154 LogSuccess(extension, params, profile_cast); |
168 } else { | 155 } else { |
169 function->OnQuotaExceeded(violation_error); | 156 function->OnQuotaExceeded(violation_error); |
170 LogFailure(extension, params.name, kQuotaExceeded); | 157 LogFailure(extension, params, kQuotaExceeded, profile_cast); |
171 } | 158 } |
172 } | 159 } |
173 | 160 |
174 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile, | 161 ExtensionFunctionDispatcher::ExtensionFunctionDispatcher(Profile* profile, |
175 Delegate* delegate) | 162 Delegate* delegate) |
176 : profile_(profile), | 163 : profile_(profile), |
177 delegate_(delegate) { | 164 delegate_(delegate) { |
178 } | 165 } |
179 | 166 |
180 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { | 167 ExtensionFunctionDispatcher::~ExtensionFunctionDispatcher() { |
(...skipping 17 matching lines...) Expand all Loading... |
198 params.source_url)); | 185 params.source_url)); |
199 | 186 |
200 scoped_refptr<ExtensionFunction> function( | 187 scoped_refptr<ExtensionFunction> function( |
201 CreateExtensionFunction(params, extension, | 188 CreateExtensionFunction(params, extension, |
202 render_view_host->GetProcess()->GetID(), | 189 render_view_host->GetProcess()->GetID(), |
203 *(service->process_map()), | 190 *(service->process_map()), |
204 extensions::ExtensionAPI::GetSharedInstance(), | 191 extensions::ExtensionAPI::GetSharedInstance(), |
205 profile(), render_view_host, render_view_host, | 192 profile(), render_view_host, render_view_host, |
206 render_view_host->GetRoutingID())); | 193 render_view_host->GetRoutingID())); |
207 if (!function) { | 194 if (!function) { |
208 LogFailure(extension, params.name, kAccessDenied); | 195 LogFailure(extension, params, kAccessDenied, profile()); |
209 return; | 196 return; |
210 } | 197 } |
211 | 198 |
212 UIThreadExtensionFunction* function_ui = | 199 UIThreadExtensionFunction* function_ui = |
213 function->AsUIThreadExtensionFunction(); | 200 function->AsUIThreadExtensionFunction(); |
214 if (!function_ui) { | 201 if (!function_ui) { |
215 NOTREACHED(); | 202 NOTREACHED(); |
216 return; | 203 return; |
217 } | 204 } |
218 function_ui->set_dispatcher(AsWeakPtr()); | 205 function_ui->set_dispatcher(AsWeakPtr()); |
219 function_ui->set_profile(profile_); | 206 function_ui->set_profile(profile_); |
220 function->set_include_incognito(service->CanCrossIncognito(extension)); | 207 function->set_include_incognito(service->CanCrossIncognito(extension)); |
221 | 208 |
222 if (!CheckPermissions(function, extension, params, render_view_host, | 209 if (!CheckPermissions(function, extension, params, render_view_host, |
223 render_view_host->GetRoutingID())) { | 210 render_view_host->GetRoutingID())) { |
224 LogFailure(extension, params.name, kAccessDenied); | 211 LogFailure(extension, params, kAccessDenied, profile()); |
225 return; | 212 return; |
226 } | 213 } |
227 | 214 |
228 ExtensionsQuotaService* quota = service->quota_service(); | 215 ExtensionsQuotaService* quota = service->quota_service(); |
229 std::string violation_error = quota->Assess(extension->id(), | 216 std::string violation_error = quota->Assess(extension->id(), |
230 function, | 217 function, |
231 ¶ms.arguments, | 218 ¶ms.arguments, |
232 base::TimeTicks::Now()); | 219 base::TimeTicks::Now()); |
233 if (violation_error.empty()) { | 220 if (violation_error.empty()) { |
234 // See crbug.com/39178. | 221 // See crbug.com/39178. |
235 ExternalProtocolHandler::PermitLaunchUrl(); | 222 ExternalProtocolHandler::PermitLaunchUrl(); |
236 | 223 |
237 function->Run(); | 224 function->Run(); |
238 LogSuccess(extension, params); | 225 LogSuccess(extension, params, profile()); |
239 } else { | 226 } else { |
240 function->OnQuotaExceeded(violation_error); | 227 function->OnQuotaExceeded(violation_error); |
241 LogFailure(extension, params.name, kQuotaExceeded); | 228 LogFailure(extension, params, kQuotaExceeded, profile()); |
242 } | 229 } |
243 | 230 |
244 // Note: do not access |this| after this point. We may have been deleted | 231 // Note: do not access |this| after this point. We may have been deleted |
245 // if function->Run() ended up closing the tab that owns us. | 232 // if function->Run() ended up closing the tab that owns us. |
246 | 233 |
247 // Check if extension was uninstalled by management.uninstall. | 234 // Check if extension was uninstalled by management.uninstall. |
248 if (!service->extensions()->GetByID(params.extension_id)) | 235 if (!service->extensions()->GetByID(params.extension_id)) |
249 return; | 236 return; |
250 | 237 |
251 // We only adjust the keepalive count for UIThreadExtensionFunction for | 238 // We only adjust the keepalive count for UIThreadExtensionFunction for |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 } | 310 } |
324 | 311 |
325 // static | 312 // static |
326 void ExtensionFunctionDispatcher::SendAccessDenied( | 313 void ExtensionFunctionDispatcher::SendAccessDenied( |
327 IPC::Sender* ipc_sender, int routing_id, int request_id) { | 314 IPC::Sender* ipc_sender, int routing_id, int request_id) { |
328 ListValue empty_list; | 315 ListValue empty_list; |
329 ipc_sender->Send(new ExtensionMsg_Response( | 316 ipc_sender->Send(new ExtensionMsg_Response( |
330 routing_id, request_id, false, empty_list, | 317 routing_id, request_id, false, empty_list, |
331 "Access to extension API denied.")); | 318 "Access to extension API denied.")); |
332 } | 319 } |
OLD | NEW |