blob: 83747f89892e5974b5e2f0e48e0841a07042c950 [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]bc0ee242013-10-22 03:46:1413#include "content/public/browser/web_contents.h"
14#include "content/public/browser/web_contents_observer.h"
[email protected]0b9de032014-03-15 05:47:0115#include "extensions/browser/extension_function_dispatcher.h"
[email protected]1a0436892014-04-01 00:38:2516#include "extensions/browser/extension_message_filter.h"
[email protected]00afda7f2014-05-29 01:18:0817#include "extensions/common/error_utils.h"
[email protected]d6ec84a2013-11-01 13:07:3818#include "extensions/common/extension_api.h"
[email protected]fb820c02014-03-13 15:07:0819#include "extensions/common/extension_messages.h"
[email protected]c5dbef02011-05-13 05:06:0920
[email protected]631bb742011-11-02 11:29:3921using content::BrowserThread;
[email protected]eaabba22012-03-07 15:02:1122using content::RenderViewHost;
[email protected]bc0ee242013-10-22 03:46:1423using content::WebContents;
[email protected]00afda7f2014-05-29 01:18:0824using extensions::ErrorUtils;
[email protected]b5b26b72013-08-02 00:25:1125using extensions::ExtensionAPI;
26using extensions::Feature;
[email protected]631bb742011-11-02 11:29:3927
[email protected]f4e972d2014-04-24 22:55:5828namespace {
29
[email protected]32f22502014-05-20 21:31:4830class ArgumentListResponseValue
[email protected]f4e972d2014-04-24 22:55:5831 : public ExtensionFunction::ResponseValueObject {
32 public:
[email protected]32f22502014-05-20 21:31:4833 ArgumentListResponseValue(const std::string& function_name,
34 const char* title,
35 ExtensionFunction* function,
36 scoped_ptr<base::ListValue> result)
[email protected]e5be73a2014-05-15 00:12:3837 : function_name_(function_name), title_(title) {
[email protected]f4e972d2014-04-24 22:55:5838 if (function->GetResultList()) {
[email protected]32f22502014-05-20 21:31:4839 DCHECK_EQ(function->GetResultList(), result.get())
[email protected]e5be73a2014-05-15 00:12:3840 << "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]f4e972d2014-04-24 22:55:5845 } else {
[email protected]32f22502014-05-20 21:31:4846 function->SetResultList(result.Pass());
[email protected]f4e972d2014-04-24 22:55:5847 }
[email protected]a0c91a9f2014-05-03 03:41:4348 // 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]f4e972d2014-04-24 22:55:5851 }
52
dcheng9168b2f2014-10-21 12:38:2453 ~ArgumentListResponseValue() override {}
[email protected]f4e972d2014-04-24 22:55:5854
dcheng9168b2f2014-10-21 12:38:2455 bool Apply() override { return true; }
[email protected]e5be73a2014-05-15 00:12:3856
57 private:
58 std::string function_name_;
59 const char* title_;
[email protected]f4e972d2014-04-24 22:55:5860};
61
treib325d8a112015-02-09 13:45:5762class 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]f4e972d2014-04-24 22:55:5881class ErrorResponseValue : public ExtensionFunction::ResponseValueObject {
82 public:
83 ErrorResponseValue(ExtensionFunction* function, const std::string& error) {
[email protected]a0c91a9f2014-05-03 03:41:4384 // 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]f4e972d2014-04-24 22:55:5886 function->SetError(error);
87 }
88
dcheng9168b2f2014-10-21 12:38:2489 ~ErrorResponseValue() override {}
[email protected]f4e972d2014-04-24 22:55:5890
dcheng9168b2f2014-10-21 12:38:2491 bool Apply() override { return false; }
[email protected]f4e972d2014-04-24 22:55:5892};
93
94class 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
dcheng9168b2f2014-10-21 12:38:24101 ~BadMessageResponseValue() override {}
[email protected]f4e972d2014-04-24 22:55:58102
dcheng9168b2f2014-10-21 12:38:24103 bool Apply() override { return false; }
[email protected]f4e972d2014-04-24 22:55:58104};
105
106class 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) {}
dcheng9168b2f2014-10-21 12:38:24112 ~RespondNowAction() override {}
[email protected]f4e972d2014-04-24 22:55:58113
dcheng9168b2f2014-10-21 12:38:24114 void Execute() override { send_response_.Run(result_->Apply()); }
[email protected]f4e972d2014-04-24 22:55:58115
116 private:
117 ExtensionFunction::ResponseValue result_;
118 SendResponseCallback send_response_;
119};
120
121class RespondLaterAction : public ExtensionFunction::ResponseActionObject {
122 public:
dcheng9168b2f2014-10-21 12:38:24123 ~RespondLaterAction() override {}
[email protected]f4e972d2014-04-24 22:55:58124
dcheng9168b2f2014-10-21 12:38:24125 void Execute() override {}
[email protected]f4e972d2014-04-24 22:55:58126};
127
asargentbf199b72014-12-10 00:52:46128// Used in implementation of ScopedUserGestureForTests.
129class 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.raula36aa8be2015-09-10 11:14:22144 friend struct base::DefaultSingletonTraits<UserGestureForTests>;
asargentbf199b72014-12-10 00:52:46145
146 base::Lock lock_; // for protecting access to count_
147 int count_;
148};
149
150// static
151UserGestureForTests* UserGestureForTests::GetInstance() {
olli.raula36aa8be2015-09-10 11:14:22152 return base::Singleton<UserGestureForTests>::get();
asargentbf199b72014-12-10 00:52:46153}
154
155UserGestureForTests::UserGestureForTests() : count_(0) {}
156
157bool UserGestureForTests::HaveGesture() {
158 base::AutoLock autolock(lock_);
159 return count_ > 0;
160}
161
162void UserGestureForTests::IncrementCount() {
163 base::AutoLock autolock(lock_);
164 ++count_;
165}
166
167void UserGestureForTests::DecrementCount() {
168 base::AutoLock autolock(lock_);
169 --count_;
170}
171
172
[email protected]f4e972d2014-04-24 22:55:58173} // namespace
174
[email protected]a2aef2e2011-05-26 22:48:12175// static
176void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) {
177 x->Destruct();
178}
179
rdevlin.cronin92503ba2015-06-12 17:00:56180// 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.
183class UIThreadExtensionFunction::RenderFrameHostTracker
[email protected]bc0ee242013-10-22 03:46:14184 : public content::WebContentsObserver {
185 public:
rdevlin.cronin92503ba2015-06-12 17:00:56186 explicit RenderFrameHostTracker(UIThreadExtensionFunction* function)
[email protected]bc0ee242013-10-22 03:46:14187 : content::WebContentsObserver(
rdevlin.cronin92503ba2015-06-12 17:00:56188 WebContents::FromRenderFrameHost(function->render_frame_host())),
[email protected]bc0ee242013-10-22 03:46:14189 function_(function) {
190 }
[email protected]942690b132010-05-11 06:42:14191
[email protected]bc0ee242013-10-22 03:46:14192 private:
193 // content::WebContentsObserver:
dcheng9168b2f2014-10-21 12:38:24194 void RenderFrameDeleted(
mostynb0eac4e1b2014-10-03 16:32:19195 content::RenderFrameHost* render_frame_host) override {
rdevlin.cronin92503ba2015-06-12 17:00:56196 if (render_frame_host == function_->render_frame_host())
197 function_->SetRenderFrameHost(nullptr);
[email protected]6dd625e2013-12-20 17:03:07198 }
[email protected]0f7daaa2011-11-22 18:34:56199
dcheng9168b2f2014-10-21 12:38:24200 bool OnMessageReceived(const IPC::Message& message,
201 content::RenderFrameHost* render_frame_host) override {
rdevlin.cronin92503ba2015-06-12 17:00:56202 return render_frame_host == function_->render_frame_host() &&
203 function_->OnMessageReceived(message);
[email protected]64ffefa2014-05-10 12:06:33204 }
205
dcheng9168b2f2014-10-21 12:38:24206 bool OnMessageReceived(const IPC::Message& message) override {
[email protected]6dd625e2013-12-20 17:03:07207 return function_->OnMessageReceived(message);
[email protected]bc0ee242013-10-22 03:46:14208 }
209
rdevlin.cronin92503ba2015-06-12 17:00:56210 UIThreadExtensionFunction* function_; // Owns us.
[email protected]bc0ee242013-10-22 03:46:14211
rdevlin.cronin92503ba2015-06-12 17:00:56212 DISALLOW_COPY_AND_ASSIGN(RenderFrameHostTracker);
[email protected]bc0ee242013-10-22 03:46:14213};
[email protected]0f7daaa2011-11-22 18:34:56214
[email protected]3a3d47472010-07-15 21:03:54215ExtensionFunction::ExtensionFunction()
[email protected]9931fbfc2010-07-23 09:15:51216 : request_id_(-1),
[email protected]637bf322011-10-01 20:46:32217 profile_id_(NULL),
reillyg9c2528c2015-02-11 00:13:11218 name_(""),
[email protected]9931fbfc2010-07-23 09:15:51219 has_callback_(false),
[email protected]6451e332010-10-05 00:14:53220 include_incognito_(false),
[email protected]a2aef2e2011-05-26 22:48:12221 user_gesture_(false),
[email protected]07ad9622013-01-18 23:00:33222 bad_message_(false),
[email protected]eb7ef5f2014-02-06 09:59:19223 histogram_value_(extensions::functions::UNKNOWN),
[email protected]0239bc52014-08-07 07:27:19224 source_tab_id_(-1),
dmazzonia6ea68f2015-06-10 00:17:05225 source_context_type_(Feature::UNSPECIFIED_CONTEXT),
226 source_process_id_(-1) {
[email protected]eb7ef5f2014-02-06 09:59:19227}
[email protected]3a3d47472010-07-15 21:03:54228
229ExtensionFunction::~ExtensionFunction() {
230}
231
[email protected]2ad65b32011-05-26 23:39:20232UIThreadExtensionFunction* ExtensionFunction::AsUIThreadExtensionFunction() {
233 return NULL;
234}
235
[email protected]c357acb42011-06-09 20:52:42236IOThreadExtensionFunction* ExtensionFunction::AsIOThreadExtensionFunction() {
237 return NULL;
238}
239
[email protected]3d0e2262012-08-02 15:32:16240bool ExtensionFunction::HasPermission() {
[email protected]b5b26b72013-08-02 00:25:11241 Feature::Availability availability =
242 ExtensionAPI::GetSharedInstance()->IsAvailable(
dcheng7921e3f2014-08-25 22:20:01243 name_, extension_.get(), source_context_type_, source_url());
[email protected]b5b26b72013-08-02 00:25:11244 return availability.is_available();
[email protected]3d0e2262012-08-02 15:32:16245}
246
[email protected]85231d72012-08-31 09:45:29247void ExtensionFunction::OnQuotaExceeded(const std::string& violation_error) {
248 error_ = violation_error;
[email protected]fd50e7b2011-11-03 09:20:25249 SendResponse(false);
250}
251
[email protected]602542d2012-04-20 02:48:01252void ExtensionFunction::SetArgs(const base::ListValue* args) {
[email protected]30294edf2009-11-10 00:24:38253 DCHECK(!args_.get()); // Should only be called once.
[email protected]16f47e082011-01-18 02:16:59254 args_.reset(args->DeepCopy());
[email protected]b83e4602009-05-15 22:58:33255}
256
[email protected]07ff5fd2012-07-12 22:39:09257void ExtensionFunction::SetResult(base::Value* result) {
258 results_.reset(new base::ListValue());
259 results_->Append(result);
260}
261
estadea68b0442015-05-12 18:11:50262void ExtensionFunction::SetResult(scoped_ptr<base::Value> result) {
263 results_.reset(new base::ListValue());
264 results_->Append(result.Pass());
265}
266
[email protected]f4e972d2014-04-24 22:55:58267void ExtensionFunction::SetResultList(scoped_ptr<base::ListValue> results) {
268 results_ = results.Pass();
269}
270
271const base::ListValue* ExtensionFunction::GetResultList() const {
[email protected]07ff5fd2012-07-12 22:39:09272 return results_.get();
[email protected]637bf322011-10-01 20:46:32273}
274
[email protected]f4e972d2014-04-24 22:55:58275std::string ExtensionFunction::GetError() const {
[email protected]3a3d47472010-07-15 21:03:54276 return error_;
277}
278
[email protected]60aad9c2012-01-13 19:55:32279void ExtensionFunction::SetError(const std::string& error) {
280 error_ = error;
281}
282
asargentbf199b72014-12-10 00:52:46283bool ExtensionFunction::user_gesture() const {
284 return user_gesture_ || UserGestureForTests::GetInstance()->HaveGesture();
285}
286
[email protected]f4e972d2014-04-24 22:55:58287ExtensionFunction::ResponseValue ExtensionFunction::NoArguments() {
[email protected]32f22502014-05-20 21:31:48288 return ResponseValue(new ArgumentListResponseValue(
289 name(), "NoArguments", this, make_scoped_ptr(new base::ListValue())));
[email protected]f4e972d2014-04-24 22:55:58290}
291
[email protected]32f22502014-05-20 21:31:48292ExtensionFunction::ResponseValue ExtensionFunction::OneArgument(
[email protected]f4e972d2014-04-24 22:55:58293 base::Value* arg) {
[email protected]32f22502014-05-20 21:31:48294 scoped_ptr<base::ListValue> args(new base::ListValue());
[email protected]f4e972d2014-04-24 22:55:58295 args->Append(arg);
[email protected]e5be73a2014-05-15 00:12:38296 return ResponseValue(
[email protected]32f22502014-05-20 21:31:48297 new ArgumentListResponseValue(name(), "OneArgument", this, args.Pass()));
[email protected]f4e972d2014-04-24 22:55:58298}
299
rdevlin.croninbeabd142015-04-14 20:28:58300ExtensionFunction::ResponseValue ExtensionFunction::OneArgument(
301 scoped_ptr<base::Value> arg) {
302 return OneArgument(arg.release());
303}
304
[email protected]32f22502014-05-20 21:31:48305ExtensionFunction::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
315ExtensionFunction::ResponseValue ExtensionFunction::ArgumentList(
316 scoped_ptr<base::ListValue> args) {
317 return ResponseValue(
318 new ArgumentListResponseValue(name(), "ArgumentList", this, args.Pass()));
[email protected]f4e972d2014-04-24 22:55:58319}
320
321ExtensionFunction::ResponseValue ExtensionFunction::Error(
322 const std::string& error) {
[email protected]e5be73a2014-05-15 00:12:38323 return ResponseValue(new ErrorResponseValue(this, error));
[email protected]f4e972d2014-04-24 22:55:58324}
325
[email protected]00afda7f2014-05-29 01:18:08326ExtensionFunction::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
333ExtensionFunction::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
341ExtensionFunction::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
treib325d8a112015-02-09 13:45:57350ExtensionFunction::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]f4e972d2014-04-24 22:55:58357ExtensionFunction::ResponseValue ExtensionFunction::BadMessage() {
[email protected]e5be73a2014-05-15 00:12:38358 return ResponseValue(new BadMessageResponseValue(this));
[email protected]f4e972d2014-04-24 22:55:58359}
360
361ExtensionFunction::ResponseAction ExtensionFunction::RespondNow(
362 ResponseValue result) {
[email protected]5b50d882014-05-09 11:37:30363 return ResponseAction(new RespondNowAction(
[email protected]f4e972d2014-04-24 22:55:58364 result.Pass(), base::Bind(&ExtensionFunction::SendResponse, this)));
365}
366
367ExtensionFunction::ResponseAction ExtensionFunction::RespondLater() {
[email protected]5b50d882014-05-09 11:37:30368 return ResponseAction(new RespondLaterAction());
369}
370
371// static
372ExtensionFunction::ResponseAction ExtensionFunction::ValidationFailure(
373 ExtensionFunction* function) {
374 return function->RespondNow(function->BadMessage());
[email protected]f4e972d2014-04-24 22:55:58375}
376
[email protected]a0c91a9f2014-05-03 03:41:43377void ExtensionFunction::Respond(ResponseValue result) {
378 SendResponse(result->Apply());
[email protected]f4e972d2014-04-24 22:55:58379}
380
[email protected]712627bf2012-04-30 03:21:04381bool ExtensionFunction::ShouldSkipQuotaLimiting() const {
382 return false;
383}
384
[email protected]a2aef2e2011-05-26 22:48:12385bool ExtensionFunction::HasOptionalArgument(size_t index) {
[email protected]4b3006f2013-12-23 22:23:08386 base::Value* value;
387 return args_->Get(index, &value) && !value->IsType(base::Value::TYPE_NULL);
[email protected]a2aef2e2011-05-26 22:48:12388}
389
[email protected]35548ab2013-05-15 08:59:47390void ExtensionFunction::SendResponseImpl(bool success) {
391 DCHECK(!response_callback_.is_null());
392
393 ResponseType type = success ? SUCCEEDED : FAILED;
[email protected]c357acb42011-06-09 20:52:42394 if (bad_message_) {
[email protected]35548ab2013-05-15 08:59:47395 type = BAD_MESSAGE;
396 LOG(ERROR) << "Bad extension message " << name_;
[email protected]c357acb42011-06-09 20:52:42397 }
398
[email protected]07ff5fd2012-07-12 22:39:09399 // If results were never set, we send an empty argument list.
[email protected]3eeddd892013-04-17 17:00:11400 if (!results_)
[email protected]aeca23f2013-06-21 22:34:41401 results_.reset(new base::ListValue());
[email protected]602542d2012-04-20 02:48:01402
kalmaned033322015-03-03 03:26:52403 response_callback_.Run(type, *results_, GetError(), histogram_value());
[email protected]c357acb42011-06-09 20:52:42404}
405
[email protected]a0c91a9f2014-05-03 03:41:43406void ExtensionFunction::OnRespondingLater(ResponseValue value) {
407 SendResponse(value->Apply());
408}
409
[email protected]a2aef2e2011-05-26 22:48:12410UIThreadExtensionFunction::UIThreadExtensionFunction()
rdevlin.cronin92503ba2015-06-12 17:00:56411 : context_(nullptr),
412 render_frame_host_(nullptr),
413 delegate_(nullptr) {
[email protected]eb7ef5f2014-02-06 09:59:19414}
[email protected]a2aef2e2011-05-26 22:48:12415
416UIThreadExtensionFunction::~UIThreadExtensionFunction() {
rdevlin.cronin585b1bc52015-06-15 21:50:17417 if (dispatcher() && render_frame_host())
[email protected]eba8f7d2014-07-28 22:09:23418 dispatcher()->OnExtensionFunctionCompleted(extension());
[email protected]a2aef2e2011-05-26 22:48:12419}
420
[email protected]2ad65b32011-05-26 23:39:20421UIThreadExtensionFunction*
422UIThreadExtensionFunction::AsUIThreadExtensionFunction() {
423 return this;
424}
425
[email protected]6dd625e2013-12-20 17:03:07426bool UIThreadExtensionFunction::OnMessageReceived(const IPC::Message& message) {
[email protected]0f7daaa2011-11-22 18:34:56427 return false;
428}
429
[email protected]a2aef2e2011-05-26 22:48:12430void UIThreadExtensionFunction::Destruct() const {
431 BrowserThread::DeleteOnUIThread::Destruct(this);
432}
433
rdevlin.croninfccacdca2015-06-16 20:02:56434content::RenderViewHost*
435UIThreadExtensionFunction::render_view_host_do_not_use() const {
rdevlin.cronin92503ba2015-06-12 17:00:56436 return render_frame_host_ ? render_frame_host_->GetRenderViewHost() : nullptr;
[email protected]6dd625e2013-12-20 17:03:07437}
438
439void UIThreadExtensionFunction::SetRenderFrameHost(
440 content::RenderFrameHost* render_frame_host) {
rdevlin.cronin92503ba2015-06-12 17:00:56441 DCHECK_NE(render_frame_host_ == nullptr, render_frame_host == nullptr);
[email protected]6dd625e2013-12-20 17:03:07442 render_frame_host_ = render_frame_host;
rdevlin.cronin92503ba2015-06-12 17:00:56443 tracker_.reset(
444 render_frame_host ? new RenderFrameHostTracker(this) : nullptr);
[email protected]a2aef2e2011-05-26 22:48:12445}
446
[email protected]91e51d612012-10-21 23:03:05447content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() {
[email protected]21a40082013-10-28 21:19:23448 content::WebContents* web_contents = NULL;
449 if (dispatcher())
rdevlin.cronincb2ec659a2015-06-10 23:32:41450 web_contents = dispatcher()->GetAssociatedWebContents();
[email protected]91e51d612012-10-21 23:03:05451
[email protected]21a40082013-10-28 21:19:23452 return web_contents;
[email protected]a2aef2e2011-05-26 22:48:12453}
454
rdevlin.cronin5fa486e2015-02-25 18:24:04455content::WebContents* UIThreadExtensionFunction::GetSenderWebContents() {
rdevlin.cronin92503ba2015-06-12 17:00:56456 return render_frame_host_ ?
457 content::WebContents::FromRenderFrameHost(render_frame_host_) : nullptr;
rdevlin.cronin5fa486e2015-02-25 18:24:04458}
459
[email protected]a2aef2e2011-05-26 22:48:12460void UIThreadExtensionFunction::SendResponse(bool success) {
[email protected]35548ab2013-05-15 08:59:47461 if (delegate_)
[email protected]ca6df682012-04-10 23:00:20462 delegate_->OnSendResponse(this, success, bad_message_);
[email protected]35548ab2013-05-15 08:59:47463 else
464 SendResponseImpl(success);
[email protected]c0b5eb02014-06-02 17:28:10465
466 if (!transferred_blob_uuids_.empty()) {
467 DCHECK(!delegate_) << "Blob transfer not supported with test delegate.";
rdevlin.cronin92503ba2015-06-12 17:00:56468 render_frame_host_->Send(
[email protected]c0b5eb02014-06-02 17:28:10469 new ExtensionMsg_TransferBlobs(transferred_blob_uuids_));
470 }
471}
472
473void 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]c5dbef02011-05-13 05:06:09477}
478
[email protected]c6970072013-01-10 02:59:43479void UIThreadExtensionFunction::WriteToConsole(
480 content::ConsoleMessageLevel level,
481 const std::string& message) {
rdevlin.croninb2cec912015-06-24 20:36:01482 // Only the main frame handles dev tools messages.
483 WebContents::FromRenderFrameHost(render_frame_host_)
484 ->GetMainFrame()
485 ->AddMessageToConsole(level, message);
[email protected]c6970072013-01-10 02:59:43486}
487
[email protected]44295a12013-06-05 08:45:46488IOThreadExtensionFunction::IOThreadExtensionFunction()
489 : routing_id_(MSG_ROUTING_NONE) {
[email protected]c357acb42011-06-09 20:52:42490}
491
492IOThreadExtensionFunction::~IOThreadExtensionFunction() {
493}
494
495IOThreadExtensionFunction*
496IOThreadExtensionFunction::AsIOThreadExtensionFunction() {
497 return this;
498}
499
500void IOThreadExtensionFunction::Destruct() const {
501 BrowserThread::DeleteOnIOThread::Destruct(this);
502}
503
504void IOThreadExtensionFunction::SendResponse(bool success) {
[email protected]35548ab2013-05-15 08:59:47505 SendResponseImpl(success);
[email protected]703e807a2009-03-28 19:56:51506}
[email protected]73404a372009-04-17 23:09:10507
[email protected]bdfc03e2011-11-22 00:20:33508AsyncExtensionFunction::AsyncExtensionFunction() {
[email protected]a2aef2e2011-05-26 22:48:12509}
510
511AsyncExtensionFunction::~AsyncExtensionFunction() {
[email protected]35213ce92010-04-08 19:06:15512}
[email protected]3a3d47472010-07-15 21:03:54513
asargentbf199b72014-12-10 00:52:46514ExtensionFunction::ScopedUserGestureForTests::ScopedUserGestureForTests() {
515 UserGestureForTests::GetInstance()->IncrementCount();
516}
517
518ExtensionFunction::ScopedUserGestureForTests::~ScopedUserGestureForTests() {
519 UserGestureForTests::GetInstance()->DecrementCount();
520}
521
[email protected]a0c91a9f2014-05-03 03:41:43522ExtensionFunction::ResponseAction AsyncExtensionFunction::Run() {
523 return RunAsync() ? RespondLater() : RespondNow(Error(error_));
524}
525
[email protected]5b50d882014-05-09 11:37:30526// static
527bool AsyncExtensionFunction::ValidationFailure(
528 AsyncExtensionFunction* function) {
529 return false;
530}
531
[email protected]3a3d47472010-07-15 21:03:54532SyncExtensionFunction::SyncExtensionFunction() {
533}
534
535SyncExtensionFunction::~SyncExtensionFunction() {
536}
537
[email protected]a0c91a9f2014-05-03 03:41:43538ExtensionFunction::ResponseAction SyncExtensionFunction::Run() {
[email protected]32f22502014-05-20 21:31:48539 return RespondNow(RunSync() ? ArgumentList(results_.Pass()) : Error(error_));
[email protected]3a3d47472010-07-15 21:03:54540}
[email protected]c357acb42011-06-09 20:52:42541
[email protected]5b50d882014-05-09 11:37:30542// static
543bool SyncExtensionFunction::ValidationFailure(SyncExtensionFunction* function) {
544 return false;
545}
546
[email protected]c357acb42011-06-09 20:52:42547SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() {
548}
549
550SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() {
551}
552
[email protected]a0c91a9f2014-05-03 03:41:43553ExtensionFunction::ResponseAction SyncIOThreadExtensionFunction::Run() {
[email protected]32f22502014-05-20 21:31:48554 return RespondNow(RunSync() ? ArgumentList(results_.Pass()) : Error(error_));
[email protected]c357acb42011-06-09 20:52:42555}
[email protected]5b50d882014-05-09 11:37:30556
557// static
558bool SyncIOThreadExtensionFunction::ValidationFailure(
559 SyncIOThreadExtensionFunction* function) {
560 return false;
561}