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 "base/metrics/sparse_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/extension_messages.h"
13 #include "content/public/browser/notification_source.h"
14 #include "content/public/browser/notification_types.h"
15 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/web_contents.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "extensions/common/extension_api.h"
19
20 using content::BrowserThread;
21 using content::RenderViewHost;
22 using content::WebContents;
23 using extensions::ExtensionAPI;
24 using extensions::Feature;
25
26 // static
Destruct(const ExtensionFunction * x)27 void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) {
28 x->Destruct();
29 }
30
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 {
36 public:
RenderViewHostTracker(UIThreadExtensionFunction * function)37 explicit RenderViewHostTracker(UIThreadExtensionFunction* function)
38 : content::WebContentsObserver(
39 WebContents::FromRenderViewHost(function->render_view_host())),
40 function_(function) {
41 }
42
43 private:
44 // content::WebContentsObserver:
RenderViewDeleted(content::RenderViewHost * render_view_host)45 virtual void RenderViewDeleted(
46 content::RenderViewHost* render_view_host) OVERRIDE {
47 if (render_view_host != function_->render_view_host())
48 return;
49
50 function_->SetRenderViewHost(NULL);
51 }
52
OnMessageReceived(const IPC::Message & message)53 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
54 return function_->OnMessageReceivedFromRenderView(message);
55 }
56
57 UIThreadExtensionFunction* function_;
58
59 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTracker);
60 };
61
ExtensionFunction()62 ExtensionFunction::ExtensionFunction()
63 : request_id_(-1),
64 profile_id_(NULL),
65 has_callback_(false),
66 include_incognito_(false),
67 user_gesture_(false),
68 bad_message_(false),
69 histogram_value_(extensions::functions::UNKNOWN) {}
70
~ExtensionFunction()71 ExtensionFunction::~ExtensionFunction() {
72 }
73
AsUIThreadExtensionFunction()74 UIThreadExtensionFunction* ExtensionFunction::AsUIThreadExtensionFunction() {
75 return NULL;
76 }
77
AsIOThreadExtensionFunction()78 IOThreadExtensionFunction* ExtensionFunction::AsIOThreadExtensionFunction() {
79 return NULL;
80 }
81
HasPermission()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();
87 }
88
OnQuotaExceeded(const std::string & violation_error)89 void ExtensionFunction::OnQuotaExceeded(const std::string& violation_error) {
90 error_ = violation_error;
91 SendResponse(false);
92 }
93
SetArgs(const base::ListValue * args)94 void ExtensionFunction::SetArgs(const base::ListValue* args) {
95 DCHECK(!args_.get()); // Should only be called once.
96 args_.reset(args->DeepCopy());
97 }
98
SetResult(base::Value * result)99 void ExtensionFunction::SetResult(base::Value* result) {
100 results_.reset(new base::ListValue());
101 results_->Append(result);
102 }
103
GetResultList()104 const base::ListValue* ExtensionFunction::GetResultList() {
105 return results_.get();
106 }
107
GetError()108 const std::string ExtensionFunction::GetError() {
109 return error_;
110 }
111
SetError(const std::string & error)112 void ExtensionFunction::SetError(const std::string& error) {
113 error_ = error;
114 }
115
Run()116 void ExtensionFunction::Run() {
117 UMA_HISTOGRAM_SPARSE_SLOWLY("Extensions.FunctionCalls", histogram_value());
118
119 if (!RunImpl())
120 SendResponse(false);
121 }
122
ShouldSkipQuotaLimiting() const123 bool ExtensionFunction::ShouldSkipQuotaLimiting() const {
124 return false;
125 }
126
HasOptionalArgument(size_t index)127 bool ExtensionFunction::HasOptionalArgument(size_t index) {
128 Value* value;
129 return args_->Get(index, &value) && !value->IsType(Value::TYPE_NULL);
130 }
131
SendResponseImpl(bool success)132 void ExtensionFunction::SendResponseImpl(bool success) {
133 DCHECK(!response_callback_.is_null());
134
135 ResponseType type = success ? SUCCEEDED : FAILED;
136 if (bad_message_) {
137 type = BAD_MESSAGE;
138 LOG(ERROR) << "Bad extension message " << name_;
139 }
140
141 // If results were never set, we send an empty argument list.
142 if (!results_)
143 results_.reset(new base::ListValue());
144
145 response_callback_.Run(type, *results_, GetError());
146 }
147
UIThreadExtensionFunction()148 UIThreadExtensionFunction::UIThreadExtensionFunction()
149 : render_view_host_(NULL), context_(NULL), delegate_(NULL) {}
150
~UIThreadExtensionFunction()151 UIThreadExtensionFunction::~UIThreadExtensionFunction() {
152 if (dispatcher() && render_view_host())
153 dispatcher()->OnExtensionFunctionCompleted(GetExtension());
154 }
155
156 UIThreadExtensionFunction*
AsUIThreadExtensionFunction()157 UIThreadExtensionFunction::AsUIThreadExtensionFunction() {
158 return this;
159 }
160
OnMessageReceivedFromRenderView(const IPC::Message & message)161 bool UIThreadExtensionFunction::OnMessageReceivedFromRenderView(
162 const IPC::Message& message) {
163 return false;
164 }
165
Destruct() const166 void UIThreadExtensionFunction::Destruct() const {
167 BrowserThread::DeleteOnUIThread::Destruct(this);
168 }
169
SetRenderViewHost(RenderViewHost * render_view_host)170 void UIThreadExtensionFunction::SetRenderViewHost(
171 RenderViewHost* render_view_host) {
172 render_view_host_ = render_view_host;
173 tracker_.reset(render_view_host ? new RenderViewHostTracker(this) : NULL);
174 }
175
GetAssociatedWebContents()176 content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() {
177 content::WebContents* web_contents = NULL;
178 if (dispatcher())
179 web_contents = dispatcher()->delegate()->GetAssociatedWebContents();
180
181 return web_contents;
182 }
183
SendResponse(bool success)184 void UIThreadExtensionFunction::SendResponse(bool success) {
185 if (delegate_)
186 delegate_->OnSendResponse(this, success, bad_message_);
187 else
188 SendResponseImpl(success);
189 }
190
WriteToConsole(content::ConsoleMessageLevel level,const std::string & message)191 void UIThreadExtensionFunction::WriteToConsole(
192 content::ConsoleMessageLevel level,
193 const std::string& message) {
194 render_view_host_->Send(new ExtensionMsg_AddMessageToConsole(
195 render_view_host_->GetRoutingID(), level, message));
196 }
197
IOThreadExtensionFunction()198 IOThreadExtensionFunction::IOThreadExtensionFunction()
199 : routing_id_(MSG_ROUTING_NONE) {
200 }
201
~IOThreadExtensionFunction()202 IOThreadExtensionFunction::~IOThreadExtensionFunction() {
203 }
204
205 IOThreadExtensionFunction*
AsIOThreadExtensionFunction()206 IOThreadExtensionFunction::AsIOThreadExtensionFunction() {
207 return this;
208 }
209
Destruct() const210 void IOThreadExtensionFunction::Destruct() const {
211 BrowserThread::DeleteOnIOThread::Destruct(this);
212 }
213
SendResponse(bool success)214 void IOThreadExtensionFunction::SendResponse(bool success) {
215 SendResponseImpl(success);
216 }
217
AsyncExtensionFunction()218 AsyncExtensionFunction::AsyncExtensionFunction() {
219 }
220
~AsyncExtensionFunction()221 AsyncExtensionFunction::~AsyncExtensionFunction() {
222 }
223
SyncExtensionFunction()224 SyncExtensionFunction::SyncExtensionFunction() {
225 }
226
~SyncExtensionFunction()227 SyncExtensionFunction::~SyncExtensionFunction() {
228 }
229
Run()230 void SyncExtensionFunction::Run() {
231 SendResponse(RunImpl());
232 }
233
SyncIOThreadExtensionFunction()234 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() {
235 }
236
~SyncIOThreadExtensionFunction()237 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() {
238 }
239
Run()240 void SyncIOThreadExtensionFunction::Run() {
241 SendResponse(RunImpl());
242 }
243