OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/extension_function.h" | 5 #include "extensions/browser/extension_function.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/metrics/sparse_histogram.h" | 8 #include "base/metrics/sparse_histogram.h" |
9 #include "chrome/browser/extensions/extension_function_dispatcher.h" | 9 #include "chrome/browser/extensions/extension_function_dispatcher.h" |
10 #include "chrome/browser/extensions/extension_service.h" | 10 #include "chrome/browser/extensions/extension_service.h" |
11 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" | 11 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
12 #include "chrome/common/extensions/extension_messages.h" | 12 #include "chrome/common/extensions/extension_messages.h" |
13 #include "content/public/browser/notification_source.h" | 13 #include "content/public/browser/notification_source.h" |
14 #include "content/public/browser/notification_types.h" | 14 #include "content/public/browser/notification_types.h" |
| 15 #include "content/public/browser/render_frame_host.h" |
15 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
16 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
17 #include "content/public/browser/web_contents_observer.h" | 18 #include "content/public/browser/web_contents_observer.h" |
18 #include "extensions/common/extension_api.h" | 19 #include "extensions/common/extension_api.h" |
19 | 20 |
20 using content::BrowserThread; | 21 using content::BrowserThread; |
21 using content::RenderViewHost; | 22 using content::RenderViewHost; |
22 using content::WebContents; | 23 using content::WebContents; |
23 using extensions::ExtensionAPI; | 24 using extensions::ExtensionAPI; |
24 using extensions::Feature; | 25 using extensions::Feature; |
25 | 26 |
26 // static | 27 // static |
27 void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) { | 28 void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) { |
28 x->Destruct(); | 29 x->Destruct(); |
29 } | 30 } |
30 | 31 |
31 // Helper class to track the lifetime of ExtensionFunction's RenderViewHost | 32 // Helper class to track the lifetime of ExtensionFunction's RenderViewHost or |
32 // pointer and NULL it out when it dies. It also allows us to filter IPC | 33 // RenderFrameHost pointer and NULL it out when it dies. It also allows us to |
33 // messages coming from the RenderViewHost. | 34 // filter IPC messages coming from the RenderViewHost/RenderFrameHost. |
34 class UIThreadExtensionFunction::RenderViewHostTracker | 35 class UIThreadExtensionFunction::RenderHostTracker |
35 : public content::WebContentsObserver { | 36 : public content::WebContentsObserver { |
36 public: | 37 public: |
37 explicit RenderViewHostTracker(UIThreadExtensionFunction* function) | 38 explicit RenderHostTracker(UIThreadExtensionFunction* function) |
38 : content::WebContentsObserver( | 39 : content::WebContentsObserver( |
39 WebContents::FromRenderViewHost(function->render_view_host())), | 40 function->render_view_host() ? |
| 41 WebContents::FromRenderViewHost(function->render_view_host()) : |
| 42 WebContents::FromRenderFrameHost( |
| 43 function->render_frame_host())), |
40 function_(function) { | 44 function_(function) { |
41 } | 45 } |
42 | 46 |
43 private: | 47 private: |
44 // content::WebContentsObserver: | 48 // content::WebContentsObserver: |
45 virtual void RenderViewDeleted( | 49 virtual void RenderViewDeleted( |
46 content::RenderViewHost* render_view_host) OVERRIDE { | 50 content::RenderViewHost* render_view_host) OVERRIDE { |
47 if (render_view_host != function_->render_view_host()) | 51 if (render_view_host != function_->render_view_host()) |
48 return; | 52 return; |
49 | 53 |
50 function_->SetRenderViewHost(NULL); | 54 function_->SetRenderViewHost(NULL); |
51 } | 55 } |
| 56 virtual void RenderFrameDeleted( |
| 57 content::RenderFrameHost* render_frame_host) OVERRIDE { |
| 58 if (render_frame_host != function_->render_frame_host()) |
| 59 return; |
| 60 |
| 61 function_->SetRenderFrameHost(NULL); |
| 62 } |
52 | 63 |
53 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { | 64 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { |
54 return function_->OnMessageReceivedFromRenderView(message); | 65 return function_->OnMessageReceived(message); |
55 } | 66 } |
56 | 67 |
57 UIThreadExtensionFunction* function_; | 68 UIThreadExtensionFunction* function_; |
58 | 69 |
59 DISALLOW_COPY_AND_ASSIGN(RenderViewHostTracker); | 70 DISALLOW_COPY_AND_ASSIGN(RenderHostTracker); |
60 }; | 71 }; |
61 | 72 |
62 ExtensionFunction::ExtensionFunction() | 73 ExtensionFunction::ExtensionFunction() |
63 : request_id_(-1), | 74 : request_id_(-1), |
64 profile_id_(NULL), | 75 profile_id_(NULL), |
65 has_callback_(false), | 76 has_callback_(false), |
66 include_incognito_(false), | 77 include_incognito_(false), |
67 user_gesture_(false), | 78 user_gesture_(false), |
68 bad_message_(false), | 79 bad_message_(false), |
69 histogram_value_(extensions::functions::UNKNOWN) {} | 80 histogram_value_(extensions::functions::UNKNOWN) {} |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 } | 150 } |
140 | 151 |
141 // If results were never set, we send an empty argument list. | 152 // If results were never set, we send an empty argument list. |
142 if (!results_) | 153 if (!results_) |
143 results_.reset(new base::ListValue()); | 154 results_.reset(new base::ListValue()); |
144 | 155 |
145 response_callback_.Run(type, *results_, GetError()); | 156 response_callback_.Run(type, *results_, GetError()); |
146 } | 157 } |
147 | 158 |
148 UIThreadExtensionFunction::UIThreadExtensionFunction() | 159 UIThreadExtensionFunction::UIThreadExtensionFunction() |
149 : render_view_host_(NULL), context_(NULL), delegate_(NULL) {} | 160 : render_view_host_(NULL), render_frame_host_(NULL), context_(NULL), |
| 161 delegate_(NULL) {} |
150 | 162 |
151 UIThreadExtensionFunction::~UIThreadExtensionFunction() { | 163 UIThreadExtensionFunction::~UIThreadExtensionFunction() { |
152 if (dispatcher() && render_view_host()) | 164 if (dispatcher() && render_view_host()) |
153 dispatcher()->OnExtensionFunctionCompleted(GetExtension()); | 165 dispatcher()->OnExtensionFunctionCompleted(GetExtension()); |
154 } | 166 } |
155 | 167 |
156 UIThreadExtensionFunction* | 168 UIThreadExtensionFunction* |
157 UIThreadExtensionFunction::AsUIThreadExtensionFunction() { | 169 UIThreadExtensionFunction::AsUIThreadExtensionFunction() { |
158 return this; | 170 return this; |
159 } | 171 } |
160 | 172 |
161 bool UIThreadExtensionFunction::OnMessageReceivedFromRenderView( | 173 bool UIThreadExtensionFunction::OnMessageReceived(const IPC::Message& message) { |
162 const IPC::Message& message) { | |
163 return false; | 174 return false; |
164 } | 175 } |
165 | 176 |
166 void UIThreadExtensionFunction::Destruct() const { | 177 void UIThreadExtensionFunction::Destruct() const { |
167 BrowserThread::DeleteOnUIThread::Destruct(this); | 178 BrowserThread::DeleteOnUIThread::Destruct(this); |
168 } | 179 } |
169 | 180 |
170 void UIThreadExtensionFunction::SetRenderViewHost( | 181 void UIThreadExtensionFunction::SetRenderViewHost( |
171 RenderViewHost* render_view_host) { | 182 RenderViewHost* render_view_host) { |
| 183 DCHECK(!render_frame_host_); |
172 render_view_host_ = render_view_host; | 184 render_view_host_ = render_view_host; |
173 tracker_.reset(render_view_host ? new RenderViewHostTracker(this) : NULL); | 185 tracker_.reset(render_view_host ? new RenderHostTracker(this) : NULL); |
| 186 } |
| 187 |
| 188 void UIThreadExtensionFunction::SetRenderFrameHost( |
| 189 content::RenderFrameHost* render_frame_host) { |
| 190 DCHECK(!render_view_host_); |
| 191 render_frame_host_ = render_frame_host; |
| 192 tracker_.reset(render_frame_host ? new RenderHostTracker(this) : NULL); |
174 } | 193 } |
175 | 194 |
176 content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() { | 195 content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() { |
177 content::WebContents* web_contents = NULL; | 196 content::WebContents* web_contents = NULL; |
178 if (dispatcher()) | 197 if (dispatcher()) |
179 web_contents = dispatcher()->delegate()->GetAssociatedWebContents(); | 198 web_contents = dispatcher()->delegate()->GetAssociatedWebContents(); |
180 | 199 |
181 return web_contents; | 200 return web_contents; |
182 } | 201 } |
183 | 202 |
184 void UIThreadExtensionFunction::SendResponse(bool success) { | 203 void UIThreadExtensionFunction::SendResponse(bool success) { |
185 if (delegate_) | 204 if (delegate_) |
186 delegate_->OnSendResponse(this, success, bad_message_); | 205 delegate_->OnSendResponse(this, success, bad_message_); |
187 else | 206 else |
188 SendResponseImpl(success); | 207 SendResponseImpl(success); |
189 } | 208 } |
190 | 209 |
191 void UIThreadExtensionFunction::WriteToConsole( | 210 void UIThreadExtensionFunction::WriteToConsole( |
192 content::ConsoleMessageLevel level, | 211 content::ConsoleMessageLevel level, |
193 const std::string& message) { | 212 const std::string& message) { |
194 render_view_host_->Send(new ExtensionMsg_AddMessageToConsole( | 213 if (render_view_host_) { |
195 render_view_host_->GetRoutingID(), level, message)); | 214 render_view_host_->Send(new ExtensionMsg_AddMessageToConsole( |
| 215 render_view_host_->GetRoutingID(), level, message)); |
| 216 } else { |
| 217 render_frame_host_->Send(new ExtensionMsg_AddMessageToConsole( |
| 218 render_frame_host_->GetRoutingID(), level, message)); |
| 219 } |
196 } | 220 } |
197 | 221 |
198 IOThreadExtensionFunction::IOThreadExtensionFunction() | 222 IOThreadExtensionFunction::IOThreadExtensionFunction() |
199 : routing_id_(MSG_ROUTING_NONE) { | 223 : routing_id_(MSG_ROUTING_NONE) { |
200 } | 224 } |
201 | 225 |
202 IOThreadExtensionFunction::~IOThreadExtensionFunction() { | 226 IOThreadExtensionFunction::~IOThreadExtensionFunction() { |
203 } | 227 } |
204 | 228 |
205 IOThreadExtensionFunction* | 229 IOThreadExtensionFunction* |
(...skipping 27 matching lines...) Expand all Loading... |
233 | 257 |
234 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { | 258 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { |
235 } | 259 } |
236 | 260 |
237 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { | 261 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { |
238 } | 262 } |
239 | 263 |
240 void SyncIOThreadExtensionFunction::Run() { | 264 void SyncIOThreadExtensionFunction::Run() { |
241 SendResponse(RunImpl()); | 265 SendResponse(RunImpl()); |
242 } | 266 } |
OLD | NEW |