Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: extensions/renderer/bindings/exception_handler.cc

Issue 2961103002: [Extensions Bindings] Add an ExceptionHandler class (Closed)
Patch Set: rebase Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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/renderer/bindings/exception_handler.h"
6
7 #include "base/logging.h"
8 #include "base/strings/stringprintf.h"
9 #include "gin/converter.h"
10
11 namespace extensions {
12
13 ExceptionHandler::ExceptionHandler(
14 const binding::AddConsoleError& add_console_error,
15 const binding::RunJSFunction& run_js)
16 : add_console_error_(add_console_error), run_js_(run_js) {}
17 ExceptionHandler::~ExceptionHandler() {}
18
19 void ExceptionHandler::HandleException(v8::Local<v8::Context> context,
20 const std::string& message,
21 const v8::TryCatch& try_catch) {
jbroman 2017/06/30 18:35:31 Hmm. Minor question here: do we want to reset the
Devlin 2017/07/06 16:49:22 Good idea; done.
22 DCHECK(try_catch.HasCaught());
23
24 v8::Isolate* isolate = context->GetIsolate();
25 v8::HandleScope handle_scope(isolate);
26
27 v8::Local<v8::Message> v8_message = try_catch.Message();
28 std::string full_message =
29 !v8_message.IsEmpty()
30 ? base::StringPrintf("%s: %s", message.c_str(),
31 gin::V8ToString(v8_message->Get()).c_str())
32 : message;
33
34 v8::Local<v8::Function> handler = GetCustomHandler(context);
35 if (!handler.IsEmpty()) {
36 v8::Local<v8::Value> arguments[] = {
37 gin::StringToV8(isolate, full_message), try_catch.Exception(),
38 };
39 v8::TryCatch try_catch(isolate);
jbroman 2017/06/30 18:35:31 Is it desirable to have exceptions here be verbose
Devlin 2017/07/06 16:49:22 Done.
jbroman 2017/07/06 21:09:18 Ah, you're quite correct; I misremembered.
40 run_js_.Run(handler, context, arraysize(arguments), arguments);
41 } else {
42 add_console_error_.Run(context, full_message);
43 }
44 }
45
46 void ExceptionHandler::SetHandlerForContext(v8::Local<v8::Context> context,
47 v8::Local<v8::Function> handler) {
48 v8::Isolate* isolate = context->GetIsolate();
49 for (auto& pair : custom_handlers_) {
50 if (pair.first == context) {
51 pair.second = v8::Global<v8::Function>(isolate, handler);
52 return;
53 }
54 }
55 custom_handlers_.emplace_back(v8::Global<v8::Context>(isolate, context),
56 v8::Global<v8::Function>(isolate, handler));
57 }
58
59 void ExceptionHandler::InvalidateContext(v8::Local<v8::Context> context) {
60 for (auto iter = custom_handlers_.begin(); iter != custom_handlers_.end();
61 ++iter) {
62 if (iter->first == context) {
63 custom_handlers_.erase(iter);
64 break;
65 }
66 }
67 // We erase only the first match we see in the list because we should have at
68 // most one handler per context; assert that there aren't any others.
69 DCHECK(GetCustomHandler(context).IsEmpty());
70 }
71
72 v8::Local<v8::Function> ExceptionHandler::GetCustomHandler(
73 v8::Local<v8::Context> context) {
74 for (auto& pair : custom_handlers_) {
75 if (pair.first == context)
76 return pair.second.Get(context->GetIsolate());
77 }
78 return v8::Local<v8::Function>();
79 }
80
81 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698