1 // Copyright (c) 2012 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.
5 #include "chrome/browser/extensions/extension_function.h"
7 #include "base/logging.h"
8 #include "base/metrics/histogram.h"
9 #include "chrome/browser/extensions/extension_function_dispatcher.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/renderer_host/chrome_render_message_filter.h"
12 #include "chrome/common/extensions/api/extension_api.h"
13 #include "chrome/common/extensions/extension_messages.h"
14 #include "content/public/browser/notification_source.h"
15 #include "content/public/browser/notification_types.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/browser/web_contents_observer.h"
20 using content::BrowserThread;
21 using content::RenderViewHost;
22 using content::WebContents;
23 using extensions::ExtensionAPI;
24 using extensions::Feature;
27 void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) {
31 // Helper class to track the lifetime of ExtensionFunction's RenderViewHost
32 // pointer and NULL it out when it dies. It also allows us to filter IPC
33 // messages coming from the RenderViewHost.
34 class UIThreadExtensionFunction::RenderViewHostTracker
35 : public content::WebContentsObserver {
37 explicit RenderViewHostTracker(UIThreadExtensionFunction* function)
38 : content::WebContentsObserver(
39 WebContents::FromRenderViewHost(function->render_view_host())),
44 // content::WebContentsObserver:
45 virtual void RenderViewDeleted(
46 content::RenderViewHost* render_view_host) OVERRIDE {
47 if (render_view_host != function_->render_view_host())
50 function_->SetRenderViewHost(NULL);
53 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
54 return function_->OnMessageReceivedFromRenderView(message);
57 UIThreadExtensionFunction* function_;
59 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTracker);
62 ExtensionFunction::ExtensionFunction()
66 include_incognito_(false),
69 histogram_value_(extensions::functions::UNKNOWN) {}
71 ExtensionFunction::~ExtensionFunction() {
74 UIThreadExtensionFunction* ExtensionFunction::AsUIThreadExtensionFunction() {
78 IOThreadExtensionFunction* ExtensionFunction::AsIOThreadExtensionFunction() {
82 bool ExtensionFunction::HasPermission() {
83 Feature::Availability availability =
84 ExtensionAPI::GetSharedInstance()->IsAvailable(
85 name_, extension_, Feature::BLESSED_EXTENSION_CONTEXT, source_url());
86 return availability.is_available();
89 void ExtensionFunction::OnQuotaExceeded(const std::string& violation_error) {
90 error_ = violation_error;
94 void ExtensionFunction::SetArgs(const base::ListValue* args) {
95 DCHECK(!args_.get()); // Should only be called once.
96 args_.reset(args->DeepCopy());
99 void ExtensionFunction::SetResult(base::Value* result) {
100 results_.reset(new base::ListValue());
101 results_->Append(result);
104 const base::ListValue* ExtensionFunction::GetResultList() {
105 return results_.get();
108 const std::string ExtensionFunction::GetError() {
112 void ExtensionFunction::SetError(const std::string& error) {
116 void ExtensionFunction::Run() {
117 UMA_HISTOGRAM_ENUMERATION("Extensions.FunctionCalls", histogram_value(),
118 extensions::functions::ENUM_BOUNDARY);
124 bool ExtensionFunction::ShouldSkipQuotaLimiting() const {
128 bool ExtensionFunction::HasOptionalArgument(size_t index) {
130 return args_->Get(index, &value) && !value->IsType(Value::TYPE_NULL);
133 void ExtensionFunction::SendResponseImpl(bool success) {
134 DCHECK(!response_callback_.is_null());
136 ResponseType type = success ? SUCCEEDED : FAILED;
139 LOG(ERROR) << "Bad extension message " << name_;
142 // If results were never set, we send an empty argument list.
144 results_.reset(new base::ListValue());
146 response_callback_.Run(type, *results_, GetError());
149 UIThreadExtensionFunction::UIThreadExtensionFunction()
150 : render_view_host_(NULL), context_(NULL), delegate_(NULL) {}
152 UIThreadExtensionFunction::~UIThreadExtensionFunction() {
153 if (dispatcher() && render_view_host())
154 dispatcher()->OnExtensionFunctionCompleted(GetExtension());
157 UIThreadExtensionFunction*
158 UIThreadExtensionFunction::AsUIThreadExtensionFunction() {
162 bool UIThreadExtensionFunction::OnMessageReceivedFromRenderView(
163 const IPC::Message& message) {
167 void UIThreadExtensionFunction::Destruct() const {
168 BrowserThread::DeleteOnUIThread::Destruct(this);
171 void UIThreadExtensionFunction::SetRenderViewHost(
172 RenderViewHost* render_view_host) {
173 render_view_host_ = render_view_host;
174 tracker_.reset(render_view_host ? new RenderViewHostTracker(this) : NULL);
177 content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() {
178 content::WebContents* web_contents = NULL;
180 web_contents = dispatcher()->delegate()->GetAssociatedWebContents();
185 void UIThreadExtensionFunction::SendResponse(bool success) {
187 delegate_->OnSendResponse(this, success, bad_message_);
189 SendResponseImpl(success);
192 void UIThreadExtensionFunction::WriteToConsole(
193 content::ConsoleMessageLevel level,
194 const std::string& message) {
195 render_view_host_->Send(new ExtensionMsg_AddMessageToConsole(
196 render_view_host_->GetRoutingID(), level, message));
199 IOThreadExtensionFunction::IOThreadExtensionFunction()
200 : routing_id_(MSG_ROUTING_NONE) {
203 IOThreadExtensionFunction::~IOThreadExtensionFunction() {
206 IOThreadExtensionFunction*
207 IOThreadExtensionFunction::AsIOThreadExtensionFunction() {
211 void IOThreadExtensionFunction::Destruct() const {
212 BrowserThread::DeleteOnIOThread::Destruct(this);
215 void IOThreadExtensionFunction::SendResponse(bool success) {
216 SendResponseImpl(success);
219 AsyncExtensionFunction::AsyncExtensionFunction() {
222 AsyncExtensionFunction::~AsyncExtensionFunction() {
225 SyncExtensionFunction::SyncExtensionFunction() {
228 SyncExtensionFunction::~SyncExtensionFunction() {
231 void SyncExtensionFunction::Run() {
232 SendResponse(RunImpl());
235 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() {
238 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() {
241 void SyncIOThreadExtensionFunction::Run() {
242 SendResponse(RunImpl());