OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/proximity_auth/webui/proximity_auth_webui_handler.h" | 5 #include "components/proximity_auth/webui/proximity_auth_webui_handler.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/i18n/time_formatting.h" | |
9 #include "base/values.h" | |
10 #include "content/public/browser/web_ui.h" | |
11 | |
7 namespace proximity_auth { | 12 namespace proximity_auth { |
8 | 13 |
14 namespace { | |
15 | |
16 // Keys in the JSON representation of a log message. | |
17 const char kLogMessageTextKey[] = "text"; | |
18 const char kLogMessageTimeKey[] = "time"; | |
19 const char kLogMessageFileKey[] = "file"; | |
20 const char kLogMessageLineKey[] = "line"; | |
21 const char kLogMessageSeverityKey[] = "severity"; | |
22 | |
23 // Converts |log_message| to a raw dictionary value used as a JSON argument to | |
24 // JavaScript functions. | |
25 scoped_ptr<base::DictionaryValue> LogMessageToDictionary( | |
26 const LogBuffer::LogMessage& log_message) { | |
27 base::DictionaryValue* dictionary = new base::DictionaryValue(); | |
Ilya Sherman
2015/05/13 21:57:10
nit: Please use a scoped_ptr here, and .Pass() it
Tim Song
2015/05/13 23:26:55
Done.
| |
28 dictionary->SetString(kLogMessageTextKey, log_message.text); | |
29 dictionary->SetString(kLogMessageTimeKey, | |
30 base::TimeFormatTimeOfDay(log_message.time)); | |
31 dictionary->SetString(kLogMessageFileKey, log_message.file); | |
32 dictionary->SetInteger(kLogMessageLineKey, log_message.line); | |
33 dictionary->SetInteger(kLogMessageSeverityKey, | |
34 static_cast<int>(log_message.severity)); | |
35 return make_scoped_ptr(dictionary); | |
36 } | |
37 | |
38 } // namespace | |
39 | |
9 ProximityAuthWebUIHandler::ProximityAuthWebUIHandler() { | 40 ProximityAuthWebUIHandler::ProximityAuthWebUIHandler() { |
41 LogBuffer::GetInstance()->AddObserver(this); | |
10 } | 42 } |
11 | 43 |
12 ProximityAuthWebUIHandler::~ProximityAuthWebUIHandler() { | 44 ProximityAuthWebUIHandler::~ProximityAuthWebUIHandler() { |
45 LogBuffer::GetInstance()->RemoveObserver(this); | |
13 } | 46 } |
14 | 47 |
15 void ProximityAuthWebUIHandler::RegisterMessages() { | 48 void ProximityAuthWebUIHandler::RegisterMessages() { |
49 web_ui()->RegisterMessageCallback( | |
50 "clearLogBuffer", base::Bind(&ProximityAuthWebUIHandler::ClearLogBuffer, | |
51 base::Unretained(this))); | |
52 | |
53 web_ui()->RegisterMessageCallback( | |
54 "getLogMessages", base::Bind(&ProximityAuthWebUIHandler::GetLogMessages, | |
55 base::Unretained(this))); | |
56 } | |
57 | |
58 void ProximityAuthWebUIHandler::OnLogMessageAdded( | |
59 const LogBuffer::LogMessage& log_message) { | |
60 scoped_ptr<base::DictionaryValue> dictionary( | |
61 LogMessageToDictionary(log_message).release()); | |
Ilya Sherman
2015/05/13 21:57:10
nit: You should be able to just assign the value,
Tim Song
2015/05/13 23:26:55
I tried both and got compile errors. It seems scop
Ilya Sherman
2015/05/13 23:42:34
Hmm, according to [ https://code.google.com/p/chro
Tim Song
2015/05/14 03:12:03
Sorry, my bad. I thought this comment was referrin
| |
62 web_ui()->CallJavascriptFunction("LogBufferInterface.onLogMessageAdded", | |
63 *dictionary); | |
64 } | |
65 | |
66 void ProximityAuthWebUIHandler::OnLogBufferCleared() { | |
67 web_ui()->CallJavascriptFunction("LogBufferInterface.onLogBufferCleared"); | |
68 } | |
69 | |
70 void ProximityAuthWebUIHandler::GetLogMessages(const base::ListValue* args) { | |
71 base::ListValue json_logs; | |
72 auto logs = LogBuffer::GetInstance()->logs(); | |
73 std::transform(logs->begin(), logs->end(), json_logs.begin(), | |
74 [](const LogBuffer::LogMessage& log) { | |
75 return LogMessageToDictionary(log).release(); | |
76 }); | |
77 | |
78 web_ui()->CallJavascriptFunction("LogBufferInterface.onGotLogMessages", | |
79 json_logs); | |
80 } | |
81 | |
82 void ProximityAuthWebUIHandler::ClearLogBuffer(const base::ListValue* args) { | |
83 // The OnLogBufferCleared() observer function will be called after the buffer | |
84 // is cleared. | |
85 LogBuffer::GetInstance()->Clear(); | |
16 } | 86 } |
17 | 87 |
18 } // namespace proximity_auth | 88 } // namespace proximity_auth |
OLD | NEW |