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