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