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