Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / webui / media / webrtc_logs_ui.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/webui/media/webrtc_logs_ui.h"
6
7 #include <vector>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/i18n/time_formatting.h"
12 #include "base/memory/ref_counted_memory.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/media/webrtc_log_list.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/upload_list.h"
21 #include "chrome/common/chrome_version_info.h"
22 #include "chrome/common/url_constants.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/browser/web_ui.h"
26 #include "content/public/browser/web_ui_data_source.h"
27 #include "content/public/browser/web_ui_message_handler.h"
28 #include "grit/browser_resources.h"
29
30 #if defined(OS_CHROMEOS)
31 #include "chrome/browser/chromeos/settings/cros_settings.h"
32 #endif
33
34 using content::WebContents;
35 using content::WebUIMessageHandler;
36
37 namespace {
38
39 content::WebUIDataSource* CreateWebRtcLogsUIHTMLSource() {
40   content::WebUIDataSource* source =
41       content::WebUIDataSource::Create(chrome::kChromeUIWebRtcLogsHost);
42
43   source->AddLocalizedString("webrtcLogsTitle", IDS_WEBRTC_LOGS_TITLE);
44   source->AddLocalizedString("webrtcLogCountFormat",
45                              IDS_WEBRTC_LOGS_LOG_COUNT_BANNER_FORMAT);
46   source->AddLocalizedString("webrtcLogHeaderFormat",
47                              IDS_WEBRTC_LOGS_LOG_HEADER_FORMAT);
48   source->AddLocalizedString("webrtcLogLocalFileLabelFormat",
49                              IDS_WEBRTC_LOGS_LOG_LOCAL_FILE_LABEL_FORMAT);
50   source->AddLocalizedString("webrtcLogLocalFileFormat",
51                              IDS_WEBRTC_LOGS_LOG_LOCAL_FILE_FORMAT);
52   source->AddLocalizedString("noLocalLogFileMessage",
53                              IDS_WEBRTC_LOGS_NO_LOCAL_LOG_FILE_MESSAGE);
54   source->AddLocalizedString("webrtcLogUploadTimeFormat",
55                              IDS_WEBRTC_LOGS_LOG_UPLOAD_TIME_FORMAT);
56   source->AddLocalizedString("webrtcLogReportIdFormat",
57                              IDS_WEBRTC_LOGS_LOG_REPORT_ID_FORMAT);
58   source->AddLocalizedString("bugLinkText", IDS_WEBRTC_LOGS_BUG_LINK_LABEL);
59   source->AddLocalizedString("webrtcLogNotUploadedMessage",
60                              IDS_WEBRTC_LOGS_LOG_NOT_UPLOADED_MESSAGE);
61   source->AddLocalizedString("noLogsMessage",
62                              IDS_WEBRTC_LOGS_NO_LOGS_MESSAGE);
63   source->SetJsonPath("strings.js");
64   source->AddResourcePath("webrtc_logs.js", IDR_WEBRTC_LOGS_JS);
65   source->SetDefaultResource(IDR_WEBRTC_LOGS_HTML);
66   return source;
67 }
68
69 ////////////////////////////////////////////////////////////////////////////////
70 //
71 // WebRtcLogsDOMHandler
72 //
73 ////////////////////////////////////////////////////////////////////////////////
74
75 // The handler for Javascript messages for the chrome://webrtc-logs/ page.
76 class WebRtcLogsDOMHandler : public WebUIMessageHandler,
77                              public UploadList::Delegate {
78  public:
79   explicit WebRtcLogsDOMHandler(Profile* profile);
80   ~WebRtcLogsDOMHandler() override;
81
82   // WebUIMessageHandler implementation.
83   void RegisterMessages() override;
84
85   // UploadList::Delegate implemenation.
86   void OnUploadListAvailable() override;
87
88  private:
89   // Asynchronously fetches the list of upload WebRTC logs. Called from JS.
90   void HandleRequestWebRtcLogs(const base::ListValue* args);
91
92   // Sends the recently uploaded logs list JS.
93   void UpdateUI();
94
95   // Loads, parses and stores the list of uploaded WebRTC logs.
96   scoped_refptr<UploadList> upload_list_;
97
98   // The directory where the logs are stored.
99   base::FilePath log_dir_;
100
101   // Set when |upload_list_| has finished populating the list of logs.
102   bool list_available_;
103
104   // Set when the webpage wants to update the list (on the webpage) but
105   // |upload_list_| hasn't finished populating the list of logs yet.
106   bool js_request_pending_;
107
108   DISALLOW_COPY_AND_ASSIGN(WebRtcLogsDOMHandler);
109 };
110
111 WebRtcLogsDOMHandler::WebRtcLogsDOMHandler(Profile* profile)
112     : log_dir_(
113           WebRtcLogList::GetWebRtcLogDirectoryForProfile(profile->GetPath())),
114       list_available_(false),
115       js_request_pending_(false) {
116   upload_list_ = WebRtcLogList::CreateWebRtcLogList(this, profile);
117 }
118
119 WebRtcLogsDOMHandler::~WebRtcLogsDOMHandler() {
120   upload_list_->ClearDelegate();
121 }
122
123 void WebRtcLogsDOMHandler::RegisterMessages() {
124   upload_list_->LoadUploadListAsynchronously();
125
126   web_ui()->RegisterMessageCallback("requestWebRtcLogsList",
127       base::Bind(&WebRtcLogsDOMHandler::HandleRequestWebRtcLogs,
128                  base::Unretained(this)));
129 }
130
131 void WebRtcLogsDOMHandler::HandleRequestWebRtcLogs(
132     const base::ListValue* args) {
133   if (list_available_)
134     UpdateUI();
135   else
136     js_request_pending_ = true;
137 }
138
139 void WebRtcLogsDOMHandler::OnUploadListAvailable() {
140   list_available_ = true;
141   if (js_request_pending_)
142     UpdateUI();
143 }
144
145 void WebRtcLogsDOMHandler::UpdateUI() {
146   std::vector<UploadList::UploadInfo> uploads;
147   upload_list_->GetUploads(50, &uploads);
148
149   base::ListValue upload_list;
150   for (std::vector<UploadList::UploadInfo>::iterator i = uploads.begin();
151        i != uploads.end();
152        ++i) {
153     base::DictionaryValue* upload = new base::DictionaryValue();
154     upload->SetString("id", i->id);
155
156     base::string16 value_w;
157     if (!i->time.is_null())
158       value_w = base::TimeFormatFriendlyDateAndTime(i->time);
159     upload->SetString("upload_time", value_w);
160
161     value_w.clear();
162     double seconds_since_epoch;
163     if (base::StringToDouble(i->local_id, &seconds_since_epoch)) {
164       base::Time capture_time = base::Time::FromDoubleT(seconds_since_epoch);
165       value_w = base::TimeFormatFriendlyDateAndTime(capture_time);
166     }
167     upload->SetString("capture_time", value_w);
168
169     base::FilePath::StringType value;
170     if (!i->local_id.empty())
171       value = log_dir_.AppendASCII(i->local_id)
172           .AddExtension(FILE_PATH_LITERAL(".gz")).value();
173     upload->SetString("local_file", value);
174
175     upload_list.Append(upload);
176   }
177
178   const chrome::VersionInfo version_info;
179   base::StringValue version(version_info.Version());
180
181   web_ui()->CallJavascriptFunction("updateWebRtcLogsList", upload_list,
182                                    version);
183 }
184
185 }  // namespace
186
187 ///////////////////////////////////////////////////////////////////////////////
188 //
189 // WebRtcLogsUI
190 //
191 ///////////////////////////////////////////////////////////////////////////////
192
193 WebRtcLogsUI::WebRtcLogsUI(content::WebUI* web_ui) : WebUIController(web_ui) {
194   Profile* profile = Profile::FromWebUI(web_ui);
195   web_ui->AddMessageHandler(new WebRtcLogsDOMHandler(profile));
196
197   // Set up the chrome://webrtc-logs/ source.
198   content::WebUIDataSource::Add(profile, CreateWebRtcLogsUIHTMLSource());
199 }