Chromium Code Reviews| 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) { |
| 172 render_view_host_ = render_view_host; | 183 render_view_host_ = render_view_host; |
| 173 tracker_.reset(render_view_host ? new RenderViewHostTracker(this) : NULL); | 184 tracker_.reset(render_view_host ? new RenderHostTracker(this) : NULL); |
| 185 } | |
| 186 | |
| 187 void UIThreadExtensionFunction::SetRenderFrameHost( | |
| 188 content::RenderFrameHost* render_frame_host) { | |
| 189 render_frame_host_ = render_frame_host; | |
|
nasko
2013/12/20 14:53:54
nit: Should we DCHECK somewhere that one or the ot
jam
2013/12/20 17:00:49
Done.
| |
| 190 tracker_.reset(render_frame_host ? new RenderHostTracker(this) : NULL); | |
| 174 } | 191 } |
| 175 | 192 |
| 176 content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() { | 193 content::WebContents* UIThreadExtensionFunction::GetAssociatedWebContents() { |
| 177 content::WebContents* web_contents = NULL; | 194 content::WebContents* web_contents = NULL; |
| 178 if (dispatcher()) | 195 if (dispatcher()) |
| 179 web_contents = dispatcher()->delegate()->GetAssociatedWebContents(); | 196 web_contents = dispatcher()->delegate()->GetAssociatedWebContents(); |
| 180 | 197 |
| 181 return web_contents; | 198 return web_contents; |
| 182 } | 199 } |
| 183 | 200 |
| 184 void UIThreadExtensionFunction::SendResponse(bool success) { | 201 void UIThreadExtensionFunction::SendResponse(bool success) { |
| 185 if (delegate_) | 202 if (delegate_) |
| 186 delegate_->OnSendResponse(this, success, bad_message_); | 203 delegate_->OnSendResponse(this, success, bad_message_); |
| 187 else | 204 else |
| 188 SendResponseImpl(success); | 205 SendResponseImpl(success); |
| 189 } | 206 } |
| 190 | 207 |
| 191 void UIThreadExtensionFunction::WriteToConsole( | 208 void UIThreadExtensionFunction::WriteToConsole( |
| 192 content::ConsoleMessageLevel level, | 209 content::ConsoleMessageLevel level, |
| 193 const std::string& message) { | 210 const std::string& message) { |
| 194 render_view_host_->Send(new ExtensionMsg_AddMessageToConsole( | 211 if (render_view_host_) { |
| 195 render_view_host_->GetRoutingID(), level, message)); | 212 render_view_host_->Send(new ExtensionMsg_AddMessageToConsole( |
| 213 render_view_host_->GetRoutingID(), level, message)); | |
| 214 } else { | |
| 215 render_frame_host_->Send(new ExtensionMsg_AddMessageToConsole( | |
| 216 render_frame_host_->GetRoutingID(), level, message)); | |
| 217 } | |
| 196 } | 218 } |
| 197 | 219 |
| 198 IOThreadExtensionFunction::IOThreadExtensionFunction() | 220 IOThreadExtensionFunction::IOThreadExtensionFunction() |
| 199 : routing_id_(MSG_ROUTING_NONE) { | 221 : routing_id_(MSG_ROUTING_NONE) { |
| 200 } | 222 } |
| 201 | 223 |
| 202 IOThreadExtensionFunction::~IOThreadExtensionFunction() { | 224 IOThreadExtensionFunction::~IOThreadExtensionFunction() { |
| 203 } | 225 } |
| 204 | 226 |
| 205 IOThreadExtensionFunction* | 227 IOThreadExtensionFunction* |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 233 | 255 |
| 234 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { | 256 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { |
| 235 } | 257 } |
| 236 | 258 |
| 237 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { | 259 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { |
| 238 } | 260 } |
| 239 | 261 |
| 240 void SyncIOThreadExtensionFunction::Run() { | 262 void SyncIOThreadExtensionFunction::Run() { |
| 241 SendResponse(RunImpl()); | 263 SendResponse(RunImpl()); |
| 242 } | 264 } |
| OLD | NEW |