[email protected] | 60aad9c | 2012-01-13 19:55:32 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 703e807a | 2009-03-28 19:56:51 | [diff] [blame] | 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/extension_function.h" |
| 6 | |
[email protected] | 73404a37 | 2009-04-17 23:09:10 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 07ad962 | 2013-01-18 23:00:33 | [diff] [blame] | 8 | #include "base/metrics/histogram.h" |
[email protected] | 703e807a | 2009-03-28 19:56:51 | [diff] [blame] | 9 | #include "chrome/browser/extensions/extension_function_dispatcher.h" |
[email protected] | eaa7dd18 | 2010-12-14 11:09:00 | [diff] [blame] | 10 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 11 | #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
[email protected] | b5b26b7 | 2013-08-02 00:25:11 | [diff] [blame] | 12 | #include "chrome/common/extensions/api/extension_api.h" |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 13 | #include "chrome/common/extensions/extension_messages.h" |
[email protected] | 86ab86b | 2011-10-19 03:07:55 | [diff] [blame] | 14 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 15 | #include "content/public/browser/notification_types.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 16 | #include "content/public/browser/render_view_host.h" |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 17 | #include "content/public/browser/web_contents.h" |
| 18 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 19 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 20 | using content::BrowserThread; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 21 | using content::RenderViewHost; |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 22 | using content::WebContents; |
[email protected] | b5b26b7 | 2013-08-02 00:25:11 | [diff] [blame] | 23 | using extensions::ExtensionAPI; |
| 24 | using extensions::Feature; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 25 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 26 | // static |
| 27 | void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) { |
| 28 | x->Destruct(); |
| 29 | } |
| 30 | |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 31 | // Helper class to track the lifetime of ExtensionFunction's RenderViewHost |
| 32 | // pointer and NULL it out when it dies. It also allows us to filter IPC |
| 33 | // messages coming from the RenderViewHost. |
| 34 | class UIThreadExtensionFunction::RenderViewHostTracker |
| 35 | : public content::WebContentsObserver { |
| 36 | public: |
| 37 | explicit RenderViewHostTracker(UIThreadExtensionFunction* function) |
| 38 | : content::WebContentsObserver( |
| 39 | WebContents::FromRenderViewHost(function->render_view_host())), |
| 40 | function_(function) { |
| 41 | } |
[email protected] | 942690b13 | 2010-05-11 06:42:14 | [diff] [blame] | 42 | |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 43 | private: |
| 44 | // content::WebContentsObserver: |
| 45 | virtual void RenderViewDeleted( |
| 46 | content::RenderViewHost* render_view_host) OVERRIDE { |
| 47 | if (render_view_host != function_->render_view_host()) |
| 48 | return; |
[email protected] | ce0e260 | 2013-03-15 20:53:27 | [diff] [blame] | 49 | |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 50 | function_->SetRenderViewHost(NULL); |
| 51 | } |
[email protected] | 0f7daaa | 2011-11-22 18:34:56 | [diff] [blame] | 52 | |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 53 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
| 54 | return function_->OnMessageReceivedFromRenderView(message); |
| 55 | } |
| 56 | |
| 57 | UIThreadExtensionFunction* function_; |
| 58 | |
| 59 | DISALLOW_COPY_AND_ASSIGN(RenderViewHostTracker); |
| 60 | }; |
[email protected] | 0f7daaa | 2011-11-22 18:34:56 | [diff] [blame] | 61 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 62 | ExtensionFunction::ExtensionFunction() |
[email protected] | 9931fbfc | 2010-07-23 09:15:51 | [diff] [blame] | 63 | : request_id_(-1), |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 64 | profile_id_(NULL), |
[email protected] | 9931fbfc | 2010-07-23 09:15:51 | [diff] [blame] | 65 | has_callback_(false), |
[email protected] | 6451e33 | 2010-10-05 00:14:53 | [diff] [blame] | 66 | include_incognito_(false), |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 67 | user_gesture_(false), |
[email protected] | 07ad962 | 2013-01-18 23:00:33 | [diff] [blame] | 68 | bad_message_(false), |
[email protected] | 4b4c013 | 2013-06-12 17:58:55 | [diff] [blame] | 69 | histogram_value_(extensions::functions::UNKNOWN) {} |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 70 | |
| 71 | ExtensionFunction::~ExtensionFunction() { |
| 72 | } |
| 73 | |
[email protected] | 2ad65b3 | 2011-05-26 23:39:20 | [diff] [blame] | 74 | UIThreadExtensionFunction* ExtensionFunction::AsUIThreadExtensionFunction() { |
| 75 | return NULL; |
| 76 | } |
| 77 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 78 | IOThreadExtensionFunction* ExtensionFunction::AsIOThreadExtensionFunction() { |
| 79 | return NULL; |
| 80 | } |
| 81 | |
[email protected] | 3d0e226 | 2012-08-02 15:32:16 | [diff] [blame] | 82 | bool ExtensionFunction::HasPermission() { |
[email protected] | b5b26b7 | 2013-08-02 00:25:11 | [diff] [blame] | 83 | Feature::Availability availability = |
| 84 | ExtensionAPI::GetSharedInstance()->IsAvailable( |
| 85 | name_, extension_, Feature::BLESSED_EXTENSION_CONTEXT, source_url()); |
| 86 | return availability.is_available(); |
[email protected] | 3d0e226 | 2012-08-02 15:32:16 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 89 | void ExtensionFunction::OnQuotaExceeded(const std::string& violation_error) { |
| 90 | error_ = violation_error; |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 91 | SendResponse(false); |
| 92 | } |
| 93 | |
[email protected] | 602542d | 2012-04-20 02:48:01 | [diff] [blame] | 94 | void ExtensionFunction::SetArgs(const base::ListValue* args) { |
[email protected] | 30294edf | 2009-11-10 00:24:38 | [diff] [blame] | 95 | DCHECK(!args_.get()); // Should only be called once. |
[email protected] | 16f47e08 | 2011-01-18 02:16:59 | [diff] [blame] | 96 | args_.reset(args->DeepCopy()); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 99 | void ExtensionFunction::SetResult(base::Value* result) { |
| 100 | results_.reset(new base::ListValue()); |
| 101 | results_->Append(result); |
| 102 | } |
| 103 | |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 104 | const base::ListValue* ExtensionFunction::GetResultList() { |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 105 | return results_.get(); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 106 | } |
| 107 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 108 | const std::string ExtensionFunction::GetError() { |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 109 | return error_; |
| 110 | } |
| 111 | |
[email protected] | 60aad9c | 2012-01-13 19:55:32 | [diff] [blame] | 112 | void ExtensionFunction::SetError(const std::string& error) { |
| 113 | error_ = error; |
| 114 | } |
| 115 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 116 | void ExtensionFunction::Run() { |
[email protected] | 07ad962 | 2013-01-18 23:00:33 | [diff] [blame] | 117 | UMA_HISTOGRAM_ENUMERATION("Extensions.FunctionCalls", histogram_value(), |
| 118 | extensions::functions::ENUM_BOUNDARY); |
| 119 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 120 | if (!RunImpl()) |
| 121 | SendResponse(false); |
| 122 | } |
| 123 | |
[email protected] | 712627bf | 2012-04-30 03:21:04 | [diff] [blame] | 124 | bool ExtensionFunction::ShouldSkipQuotaLimiting() const { |
| 125 | return false; |
| 126 | } |
| 127 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 128 | bool ExtensionFunction::HasOptionalArgument(size_t index) { |
| 129 | Value* value; |
| 130 | return args_->Get(index, &value) && !value->IsType(Value::TYPE_NULL); |
| 131 | } |
| 132 | |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 133 | void ExtensionFunction::SendResponseImpl(bool success) { |
| 134 | DCHECK(!response_callback_.is_null()); |
| 135 | |
| 136 | ResponseType type = success ? SUCCEEDED : FAILED; |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 137 | if (bad_message_) { |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 138 | type = BAD_MESSAGE; |
| 139 | LOG(ERROR) << "Bad extension message " << name_; |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 142 | // If results were never set, we send an empty argument list. |
[email protected] | 3eeddd89 | 2013-04-17 17:00:11 | [diff] [blame] | 143 | if (!results_) |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 144 | results_.reset(new base::ListValue()); |
[email protected] | 602542d | 2012-04-20 02:48:01 | [diff] [blame] | 145 | |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 146 | response_callback_.Run(type, *results_, GetError()); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 149 | UIThreadExtensionFunction::UIThreadExtensionFunction() |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame^] | 150 | : render_view_host_(NULL), context_(NULL), delegate_(NULL) {} |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 151 | |
| 152 | UIThreadExtensionFunction::~UIThreadExtensionFunction() { |
[email protected] | 7042b68 | 2012-04-19 22:57:51 | [diff] [blame] | 153 | if (dispatcher() && render_view_host()) |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 154 | dispatcher()->OnExtensionFunctionCompleted(GetExtension()); |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 155 | } |
| 156 | |
[email protected] | 2ad65b3 | 2011-05-26 23:39:20 | [diff] [blame] | 157 | UIThreadExtensionFunction* |
| 158 | UIThreadExtensionFunction::AsUIThreadExtensionFunction() { |
| 159 | return this; |
| 160 | } |
| 161 | |
[email protected] | 0f7daaa | 2011-11-22 18:34:56 | [diff] [blame] | 162 | bool UIThreadExtensionFunction::OnMessageReceivedFromRenderView( |
| 163 | const IPC::Message& message) { |
| 164 | return false; |
| 165 | } |
| 166 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 167 | void UIThreadExtensionFunction::Destruct() const { |
| 168 | BrowserThread::DeleteOnUIThread::Destruct(this); |
| 169 | } |
| 170 | |
| 171 | void UIThreadExtensionFunction::SetRenderViewHost( |
| 172 | RenderViewHost* render_view_host) { |
| 173 | render_view_host_ = render_view_host; |
[email protected] | ce0e260 | 2013-03-15 20:53:27 | [diff] [blame] | 174 | tracker_.reset(render_view_host ? new RenderViewHostTracker(this) : NULL); |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 175 | } |
| 176 | |
[email protected] | 91e51d61 | 2012-10-21 23:03:05 | [diff] [blame] | 177 | content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() { |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame^] | 178 | content::WebContents* web_contents = NULL; |
| 179 | if (dispatcher()) |
| 180 | web_contents = dispatcher()->delegate()->GetAssociatedWebContents(); |
[email protected] | 91e51d61 | 2012-10-21 23:03:05 | [diff] [blame] | 181 | |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame^] | 182 | return web_contents; |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | void UIThreadExtensionFunction::SendResponse(bool success) { |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 186 | if (delegate_) |
[email protected] | ca6df68 | 2012-04-10 23:00:20 | [diff] [blame] | 187 | delegate_->OnSendResponse(this, success, bad_message_); |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 188 | else |
| 189 | SendResponseImpl(success); |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | c697007 | 2013-01-10 02:59:43 | [diff] [blame] | 192 | void UIThreadExtensionFunction::WriteToConsole( |
| 193 | content::ConsoleMessageLevel level, |
| 194 | const std::string& message) { |
| 195 | render_view_host_->Send(new ExtensionMsg_AddMessageToConsole( |
| 196 | render_view_host_->GetRoutingID(), level, message)); |
| 197 | } |
| 198 | |
[email protected] | 44295a1 | 2013-06-05 08:45:46 | [diff] [blame] | 199 | IOThreadExtensionFunction::IOThreadExtensionFunction() |
| 200 | : routing_id_(MSG_ROUTING_NONE) { |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | IOThreadExtensionFunction::~IOThreadExtensionFunction() { |
| 204 | } |
| 205 | |
| 206 | IOThreadExtensionFunction* |
| 207 | IOThreadExtensionFunction::AsIOThreadExtensionFunction() { |
| 208 | return this; |
| 209 | } |
| 210 | |
| 211 | void IOThreadExtensionFunction::Destruct() const { |
| 212 | BrowserThread::DeleteOnIOThread::Destruct(this); |
| 213 | } |
| 214 | |
| 215 | void IOThreadExtensionFunction::SendResponse(bool success) { |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 216 | SendResponseImpl(success); |
[email protected] | 703e807a | 2009-03-28 19:56:51 | [diff] [blame] | 217 | } |
[email protected] | 73404a37 | 2009-04-17 23:09:10 | [diff] [blame] | 218 | |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 219 | AsyncExtensionFunction::AsyncExtensionFunction() { |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | AsyncExtensionFunction::~AsyncExtensionFunction() { |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 223 | } |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 224 | |
| 225 | SyncExtensionFunction::SyncExtensionFunction() { |
| 226 | } |
| 227 | |
| 228 | SyncExtensionFunction::~SyncExtensionFunction() { |
| 229 | } |
| 230 | |
| 231 | void SyncExtensionFunction::Run() { |
| 232 | SendResponse(RunImpl()); |
| 233 | } |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 234 | |
| 235 | SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { |
| 236 | } |
| 237 | |
| 238 | SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { |
| 239 | } |
| 240 | |
| 241 | void SyncIOThreadExtensionFunction::Run() { |
| 242 | SendResponse(RunImpl()); |
| 243 | } |