| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/extensions/extension_function.h" | 5 #include "chrome/browser/extensions/extension_function.h" |
| 6 | 6 |
| 7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.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/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/renderer_host/chrome_render_message_filter.h" |
| 12 #include "chrome/common/extensions/extension_messages.h" | 13 #include "chrome/common/extensions/extension_messages.h" |
| 13 #include "content/browser/renderer_host/render_process_host.h" | 14 #include "content/browser/renderer_host/render_process_host.h" |
| 14 #include "content/browser/renderer_host/render_view_host.h" | 15 #include "content/browser/renderer_host/render_view_host.h" |
| 15 #include "content/browser/user_metrics.h" | 16 #include "content/browser/user_metrics.h" |
| 16 #include "content/common/notification_source.h" | 17 #include "content/common/notification_source.h" |
| 17 #include "content/common/notification_type.h" | 18 #include "content/common/notification_type.h" |
| 18 #include "content/common/result_codes.h" | 19 #include "content/common/result_codes.h" |
| 19 | 20 |
| 20 // static | 21 // static |
| 21 void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) { | 22 void ExtensionFunctionDeleteTraits::Destruct(const ExtensionFunction* x) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 35 const NotificationSource& source, | 36 const NotificationSource& source, |
| 36 const NotificationDetails& details) { | 37 const NotificationDetails& details) { |
| 37 CHECK(type == NotificationType::RENDER_VIEW_HOST_DELETED); | 38 CHECK(type == NotificationType::RENDER_VIEW_HOST_DELETED); |
| 38 CHECK(Source<RenderViewHost>(source).ptr() == | 39 CHECK(Source<RenderViewHost>(source).ptr() == |
| 39 function_->render_view_host()); | 40 function_->render_view_host()); |
| 40 function_->SetRenderViewHost(NULL); | 41 function_->SetRenderViewHost(NULL); |
| 41 } | 42 } |
| 42 | 43 |
| 43 ExtensionFunction::ExtensionFunction() | 44 ExtensionFunction::ExtensionFunction() |
| 44 : request_id_(-1), | 45 : request_id_(-1), |
| 45 profile_id_(0), | 46 profile_id_(Profile::kInvalidProfileId), |
| 46 has_callback_(false), | 47 has_callback_(false), |
| 47 include_incognito_(false), | 48 include_incognito_(false), |
| 48 user_gesture_(false), | 49 user_gesture_(false), |
| 49 args_(NULL), | 50 args_(NULL), |
| 50 bad_message_(false) { | 51 bad_message_(false) { |
| 51 } | 52 } |
| 52 | 53 |
| 53 ExtensionFunction::~ExtensionFunction() { | 54 ExtensionFunction::~ExtensionFunction() { |
| 54 } | 55 } |
| 55 | 56 |
| 56 UIThreadExtensionFunction* ExtensionFunction::AsUIThreadExtensionFunction() { | 57 UIThreadExtensionFunction* ExtensionFunction::AsUIThreadExtensionFunction() { |
| 57 return NULL; | 58 return NULL; |
| 58 } | 59 } |
| 59 | 60 |
| 61 IOThreadExtensionFunction* ExtensionFunction::AsIOThreadExtensionFunction() { |
| 62 return NULL; |
| 63 } |
| 64 |
| 60 void ExtensionFunction::SetArgs(const ListValue* args) { | 65 void ExtensionFunction::SetArgs(const ListValue* args) { |
| 61 DCHECK(!args_.get()); // Should only be called once. | 66 DCHECK(!args_.get()); // Should only be called once. |
| 62 args_.reset(args->DeepCopy()); | 67 args_.reset(args->DeepCopy()); |
| 63 } | 68 } |
| 64 | 69 |
| 65 const std::string ExtensionFunction::GetResult() { | 70 const std::string ExtensionFunction::GetResult() { |
| 66 std::string json; | 71 std::string json; |
| 67 // Some functions might not need to return any results. | 72 // Some functions might not need to return any results. |
| 68 if (result_.get()) | 73 if (result_.get()) |
| 69 base::JSONWriter::Write(result_.get(), false, &json); | 74 base::JSONWriter::Write(result_.get(), false, &json); |
| 70 return json; | 75 return json; |
| 71 } | 76 } |
| 72 | 77 |
| 73 const std::string ExtensionFunction::GetError() { | 78 const std::string ExtensionFunction::GetError() { |
| 74 return error_; | 79 return error_; |
| 75 } | 80 } |
| 76 | 81 |
| 77 void ExtensionFunction::Run() { | 82 void ExtensionFunction::Run() { |
| 78 if (!RunImpl()) | 83 if (!RunImpl()) |
| 79 SendResponse(false); | 84 SendResponse(false); |
| 80 } | 85 } |
| 81 | 86 |
| 82 bool ExtensionFunction::HasOptionalArgument(size_t index) { | 87 bool ExtensionFunction::HasOptionalArgument(size_t index) { |
| 83 Value* value; | 88 Value* value; |
| 84 return args_->Get(index, &value) && !value->IsType(Value::TYPE_NULL); | 89 return args_->Get(index, &value) && !value->IsType(Value::TYPE_NULL); |
| 85 } | 90 } |
| 86 | 91 |
| 92 void ExtensionFunction::SendResponseImpl(base::ProcessHandle process, |
| 93 IPC::Message::Sender* ipc_sender, |
| 94 int routing_id, |
| 95 bool success) { |
| 96 DCHECK(ipc_sender); |
| 97 if (bad_message_) { |
| 98 HandleBadMessage(process); |
| 99 return; |
| 100 } |
| 101 |
| 102 ipc_sender->Send(new ExtensionMsg_Response( |
| 103 routing_id, request_id_, success, GetResult(), GetError())); |
| 104 } |
| 105 |
| 106 void ExtensionFunction::HandleBadMessage(base::ProcessHandle process) { |
| 107 LOG(ERROR) << "bad extension message " << name_ << " : terminating renderer."; |
| 108 if (RenderProcessHost::run_renderer_in_process()) { |
| 109 // In single process mode it is better if we don't suicide but just crash. |
| 110 CHECK(false); |
| 111 } else { |
| 112 NOTREACHED(); |
| 113 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_EFD")); |
| 114 if (process) |
| 115 base::KillProcess(process, ResultCodes::KILLED_BAD_MESSAGE, false); |
| 116 } |
| 117 } |
| 87 UIThreadExtensionFunction::UIThreadExtensionFunction() | 118 UIThreadExtensionFunction::UIThreadExtensionFunction() |
| 88 : profile_(NULL) { | 119 : render_view_host_(NULL), profile_(NULL) { |
| 89 } | 120 } |
| 90 | 121 |
| 91 UIThreadExtensionFunction::~UIThreadExtensionFunction() { | 122 UIThreadExtensionFunction::~UIThreadExtensionFunction() { |
| 92 } | 123 } |
| 93 | 124 |
| 94 UIThreadExtensionFunction* | 125 UIThreadExtensionFunction* |
| 95 UIThreadExtensionFunction::AsUIThreadExtensionFunction() { | 126 UIThreadExtensionFunction::AsUIThreadExtensionFunction() { |
| 96 return this; | 127 return this; |
| 97 } | 128 } |
| 98 | 129 |
| 99 void UIThreadExtensionFunction::Destruct() const { | 130 void UIThreadExtensionFunction::Destruct() const { |
| 100 BrowserThread::DeleteOnUIThread::Destruct(this); | 131 BrowserThread::DeleteOnUIThread::Destruct(this); |
| 101 } | 132 } |
| 102 | 133 |
| 103 void UIThreadExtensionFunction::SetRenderViewHost( | 134 void UIThreadExtensionFunction::SetRenderViewHost( |
| 104 RenderViewHost* render_view_host) { | 135 RenderViewHost* render_view_host) { |
| 105 render_view_host_ = render_view_host; | 136 render_view_host_ = render_view_host; |
| 106 tracker_.reset(render_view_host ? new RenderViewHostTracker(this) : NULL); | 137 tracker_.reset(render_view_host ? new RenderViewHostTracker(this) : NULL); |
| 107 } | 138 } |
| 108 | 139 |
| 109 Browser* UIThreadExtensionFunction::GetCurrentBrowser() { | 140 Browser* UIThreadExtensionFunction::GetCurrentBrowser() { |
| 110 return dispatcher()->GetCurrentBrowser(render_view_host_, include_incognito_); | 141 return dispatcher()->GetCurrentBrowser(render_view_host_, include_incognito_); |
| 111 } | 142 } |
| 112 | 143 |
| 113 void UIThreadExtensionFunction::SendResponse(bool success) { | 144 void UIThreadExtensionFunction::SendResponse(bool success) { |
| 114 if (!render_view_host_ || !dispatcher()) | 145 if (!render_view_host_ || !dispatcher()) |
| 115 return; | 146 return; |
| 116 if (bad_message_) { | |
| 117 HandleBadMessage(); | |
| 118 return; | |
| 119 } | |
| 120 | 147 |
| 121 render_view_host_->Send(new ExtensionMsg_Response( | 148 SendResponseImpl(render_view_host_->process()->GetHandle(), |
| 122 render_view_host_->routing_id(), request_id_, success, | 149 render_view_host_, |
| 123 GetResult(), GetError())); | 150 render_view_host_->routing_id(), |
| 151 success); |
| 124 } | 152 } |
| 125 | 153 |
| 126 void UIThreadExtensionFunction::HandleBadMessage() { | 154 IOThreadExtensionFunction::IOThreadExtensionFunction() |
| 127 LOG(ERROR) << "bad extension message " << name_ << " : terminating renderer."; | 155 : routing_id_(-1) { |
| 128 if (RenderProcessHost::run_renderer_in_process()) { | 156 } |
| 129 // In single process mode it is better if we don't suicide but just crash. | 157 |
| 130 CHECK(false); | 158 IOThreadExtensionFunction::~IOThreadExtensionFunction() { |
| 131 } else { | 159 } |
| 132 NOTREACHED(); | 160 |
| 133 UserMetrics::RecordAction(UserMetricsAction("BadMessageTerminate_EFD")); | 161 IOThreadExtensionFunction* |
| 134 if (render_view_host_) { | 162 IOThreadExtensionFunction::AsIOThreadExtensionFunction() { |
| 135 base::KillProcess(render_view_host_->process()->GetHandle(), | 163 return this; |
| 136 ResultCodes::KILLED_BAD_MESSAGE, false); | 164 } |
| 137 } | 165 |
| 138 } | 166 void IOThreadExtensionFunction::Destruct() const { |
| 167 BrowserThread::DeleteOnIOThread::Destruct(this); |
| 168 } |
| 169 |
| 170 void IOThreadExtensionFunction::SendResponse(bool success) { |
| 171 if (!ipc_sender()) |
| 172 return; |
| 173 |
| 174 SendResponseImpl(ipc_sender()->peer_handle(), |
| 175 ipc_sender(), routing_id_, success); |
| 139 } | 176 } |
| 140 | 177 |
| 141 AsyncExtensionFunction::AsyncExtensionFunction() { | 178 AsyncExtensionFunction::AsyncExtensionFunction() { |
| 142 } | 179 } |
| 143 | 180 |
| 144 AsyncExtensionFunction::~AsyncExtensionFunction() { | 181 AsyncExtensionFunction::~AsyncExtensionFunction() { |
| 145 } | 182 } |
| 146 | 183 |
| 147 SyncExtensionFunction::SyncExtensionFunction() { | 184 SyncExtensionFunction::SyncExtensionFunction() { |
| 148 } | 185 } |
| 149 | 186 |
| 150 SyncExtensionFunction::~SyncExtensionFunction() { | 187 SyncExtensionFunction::~SyncExtensionFunction() { |
| 151 } | 188 } |
| 152 | 189 |
| 153 void SyncExtensionFunction::Run() { | 190 void SyncExtensionFunction::Run() { |
| 154 SendResponse(RunImpl()); | 191 SendResponse(RunImpl()); |
| 155 } | 192 } |
| 193 |
| 194 SyncIOThreadExtensionFunction::SyncIOThreadExtensionFunction() { |
| 195 } |
| 196 |
| 197 SyncIOThreadExtensionFunction::~SyncIOThreadExtensionFunction() { |
| 198 } |
| 199 |
| 200 void SyncIOThreadExtensionFunction::Run() { |
| 201 SendResponse(RunImpl()); |
| 202 } |
| OLD | NEW |