blob: 6a136b2c0613e69988afe123429e42314fb864ee [file] [log] [blame]
[email protected]14c3571a2013-11-13 00:18:441// Copyright 2013 The Chromium Authors. All rights reserved.
[email protected]703e807a2009-03-28 19:56:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]14c3571a2013-11-13 00:18:445#include "extensions/browser/extension_function.h"
[email protected]703e807a2009-03-28 19:56:516
[email protected]73404a372009-04-17 23:09:107#include "base/logging.h"
asargentbf199b72014-12-10 00:52:468#include "base/memory/singleton.h"
9#include "base/synchronization/lock.h"
[email protected]86ab86b2011-10-19 03:07:5510#include "content/public/browser/notification_source.h"
[email protected]0d6e9bd2011-10-18 04:29:1611#include "content/public/browser/notification_types.h"
[email protected]6dd625e2013-12-20 17:03:0712#include "content/public/browser/render_frame_host.h"
[email protected]9c1662b2012-03-06 15:44:3313#include "content/public/browser/render_view_host.h"
[email protected]bc0ee242013-10-22 03:46:1414#include "content/public/browser/web_contents.h"
15#include "content/public/browser/web_contents_observer.h"
[email protected]0b9de032014-03-15 05:47:0116#include "extensions/browser/extension_function_dispatcher.h"
[email protected]1a0436892014-04-01 00:38:2517#include "extensions/browser/extension_message_filter.h"
[email protected]00afda7f2014-05-29 01:18:0818#include "extensions/common/error_utils.h"
[email protected]d6ec84a2013-11-01 13:07:3819#include "extensions/common/extension_api.h"
[email protected]fb820c02014-03-13 15:07:0820#include "extensions/common/extension_messages.h"
[email protected]c5dbef02011-05-13 05:06:0921
[email protected]631bb742011-11-02 11:29:3922using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1123using content::RenderViewHost;
[email protected]bc0ee242013-10-22 03:46:1424using content::WebContents;
[email protected]00afda7f2014-05-29 01:18:0825using extensions::ErrorUtils;
[email protected]b5b26b72013-08-02 00:25:1126using extensions::ExtensionAPI;
27using extensions::Feature;
[email protected]631bb742011-11-02 11:29:3928
[email protected]f4e972d2014-04-24 22:55:5829namespace {
30
[email protected]32f22502014-05-20 21:31:4831class ArgumentListResponseValue
[email protected]f4e972d2014-04-24 22:55:5832 : public ExtensionFunction::ResponseValueObject {
33 public:
[email protected]32f22502014-05-20 21:31:4834 ArgumentListResponseValue(const std::string& function_name,
35 const char* title,
36 ExtensionFunction* function,
37 scoped_ptr<base::ListValue> result)
[email protected]e5be73a2014-05-15 00:12:3838 : function_name_(function_name), title_(title) {
[email protected]f4e972d2014-04-24 22:55:5839 if (function->GetResultList()) {
[email protected]32f22502014-05-20 21:31:4840 DCHECK_EQ(function->GetResultList(), result.get())
[email protected]e5be73a2014-05-15 00:12:3841 << "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]f4e972d2014-04-24 22:55:5846 } else {
[email protected]32f22502014-05-20 21:31:4847 function->SetResultList(result.Pass());
[email protected]f4e972d2014-04-24 22:55:5848 }
[email protected]a0c91a9f2014-05-03 03:41:4349 // 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]f4e972d2014-04-24 22:55:5852 }
53
dcheng9168b2f2014-10-21 12:38:2454 ~ArgumentListResponseValue() override {}
[email protected]f4e972d2014-04-24 22:55:5855
dcheng9168b2f2014-10-21 12:38:2456 bool Apply() override { return true; }
[email protected]e5be73a2014-05-15 00:12:3857
58 private:
59 std::string function_name_;
60 const char* title_;
[email protected]f4e972d2014-04-24 22:55:5861};
62
treib325d8a112015-02-09 13:45:5763class 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]f4e972d2014-04-24 22:55:5882class ErrorResponseValue : public ExtensionFunction::ResponseValueObject {
83 public:
84 ErrorResponseValue(ExtensionFunction* function, const std::string& error) {
[email protected]a0c91a9f2014-05-03 03:41:4385 // 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]f4e972d2014-04-24 22:55:5887 function->SetError(error);
88 }
89
dcheng9168b2f2014-10-21 12:38:2490 ~ErrorResponseValue() override {}
[email protected]f4e972d2014-04-24 22:55:5891
dcheng9168b2f2014-10-21 12:38:2492 bool Apply() override { return false; }
[email protected]f4e972d2014-04-24 22:55:5893};
94
95class 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
dcheng9168b2f2014-10-21 12:38:24102 ~BadMessageResponseValue() override {}
[email protected]f4e972d2014-04-24 22:55:58103
dcheng9168b2f2014-10-21 12:38:24104 bool Apply() override { return false; }
[email protected]f4e972d2014-04-24 22:55:58105};
106
107class 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) {}
dcheng9168b2f2014-10-21 12:38:24113 ~RespondNowAction() override {}
[email protected]f4e972d2014-04-24 22:55:58114
dcheng9168b2f2014-10-21 12:38:24115 void Execute() override { send_response_.Run(result_->Apply()); }
[email protected]f4e972d2014-04-24 22:55:58116
117 private:
118 ExtensionFunction::ResponseValue result_;
119 SendResponseCallback send_response_;
120};
121
122class RespondLaterAction : public ExtensionFunction::ResponseActionObject {
123 public:
dcheng9168b2f2014-10-21 12:38:24124 ~RespondLaterAction() override {}
[email protected]f4e972d2014-04-24 22:55:58125
dcheng9168b2f2014-10-21 12:38:24126 void Execute() override {}
[email protected]f4e972d2014-04-24 22:55:58127};
128
asargentbf199b72014-12-10 00:52:46129// Used in implementation of ScopedUserGestureForTests.
130class 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
152UserGestureForTests* UserGestureForTests::GetInstance() {
153 return Singleton<UserGestureForTests>::get();
154}
155
156UserGestureForTests::UserGestureForTests() : count_(0) {}
157
158bool UserGestureForTests::HaveGesture() {
159 base::AutoLock autolock(lock_);
160 return count_ > 0;
161}
162
163void UserGestureForTests::IncrementCount() {
164 base::AutoLock autolock(lock_);
165 ++count_;
166}
167
168void UserGestureForTests::DecrementCount() {
169 base::AutoLock autolock(lock_);
170 --count_;
171}
172
173
[email protected]f4e972d2014-04-24 22:55:58174} // namespace
175
[email protected]a2aef2e2011-05-26 22:48:12176// static
177void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) {
178 x->Destruct();
179}
180
[email protected]6dd625e2013-12-20 17:03:07181// 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.
184class UIThreadExtensionFunction::RenderHostTracker
[email protected]bc0ee242013-10-22 03:46:14185 : public content::WebContentsObserver {
186 public:
[email protected]6dd625e2013-12-20 17:03:07187 explicit RenderHostTracker(UIThreadExtensionFunction* function)
[email protected]bc0ee242013-10-22 03:46:14188 : content::WebContentsObserver(
[email protected]eb7ef5f2014-02-06 09:59:19189 function->render_view_host() ?
[email protected]6dd625e2013-12-20 17:03:07190 WebContents::FromRenderViewHost(function->render_view_host()) :
191 WebContents::FromRenderFrameHost(
192 function->render_frame_host())),
[email protected]bc0ee242013-10-22 03:46:14193 function_(function) {
194 }
[email protected]942690b132010-05-11 06:42:14195
[email protected]bc0ee242013-10-22 03:46:14196 private:
197 // content::WebContentsObserver:
dcheng9168b2f2014-10-21 12:38:24198 void RenderViewDeleted(content::RenderViewHost* render_view_host) override {
[email protected]bc0ee242013-10-22 03:46:14199 if (render_view_host != function_->render_view_host())
200 return;
[email protected]ce0e2602013-03-15 20:53:27201
[email protected]bc0ee242013-10-22 03:46:14202 function_->SetRenderViewHost(NULL);
203 }
dcheng9168b2f2014-10-21 12:38:24204 void RenderFrameDeleted(
mostynb0eac4e1b2014-10-03 16:32:19205 content::RenderFrameHost* render_frame_host) override {
[email protected]6dd625e2013-12-20 17:03:07206 if (render_frame_host != function_->render_frame_host())
207 return;
208
209 function_->SetRenderFrameHost(NULL);
210 }
[email protected]0f7daaa2011-11-22 18:34:56211
dcheng9168b2f2014-10-21 12:38:24212 bool OnMessageReceived(const IPC::Message& message,
213 content::RenderFrameHost* render_frame_host) override {
[email protected]64ffefa2014-05-10 12:06:33214 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
dcheng9168b2f2014-10-21 12:38:24221 bool OnMessageReceived(const IPC::Message& message) override {
[email protected]6dd625e2013-12-20 17:03:07222 return function_->OnMessageReceived(message);
[email protected]bc0ee242013-10-22 03:46:14223 }
224
225 UIThreadExtensionFunction* function_;
226
[email protected]6dd625e2013-12-20 17:03:07227 DISALLOW_COPY_AND_ASSIGN(RenderHostTracker);
[email protected]bc0ee242013-10-22 03:46:14228};
[email protected]0f7daaa2011-11-22 18:34:56229
[email protected]3a3d47472010-07-15 21:03:54230ExtensionFunction::ExtensionFunction()
[email protected]9931fbfc2010-07-23 09:15:51231 : request_id_(-1),
[email protected]637bf322011-10-01 20:46:32232 profile_id_(NULL),
reillyg9c2528c2015-02-11 00:13:11233 name_(""),
[email protected]9931fbfc2010-07-23 09:15:51234 has_callback_(false),
[email protected]6451e332010-10-05 00:14:53235 include_incognito_(false),
[email protected]a2aef2e2011-05-26 22:48:12236 user_gesture_(false),
[email protected]07ad9622013-01-18 23:00:33237 bad_message_(false),
[email protected]eb7ef5f2014-02-06 09:59:19238 histogram_value_(extensions::functions::UNKNOWN),
[email protected]0239bc52014-08-07 07:27:19239 source_tab_id_(-1),
240 source_context_type_(Feature::UNSPECIFIED_CONTEXT) {
[email protected]eb7ef5f2014-02-06 09:59:19241}
[email protected]3a3d47472010-07-15 21:03:54242
243ExtensionFunction::~ExtensionFunction() {
244}
245
[email protected]2ad65b32011-05-26 23:39:20246UIThreadExtensionFunction* ExtensionFunction::AsUIThreadExtensionFunction() {
247 return NULL;
248}
249
[email protected]c357acb42011-06-09 20:52:42250IOThreadExtensionFunction* ExtensionFunction::AsIOThreadExtensionFunction() {
251 return NULL;
252}
253
[email protected]3d0e2262012-08-02 15:32:16254bool ExtensionFunction::HasPermission() {
[email protected]b5b26b72013-08-02 00:25:11255 Feature::Availability availability =
256 ExtensionAPI::GetSharedInstance()->IsAvailable(
dcheng7921e3f2014-08-25 22:20:01257 name_, extension_.get(), source_context_type_, source_url());
[email protected]b5b26b72013-08-02 00:25:11258 return availability.is_available();
[email protected]3d0e2262012-08-02 15:32:16259}
260
[email protected]85231d72012-08-31 09:45:29261void ExtensionFunction::OnQuotaExceeded(const std::string& violation_error) {
262 error_ = violation_error;
[email protected]fd50e7b2011-11-03 09:20:25263 SendResponse(false);
264}
265
[email protected]602542d2012-04-20 02:48:01266void ExtensionFunction::SetArgs(const base::ListValue* args) {
[email protected]30294edf2009-11-10 00:24:38267 DCHECK(!args_.get()); // Should only be called once.
[email protected]16f47e082011-01-18 02:16:59268 args_.reset(args->DeepCopy());
[email protected]b83e4602009-05-15 22:58:33269}
270
[email protected]07ff5fd2012-07-12 22:39:09271void ExtensionFunction::SetResult(base::Value* result) {
272 results_.reset(new base::ListValue());
273 results_->Append(result);
274}
275
[email protected]f4e972d2014-04-24 22:55:58276void ExtensionFunction::SetResultList(scoped_ptr<base::ListValue> results) {
277 results_ = results.Pass();
278}
279
280const base::ListValue* ExtensionFunction::GetResultList() const {
[email protected]07ff5fd2012-07-12 22:39:09281 return results_.get();
[email protected]637bf322011-10-01 20:46:32282}
283
[email protected]f4e972d2014-04-24 22:55:58284std::string ExtensionFunction::GetError() const {
[email protected]3a3d47472010-07-15 21:03:54285 return error_;
286}
287
[email protected]60aad9c2012-01-13 19:55:32288void ExtensionFunction::SetError(const std::string& error) {
289 error_ = error;
290}
291
asargentbf199b72014-12-10 00:52:46292bool ExtensionFunction::user_gesture() const {
293 return user_gesture_ || UserGestureForTests::GetInstance()->HaveGesture();
294}
295
[email protected]f4e972d2014-04-24 22:55:58296ExtensionFunction::ResponseValue ExtensionFunction::NoArguments() {
[email protected]32f22502014-05-20 21:31:48297 return ResponseValue(new ArgumentListResponseValue(
298 name(), "NoArguments", this, make_scoped_ptr(new base::ListValue())));
[email protected]f4e972d2014-04-24 22:55:58299}
300
[email protected]32f22502014-05-20 21:31:48301ExtensionFunction::ResponseValue ExtensionFunction::OneArgument(
[email protected]f4e972d2014-04-24 22:55:58302 base::Value* arg) {
[email protected]32f22502014-05-20 21:31:48303 scoped_ptr<base::ListValue> args(new base::ListValue());
[email protected]f4e972d2014-04-24 22:55:58304 args->Append(arg);
[email protected]e5be73a2014-05-15 00:12:38305 return ResponseValue(
[email protected]32f22502014-05-20 21:31:48306 new ArgumentListResponseValue(name(), "OneArgument", this, args.Pass()));
[email protected]f4e972d2014-04-24 22:55:58307}
308
[email protected]32f22502014-05-20 21:31:48309ExtensionFunction::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
319ExtensionFunction::ResponseValue ExtensionFunction::ArgumentList(
320 scoped_ptr<base::ListValue> args) {
321 return ResponseValue(
322 new ArgumentListResponseValue(name(), "ArgumentList", this, args.Pass()));
[email protected]f4e972d2014-04-24 22:55:58323}
324
325ExtensionFunction::ResponseValue ExtensionFunction::Error(
326 const std::string& error) {
[email protected]e5be73a2014-05-15 00:12:38327 return ResponseValue(new ErrorResponseValue(this, error));
[email protected]f4e972d2014-04-24 22:55:58328}
329
[email protected]00afda7f2014-05-29 01:18:08330ExtensionFunction::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
337ExtensionFunction::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
345ExtensionFunction::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
treib325d8a112015-02-09 13:45:57354ExtensionFunction::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]f4e972d2014-04-24 22:55:58361ExtensionFunction::ResponseValue ExtensionFunction::BadMessage() {
[email protected]e5be73a2014-05-15 00:12:38362 return ResponseValue(new BadMessageResponseValue(this));
[email protected]f4e972d2014-04-24 22:55:58363}
364
365ExtensionFunction::ResponseAction ExtensionFunction::RespondNow(
366 ResponseValue result) {
[email protected]5b50d882014-05-09 11:37:30367 return ResponseAction(new RespondNowAction(
[email protected]f4e972d2014-04-24 22:55:58368 result.Pass(), base::Bind(&ExtensionFunction::SendResponse, this)));
369}
370
371ExtensionFunction::ResponseAction ExtensionFunction::RespondLater() {
[email protected]5b50d882014-05-09 11:37:30372 return ResponseAction(new RespondLaterAction());
373}
374
375// static
376ExtensionFunction::ResponseAction ExtensionFunction::ValidationFailure(
377 ExtensionFunction* function) {
378 return function->RespondNow(function->BadMessage());
[email protected]f4e972d2014-04-24 22:55:58379}
380
[email protected]a0c91a9f2014-05-03 03:41:43381void ExtensionFunction::Respond(ResponseValue result) {
382 SendResponse(result->Apply());
[email protected]f4e972d2014-04-24 22:55:58383}
384
[email protected]712627bf2012-04-30 03:21:04385bool ExtensionFunction::ShouldSkipQuotaLimiting() const {
386 return false;
387}
388
[email protected]a2aef2e2011-05-26 22:48:12389bool ExtensionFunction::HasOptionalArgument(size_t index) {
[email protected]4b3006f2013-12-23 22:23:08390 base::Value* value;
391 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL);
[email protected]a2aef2e2011-05-26 22:48:12392}
393
[email protected]35548ab2013-05-15 08:59:47394void ExtensionFunction::SendResponseImpl(bool success) {
395 DCHECK(!response_callback_.is_null());
396
397 ResponseType type = success ? SUCCEEDED : FAILED;
[email protected]c357acb42011-06-09 20:52:42398 if (bad_message_) {
[email protected]35548ab2013-05-15 08:59:47399 type = BAD_MESSAGE;
400 LOG(ERROR) << "Bad extension message " << name_;
[email protected]c357acb42011-06-09 20:52:42401 }
402
[email protected]07ff5fd2012-07-12 22:39:09403 // If results were never set, we send an empty argument list.
[email protected]3eeddd892013-04-17 17:00:11404 if (!results_)
[email protected]aeca23f2013-06-21 22:34:41405 results_.reset(new base::ListValue());
[email protected]602542d2012-04-20 02:48:01406
kalmaned033322015-03-03 03:26:52407 response_callback_.Run(type, *results_, GetError(), histogram_value());
[email protected]c357acb42011-06-09 20:52:42408}
409
[email protected]a0c91a9f2014-05-03 03:41:43410void ExtensionFunction::OnRespondingLater(ResponseValue value) {
411 SendResponse(value->Apply());
412}
413
[email protected]a2aef2e2011-05-26 22:48:12414UIThreadExtensionFunction::UIThreadExtensionFunction()
[email protected]eb7ef5f2014-02-06 09:59:19415 : render_view_host_(NULL),
416 render_frame_host_(NULL),
417 context_(NULL),
418 delegate_(NULL) {
419}
[email protected]a2aef2e2011-05-26 22:48:12420
421UIThreadExtensionFunction::~UIThreadExtensionFunction() {
[email protected]7042b682012-04-19 22:57:51422 if (dispatcher() && render_view_host())
[email protected]eba8f7d2014-07-28 22:09:23423 dispatcher()->OnExtensionFunctionCompleted(extension());
[email protected]a2aef2e2011-05-26 22:48:12424}
425
[email protected]2ad65b32011-05-26 23:39:20426UIThreadExtensionFunction*
427UIThreadExtensionFunction::AsUIThreadExtensionFunction() {
428 return this;
429}
430
[email protected]6dd625e2013-12-20 17:03:07431bool UIThreadExtensionFunction::OnMessageReceived(const IPC::Message& message) {
[email protected]0f7daaa2011-11-22 18:34:56432 return false;
433}
434
[email protected]a2aef2e2011-05-26 22:48:12435void UIThreadExtensionFunction::Destruct() const {
436 BrowserThread::DeleteOnUIThread::Destruct(this);
437}
438
439void UIThreadExtensionFunction::SetRenderViewHost(
440 RenderViewHost* render_view_host) {
[email protected]6dd625e2013-12-20 17:03:07441 DCHECK(!render_frame_host_);
[email protected]a2aef2e2011-05-26 22:48:12442 render_view_host_ = render_view_host;
[email protected]6dd625e2013-12-20 17:03:07443 tracker_.reset(render_view_host ? new RenderHostTracker(this) : NULL);
444}
445
446void 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]a2aef2e2011-05-26 22:48:12451}
452
[email protected]91e51d612012-10-21 23:03:05453content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() {
[email protected]21a40082013-10-28 21:19:23454 content::WebContents* web_contents = NULL;
455 if (dispatcher())
456 web_contents = dispatcher()->delegate()->GetAssociatedWebContents();
[email protected]91e51d612012-10-21 23:03:05457
[email protected]21a40082013-10-28 21:19:23458 return web_contents;
[email protected]a2aef2e2011-05-26 22:48:12459}
460
rdevlin.cronin5fa486e2015-02-25 18:24:04461content::WebContents* UIThreadExtensionFunction::GetSenderWebContents() {
462 return render_view_host_ ?
463 content::WebContents::FromRenderViewHost(render_view_host_) : nullptr;
464}
465
[email protected]a2aef2e2011-05-26 22:48:12466void UIThreadExtensionFunction::SendResponse(bool success) {
[email protected]35548ab2013-05-15 08:59:47467 if (delegate_)
[email protected]ca6df682012-04-10 23:00:20468 delegate_->OnSendResponse(this, success, bad_message_);
[email protected]35548ab2013-05-15 08:59:47469 else
470 SendResponseImpl(success);
[email protected]c0b5eb02014-06-02 17:28:10471
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
479void 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]c5dbef02011-05-13 05:06:09483}
484
[email protected]c6970072013-01-10 02:59:43485void UIThreadExtensionFunction::WriteToConsole(
486 content::ConsoleMessageLevel level,
487 const std::string& message) {
[email protected]c0b5eb02014-06-02 17:28:10488 GetIPCSender()->Send(
489 new ExtensionMsg_AddMessageToConsole(GetRoutingID(), level, message));
490}
491
492IPC::Sender* UIThreadExtensionFunction::GetIPCSender() {
493 if (render_view_host_)
494 return render_view_host_;
495 else
496 return render_frame_host_;
497}
498
499int UIThreadExtensionFunction::GetRoutingID() {
500 if (render_view_host_)
501 return render_view_host_->GetRoutingID();
502 else
503 return render_frame_host_->GetRoutingID();
[email protected]c6970072013-01-10 02:59:43504}
505
[email protected]44295a12013-06-05 08:45:46506IOThreadExtensionFunction::IOThreadExtensionFunction()
507 : routing_id_(MSG_ROUTING_NONE) {
[email protected]c357acb42011-06-09 20:52:42508}
509
510IOThreadExtensionFunction::~IOThreadExtensionFunction() {
511}
512
513IOThreadExtensionFunction*
514IOThreadExtensionFunction::AsIOThreadExtensionFunction() {
515 return this;
516}
517
518void IOThreadExtensionFunction::Destruct() const {
519 BrowserThread::DeleteOnIOThread::Destruct(this);
520}
521
522void IOThreadExtensionFunction::SendResponse(bool success) {
[email protected]35548ab2013-05-15 08:59:47523 SendResponseImpl(success);
[email protected]703e807a2009-03-28 19:56:51524}
[email protected]73404a372009-04-17 23:09:10525
[email protected]bdfc03e2011-11-22 00:20:33526AsyncExtensionFunction::AsyncExtensionFunction() {
[email protected]a2aef2e2011-05-26 22:48:12527}
528
529AsyncExtensionFunction::~AsyncExtensionFunction() {
[email protected]35213ce92010-04-08 19:06:15530}
[email protected]3a3d47472010-07-15 21:03:54531
asargentbf199b72014-12-10 00:52:46532ExtensionFunction::ScopedUserGestureForTests::ScopedUserGestureForTests() {
533 UserGestureForTests::GetInstance()->IncrementCount();
534}
535
536ExtensionFunction::ScopedUserGestureForTests::~ScopedUserGestureForTests() {
537 UserGestureForTests::GetInstance()->DecrementCount();
538}
539
[email protected]a0c91a9f2014-05-03 03:41:43540ExtensionFunction::ResponseAction AsyncExtensionFunction::Run() {
541 return RunAsync() ? RespondLater() : RespondNow(Error(error_));
542}
543
[email protected]5b50d882014-05-09 11:37:30544// static
545bool AsyncExtensionFunction::ValidationFailure(
546 AsyncExtensionFunction* function) {
547 return false;
548}
549
[email protected]3a3d47472010-07-15 21:03:54550SyncExtensionFunction::SyncExtensionFunction() {
551}
552
553SyncExtensionFunction::~SyncExtensionFunction() {
554}
555
[email protected]a0c91a9f2014-05-03 03:41:43556ExtensionFunction::ResponseAction SyncExtensionFunction::Run() {
[email protected]32f22502014-05-20 21:31:48557 return RespondNow(RunSync() ? ArgumentList(results_.Pass()) : Error(error_));
[email protected]3a3d47472010-07-15 21:03:54558}
[email protected]c357acb42011-06-09 20:52:42559
[email protected]5b50d882014-05-09 11:37:30560// static
561bool SyncExtensionFunction::ValidationFailure(SyncExtensionFunction* function) {
562 return false;
563}
564
[email protected]c357acb42011-06-09 20:52:42565SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() {
566}
567
568SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() {
569}
570
[email protected]a0c91a9f2014-05-03 03:41:43571ExtensionFunction::ResponseAction SyncIOThreadExtensionFunction::Run() {
[email protected]32f22502014-05-20 21:31:48572 return RespondNow(RunSync() ? ArgumentList(results_.Pass()) : Error(error_));
[email protected]c357acb42011-06-09 20:52:42573}
[email protected]5b50d882014-05-09 11:37:30574
575// static
576bool SyncIOThreadExtensionFunction::ValidationFailure(
577 SyncIOThreadExtensionFunction* function) {
578 return false;
579}