[email protected] | 14c3571a | 2013-11-13 00:18:44 | [diff] [blame] | 1 | // Copyright 2013 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 | |
[email protected] | 14c3571a | 2013-11-13 00:18:44 | [diff] [blame] | 5 | #include "extensions/browser/extension_function.h" |
[email protected] | 703e807a | 2009-03-28 19:56:51 | [diff] [blame] | 6 | |
[email protected] | 73404a37 | 2009-04-17 23:09:10 | [diff] [blame] | 7 | #include "base/logging.h" |
[email protected] | 86ab86b | 2011-10-19 03:07:55 | [diff] [blame] | 8 | #include "content/public/browser/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 9 | #include "content/public/browser/notification_types.h" |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 10 | #include "content/public/browser/render_frame_host.h" |
[email protected] | 9c1662b | 2012-03-06 15:44:33 | [diff] [blame] | 11 | #include "content/public/browser/render_view_host.h" |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 12 | #include "content/public/browser/web_contents.h" |
| 13 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 0b9de03 | 2014-03-15 05:47:01 | [diff] [blame] | 14 | #include "extensions/browser/extension_function_dispatcher.h" |
[email protected] | 1a043689 | 2014-04-01 00:38:25 | [diff] [blame] | 15 | #include "extensions/browser/extension_message_filter.h" |
[email protected] | d6ec84a | 2013-11-01 13:07:38 | [diff] [blame] | 16 | #include "extensions/common/extension_api.h" |
[email protected] | fb820c0 | 2014-03-13 15:07:08 | [diff] [blame] | 17 | #include "extensions/common/extension_messages.h" |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 18 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 19 | using content::BrowserThread; |
[email protected] | eaabba2 | 2012-03-07 15:02:11 | [diff] [blame] | 20 | using content::RenderViewHost; |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 21 | using content::WebContents; |
[email protected] | b5b26b7 | 2013-08-02 00:25:11 | [diff] [blame] | 22 | using extensions::ExtensionAPI; |
| 23 | using extensions::Feature; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 24 | |
[email protected] | f4e972d | 2014-04-24 22:55:58 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | class MultipleArgumentsResponseValue |
| 28 | : public ExtensionFunction::ResponseValueObject { |
| 29 | public: |
| 30 | MultipleArgumentsResponseValue(ExtensionFunction* function, |
| 31 | base::ListValue* result) { |
| 32 | if (function->GetResultList()) { |
| 33 | DCHECK_EQ(function->GetResultList(), result); |
| 34 | } else { |
| 35 | function->SetResultList(make_scoped_ptr(result)); |
| 36 | } |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 37 | // It would be nice to DCHECK(error.empty()) but some legacy extension |
| 38 | // function implementations... I'm looking at chrome.input.ime... do this |
| 39 | // for some reason. |
[email protected] | f4e972d | 2014-04-24 22:55:58 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | virtual ~MultipleArgumentsResponseValue() {} |
| 43 | |
| 44 | virtual bool Apply() OVERRIDE { return true; } |
| 45 | }; |
| 46 | |
| 47 | class ErrorResponseValue : public ExtensionFunction::ResponseValueObject { |
| 48 | public: |
| 49 | ErrorResponseValue(ExtensionFunction* function, const std::string& error) { |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 50 | // It would be nice to DCHECK(!error.empty()) but too many legacy extension |
| 51 | // function implementations don't set error but signal failure. |
[email protected] | f4e972d | 2014-04-24 22:55:58 | [diff] [blame] | 52 | function->SetError(error); |
| 53 | } |
| 54 | |
| 55 | virtual ~ErrorResponseValue() {} |
| 56 | |
| 57 | virtual bool Apply() OVERRIDE { return false; } |
| 58 | }; |
| 59 | |
| 60 | class BadMessageResponseValue : public ExtensionFunction::ResponseValueObject { |
| 61 | public: |
| 62 | explicit BadMessageResponseValue(ExtensionFunction* function) { |
| 63 | function->set_bad_message(true); |
| 64 | NOTREACHED() << function->name() << ": bad message"; |
| 65 | } |
| 66 | |
| 67 | virtual ~BadMessageResponseValue() {} |
| 68 | |
| 69 | virtual bool Apply() OVERRIDE { return false; } |
| 70 | }; |
| 71 | |
| 72 | class RespondNowAction : public ExtensionFunction::ResponseActionObject { |
| 73 | public: |
| 74 | typedef base::Callback<void(bool)> SendResponseCallback; |
| 75 | RespondNowAction(ExtensionFunction::ResponseValue result, |
| 76 | const SendResponseCallback& send_response) |
| 77 | : result_(result.Pass()), send_response_(send_response) {} |
| 78 | virtual ~RespondNowAction() {} |
| 79 | |
| 80 | virtual void Execute() OVERRIDE { send_response_.Run(result_->Apply()); } |
| 81 | |
| 82 | private: |
| 83 | ExtensionFunction::ResponseValue result_; |
| 84 | SendResponseCallback send_response_; |
| 85 | }; |
| 86 | |
| 87 | class RespondLaterAction : public ExtensionFunction::ResponseActionObject { |
| 88 | public: |
| 89 | virtual ~RespondLaterAction() {} |
| 90 | |
| 91 | virtual void Execute() OVERRIDE {} |
| 92 | }; |
| 93 | |
| 94 | } // namespace |
| 95 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 96 | // static |
| 97 | void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) { |
| 98 | x->Destruct(); |
| 99 | } |
| 100 | |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 101 | // Helper class to track the lifetime of ExtensionFunction's RenderViewHost or |
| 102 | // RenderFrameHost pointer and NULL it out when it dies. It also allows us to |
| 103 | // filter IPC messages coming from the RenderViewHost/RenderFrameHost. |
| 104 | class UIThreadExtensionFunction::RenderHostTracker |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 105 | : public content::WebContentsObserver { |
| 106 | public: |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 107 | explicit RenderHostTracker(UIThreadExtensionFunction* function) |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 108 | : content::WebContentsObserver( |
[email protected] | eb7ef5f | 2014-02-06 09:59:19 | [diff] [blame] | 109 | function->render_view_host() ? |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 110 | WebContents::FromRenderViewHost(function->render_view_host()) : |
| 111 | WebContents::FromRenderFrameHost( |
| 112 | function->render_frame_host())), |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 113 | function_(function) { |
| 114 | } |
[email protected] | 942690b13 | 2010-05-11 06:42:14 | [diff] [blame] | 115 | |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 116 | private: |
| 117 | // content::WebContentsObserver: |
| 118 | virtual void RenderViewDeleted( |
| 119 | content::RenderViewHost* render_view_host) OVERRIDE { |
| 120 | if (render_view_host != function_->render_view_host()) |
| 121 | return; |
[email protected] | ce0e260 | 2013-03-15 20:53:27 | [diff] [blame] | 122 | |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 123 | function_->SetRenderViewHost(NULL); |
| 124 | } |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 125 | virtual void RenderFrameDeleted( |
| 126 | content::RenderFrameHost* render_frame_host) OVERRIDE { |
| 127 | if (render_frame_host != function_->render_frame_host()) |
| 128 | return; |
| 129 | |
| 130 | function_->SetRenderFrameHost(NULL); |
| 131 | } |
[email protected] | 0f7daaa | 2011-11-22 18:34:56 | [diff] [blame] | 132 | |
[email protected] | 64ffefa | 2014-05-10 12:06:33 | [diff] [blame^] | 133 | virtual bool OnMessageReceived( |
| 134 | const IPC::Message& message, |
| 135 | content::RenderFrameHost* render_frame_host) OVERRIDE { |
| 136 | DCHECK(render_frame_host); |
| 137 | if (render_frame_host == function_->render_frame_host()) |
| 138 | return function_->OnMessageReceived(message); |
| 139 | else |
| 140 | return false; |
| 141 | } |
| 142 | |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 143 | virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 144 | return function_->OnMessageReceived(message); |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | UIThreadExtensionFunction* function_; |
| 148 | |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 149 | DISALLOW_COPY_AND_ASSIGN(RenderHostTracker); |
[email protected] | bc0ee24 | 2013-10-22 03:46:14 | [diff] [blame] | 150 | }; |
[email protected] | 0f7daaa | 2011-11-22 18:34:56 | [diff] [blame] | 151 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 152 | ExtensionFunction::ExtensionFunction() |
[email protected] | 9931fbfc | 2010-07-23 09:15:51 | [diff] [blame] | 153 | : request_id_(-1), |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 154 | profile_id_(NULL), |
[email protected] | 9931fbfc | 2010-07-23 09:15:51 | [diff] [blame] | 155 | has_callback_(false), |
[email protected] | 6451e33 | 2010-10-05 00:14:53 | [diff] [blame] | 156 | include_incognito_(false), |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 157 | user_gesture_(false), |
[email protected] | 07ad962 | 2013-01-18 23:00:33 | [diff] [blame] | 158 | bad_message_(false), |
[email protected] | eb7ef5f | 2014-02-06 09:59:19 | [diff] [blame] | 159 | histogram_value_(extensions::functions::UNKNOWN), |
| 160 | source_tab_id_(-1) { |
| 161 | } |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 162 | |
| 163 | ExtensionFunction::~ExtensionFunction() { |
| 164 | } |
| 165 | |
[email protected] | 2ad65b3 | 2011-05-26 23:39:20 | [diff] [blame] | 166 | UIThreadExtensionFunction* ExtensionFunction::AsUIThreadExtensionFunction() { |
| 167 | return NULL; |
| 168 | } |
| 169 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 170 | IOThreadExtensionFunction* ExtensionFunction::AsIOThreadExtensionFunction() { |
| 171 | return NULL; |
| 172 | } |
| 173 | |
[email protected] | 3d0e226 | 2012-08-02 15:32:16 | [diff] [blame] | 174 | bool ExtensionFunction::HasPermission() { |
[email protected] | b5b26b7 | 2013-08-02 00:25:11 | [diff] [blame] | 175 | Feature::Availability availability = |
| 176 | ExtensionAPI::GetSharedInstance()->IsAvailable( |
| 177 | name_, extension_, Feature::BLESSED_EXTENSION_CONTEXT, source_url()); |
| 178 | return availability.is_available(); |
[email protected] | 3d0e226 | 2012-08-02 15:32:16 | [diff] [blame] | 179 | } |
| 180 | |
[email protected] | 85231d7 | 2012-08-31 09:45:29 | [diff] [blame] | 181 | void ExtensionFunction::OnQuotaExceeded(const std::string& violation_error) { |
| 182 | error_ = violation_error; |
[email protected] | fd50e7b | 2011-11-03 09:20:25 | [diff] [blame] | 183 | SendResponse(false); |
| 184 | } |
| 185 | |
[email protected] | 602542d | 2012-04-20 02:48:01 | [diff] [blame] | 186 | void ExtensionFunction::SetArgs(const base::ListValue* args) { |
[email protected] | 30294edf | 2009-11-10 00:24:38 | [diff] [blame] | 187 | DCHECK(!args_.get()); // Should only be called once. |
[email protected] | 16f47e08 | 2011-01-18 02:16:59 | [diff] [blame] | 188 | args_.reset(args->DeepCopy()); |
[email protected] | b83e460 | 2009-05-15 22:58:33 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 191 | void ExtensionFunction::SetResult(base::Value* result) { |
| 192 | results_.reset(new base::ListValue()); |
| 193 | results_->Append(result); |
| 194 | } |
| 195 | |
[email protected] | f4e972d | 2014-04-24 22:55:58 | [diff] [blame] | 196 | void ExtensionFunction::SetResultList(scoped_ptr<base::ListValue> results) { |
| 197 | results_ = results.Pass(); |
| 198 | } |
| 199 | |
| 200 | const base::ListValue* ExtensionFunction::GetResultList() const { |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 201 | return results_.get(); |
[email protected] | 637bf32 | 2011-10-01 20:46:32 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | f4e972d | 2014-04-24 22:55:58 | [diff] [blame] | 204 | std::string ExtensionFunction::GetError() const { |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 205 | return error_; |
| 206 | } |
| 207 | |
[email protected] | 60aad9c | 2012-01-13 19:55:32 | [diff] [blame] | 208 | void ExtensionFunction::SetError(const std::string& error) { |
| 209 | error_ = error; |
| 210 | } |
| 211 | |
[email protected] | f4e972d | 2014-04-24 22:55:58 | [diff] [blame] | 212 | ExtensionFunction::ResponseValue ExtensionFunction::NoArguments() { |
| 213 | return MultipleArguments(new base::ListValue()); |
| 214 | } |
| 215 | |
| 216 | ExtensionFunction::ResponseValue ExtensionFunction::SingleArgument( |
| 217 | base::Value* arg) { |
| 218 | base::ListValue* args = new base::ListValue(); |
| 219 | args->Append(arg); |
| 220 | return MultipleArguments(args); |
| 221 | } |
| 222 | |
| 223 | ExtensionFunction::ResponseValue ExtensionFunction::MultipleArguments( |
| 224 | base::ListValue* args) { |
| 225 | return scoped_ptr<ResponseValueObject>( |
| 226 | new MultipleArgumentsResponseValue(this, args)); |
| 227 | } |
| 228 | |
| 229 | ExtensionFunction::ResponseValue ExtensionFunction::Error( |
| 230 | const std::string& error) { |
| 231 | return scoped_ptr<ResponseValueObject>(new ErrorResponseValue(this, error)); |
| 232 | } |
| 233 | |
| 234 | ExtensionFunction::ResponseValue ExtensionFunction::BadMessage() { |
| 235 | return scoped_ptr<ResponseValueObject>(new BadMessageResponseValue(this)); |
| 236 | } |
| 237 | |
| 238 | ExtensionFunction::ResponseAction ExtensionFunction::RespondNow( |
| 239 | ResponseValue result) { |
[email protected] | 5b50d88 | 2014-05-09 11:37:30 | [diff] [blame] | 240 | return ResponseAction(new RespondNowAction( |
[email protected] | f4e972d | 2014-04-24 22:55:58 | [diff] [blame] | 241 | result.Pass(), base::Bind(&ExtensionFunction::SendResponse, this))); |
| 242 | } |
| 243 | |
| 244 | ExtensionFunction::ResponseAction ExtensionFunction::RespondLater() { |
[email protected] | 5b50d88 | 2014-05-09 11:37:30 | [diff] [blame] | 245 | return ResponseAction(new RespondLaterAction()); |
| 246 | } |
| 247 | |
| 248 | // static |
| 249 | ExtensionFunction::ResponseAction ExtensionFunction::ValidationFailure( |
| 250 | ExtensionFunction* function) { |
| 251 | return function->RespondNow(function->BadMessage()); |
[email protected] | f4e972d | 2014-04-24 22:55:58 | [diff] [blame] | 252 | } |
| 253 | |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 254 | void ExtensionFunction::Respond(ResponseValue result) { |
| 255 | SendResponse(result->Apply()); |
[email protected] | f4e972d | 2014-04-24 22:55:58 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | 712627bf | 2012-04-30 03:21:04 | [diff] [blame] | 258 | bool ExtensionFunction::ShouldSkipQuotaLimiting() const { |
| 259 | return false; |
| 260 | } |
| 261 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 262 | bool ExtensionFunction::HasOptionalArgument(size_t index) { |
[email protected] | 4b3006f | 2013-12-23 22:23:08 | [diff] [blame] | 263 | base::Value* value; |
| 264 | return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL); |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 265 | } |
| 266 | |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 267 | void ExtensionFunction::SendResponseImpl(bool success) { |
| 268 | DCHECK(!response_callback_.is_null()); |
| 269 | |
| 270 | ResponseType type = success ? SUCCEEDED : FAILED; |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 271 | if (bad_message_) { |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 272 | type = BAD_MESSAGE; |
| 273 | LOG(ERROR) << "Bad extension message " << name_; |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 274 | } |
| 275 | |
[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 276 | // If results were never set, we send an empty argument list. |
[email protected] | 3eeddd89 | 2013-04-17 17:00:11 | [diff] [blame] | 277 | if (!results_) |
[email protected] | aeca23f | 2013-06-21 22:34:41 | [diff] [blame] | 278 | results_.reset(new base::ListValue()); |
[email protected] | 602542d | 2012-04-20 02:48:01 | [diff] [blame] | 279 | |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 280 | response_callback_.Run(type, *results_, GetError()); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 281 | } |
| 282 | |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 283 | void ExtensionFunction::OnRespondingLater(ResponseValue value) { |
| 284 | SendResponse(value->Apply()); |
| 285 | } |
| 286 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 287 | UIThreadExtensionFunction::UIThreadExtensionFunction() |
[email protected] | eb7ef5f | 2014-02-06 09:59:19 | [diff] [blame] | 288 | : render_view_host_(NULL), |
| 289 | render_frame_host_(NULL), |
| 290 | context_(NULL), |
| 291 | delegate_(NULL) { |
| 292 | } |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 293 | |
| 294 | UIThreadExtensionFunction::~UIThreadExtensionFunction() { |
[email protected] | 7042b68 | 2012-04-19 22:57:51 | [diff] [blame] | 295 | if (dispatcher() && render_view_host()) |
[email protected] | 720ad131 | 2012-02-27 23:07:36 | [diff] [blame] | 296 | dispatcher()->OnExtensionFunctionCompleted(GetExtension()); |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 297 | } |
| 298 | |
[email protected] | 2ad65b3 | 2011-05-26 23:39:20 | [diff] [blame] | 299 | UIThreadExtensionFunction* |
| 300 | UIThreadExtensionFunction::AsUIThreadExtensionFunction() { |
| 301 | return this; |
| 302 | } |
| 303 | |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 304 | bool UIThreadExtensionFunction::OnMessageReceived(const IPC::Message& message) { |
[email protected] | 0f7daaa | 2011-11-22 18:34:56 | [diff] [blame] | 305 | return false; |
| 306 | } |
| 307 | |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 308 | void UIThreadExtensionFunction::Destruct() const { |
| 309 | BrowserThread::DeleteOnUIThread::Destruct(this); |
| 310 | } |
| 311 | |
| 312 | void UIThreadExtensionFunction::SetRenderViewHost( |
| 313 | RenderViewHost* render_view_host) { |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 314 | DCHECK(!render_frame_host_); |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 315 | render_view_host_ = render_view_host; |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 316 | tracker_.reset(render_view_host ? new RenderHostTracker(this) : NULL); |
| 317 | } |
| 318 | |
| 319 | void UIThreadExtensionFunction::SetRenderFrameHost( |
| 320 | content::RenderFrameHost* render_frame_host) { |
| 321 | DCHECK(!render_view_host_); |
| 322 | render_frame_host_ = render_frame_host; |
| 323 | tracker_.reset(render_frame_host ? new RenderHostTracker(this) : NULL); |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 324 | } |
| 325 | |
[email protected] | 91e51d61 | 2012-10-21 23:03:05 | [diff] [blame] | 326 | content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() { |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame] | 327 | content::WebContents* web_contents = NULL; |
| 328 | if (dispatcher()) |
| 329 | web_contents = dispatcher()->delegate()->GetAssociatedWebContents(); |
[email protected] | 91e51d61 | 2012-10-21 23:03:05 | [diff] [blame] | 330 | |
[email protected] | 21a4008 | 2013-10-28 21:19:23 | [diff] [blame] | 331 | return web_contents; |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | void UIThreadExtensionFunction::SendResponse(bool success) { |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 335 | if (delegate_) |
[email protected] | ca6df68 | 2012-04-10 23:00:20 | [diff] [blame] | 336 | delegate_->OnSendResponse(this, success, bad_message_); |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 337 | else |
| 338 | SendResponseImpl(success); |
[email protected] | c5dbef0 | 2011-05-13 05:06:09 | [diff] [blame] | 339 | } |
| 340 | |
[email protected] | c697007 | 2013-01-10 02:59:43 | [diff] [blame] | 341 | void UIThreadExtensionFunction::WriteToConsole( |
| 342 | content::ConsoleMessageLevel level, |
| 343 | const std::string& message) { |
[email protected] | 6dd625e | 2013-12-20 17:03:07 | [diff] [blame] | 344 | if (render_view_host_) { |
| 345 | render_view_host_->Send(new ExtensionMsg_AddMessageToConsole( |
| 346 | render_view_host_->GetRoutingID(), level, message)); |
| 347 | } else { |
| 348 | render_frame_host_->Send(new ExtensionMsg_AddMessageToConsole( |
| 349 | render_frame_host_->GetRoutingID(), level, message)); |
| 350 | } |
[email protected] | c697007 | 2013-01-10 02:59:43 | [diff] [blame] | 351 | } |
| 352 | |
[email protected] | 44295a1 | 2013-06-05 08:45:46 | [diff] [blame] | 353 | IOThreadExtensionFunction::IOThreadExtensionFunction() |
| 354 | : routing_id_(MSG_ROUTING_NONE) { |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | IOThreadExtensionFunction::~IOThreadExtensionFunction() { |
| 358 | } |
| 359 | |
| 360 | IOThreadExtensionFunction* |
| 361 | IOThreadExtensionFunction::AsIOThreadExtensionFunction() { |
| 362 | return this; |
| 363 | } |
| 364 | |
| 365 | void IOThreadExtensionFunction::Destruct() const { |
| 366 | BrowserThread::DeleteOnIOThread::Destruct(this); |
| 367 | } |
| 368 | |
| 369 | void IOThreadExtensionFunction::SendResponse(bool success) { |
[email protected] | 35548ab | 2013-05-15 08:59:47 | [diff] [blame] | 370 | SendResponseImpl(success); |
[email protected] | 703e807a | 2009-03-28 19:56:51 | [diff] [blame] | 371 | } |
[email protected] | 73404a37 | 2009-04-17 23:09:10 | [diff] [blame] | 372 | |
[email protected] | bdfc03e | 2011-11-22 00:20:33 | [diff] [blame] | 373 | AsyncExtensionFunction::AsyncExtensionFunction() { |
[email protected] | a2aef2e | 2011-05-26 22:48:12 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | AsyncExtensionFunction::~AsyncExtensionFunction() { |
[email protected] | 35213ce9 | 2010-04-08 19:06:15 | [diff] [blame] | 377 | } |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 378 | |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 379 | ExtensionFunction::ResponseAction AsyncExtensionFunction::Run() { |
| 380 | return RunAsync() ? RespondLater() : RespondNow(Error(error_)); |
| 381 | } |
| 382 | |
[email protected] | 5b50d88 | 2014-05-09 11:37:30 | [diff] [blame] | 383 | // static |
| 384 | bool AsyncExtensionFunction::ValidationFailure( |
| 385 | AsyncExtensionFunction* function) { |
| 386 | return false; |
| 387 | } |
| 388 | |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 389 | SyncExtensionFunction::SyncExtensionFunction() { |
| 390 | } |
| 391 | |
| 392 | SyncExtensionFunction::~SyncExtensionFunction() { |
| 393 | } |
| 394 | |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 395 | ExtensionFunction::ResponseAction SyncExtensionFunction::Run() { |
| 396 | return RespondNow(RunSync() ? MultipleArguments(results_.get()) |
| 397 | : Error(error_)); |
[email protected] | 3a3d4747 | 2010-07-15 21:03:54 | [diff] [blame] | 398 | } |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 399 | |
[email protected] | 5b50d88 | 2014-05-09 11:37:30 | [diff] [blame] | 400 | // static |
| 401 | bool SyncExtensionFunction::ValidationFailure(SyncExtensionFunction* function) { |
| 402 | return false; |
| 403 | } |
| 404 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 405 | SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { |
| 406 | } |
| 407 | |
| 408 | SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { |
| 409 | } |
| 410 | |
[email protected] | a0c91a9f | 2014-05-03 03:41:43 | [diff] [blame] | 411 | ExtensionFunction::ResponseAction SyncIOThreadExtensionFunction::Run() { |
| 412 | return RespondNow(RunSync() ? MultipleArguments(results_.get()) |
| 413 | : Error(error_)); |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 414 | } |
[email protected] | 5b50d88 | 2014-05-09 11:37:30 | [diff] [blame] | 415 | |
| 416 | // static |
| 417 | bool SyncIOThreadExtensionFunction::ValidationFailure( |
| 418 | SyncIOThreadExtensionFunction* function) { |
| 419 | return false; |
| 420 | } |