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