Convert browser_tests to Swarming.
[chromium-blink-merge.git] / chrome / browser / feedback / system_logs / log_sources / chrome_internal_log_source.cc
blobe843cd10fae95b1d0ffe66b042bd7b828db9c18c
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.
5 #include "chrome/browser/feedback/system_logs/log_sources/chrome_internal_log_source.h"
7 #include "base/json/json_string_value_serializer.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/sys_info.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/browser/sync/about_sync_util.h"
14 #include "chrome/browser/sync/profile_sync_service_factory.h"
15 #include "chrome/common/chrome_version_info.h"
16 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_pref_names.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "extensions/browser/extension_registry.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/common/extension_set.h"
22 #if defined(OS_WIN)
23 #include "base/win/win_util.h"
24 #endif
26 namespace {
28 const char kSyncDataKey[] = "about_sync_data";
29 const char kExtensionsListKey[] = "extensions";
30 const char kDataReductionProxyKey[] = "data_reduction_proxy";
31 const char kChromeVersionTag[] = "CHROME VERSION";
32 #if !defined(OS_CHROMEOS)
33 const char kOsVersionTag[] = "OS VERSION";
34 #endif
35 #if defined(OS_WIN)
36 const char kUsbKeyboardDetected[] = "usb_keyboard_detected";
37 #endif
39 } // namespace
41 namespace system_logs {
43 ChromeInternalLogSource::ChromeInternalLogSource()
44 : SystemLogsSource("ChromeInternal") {
47 ChromeInternalLogSource::~ChromeInternalLogSource() {
50 void ChromeInternalLogSource::Fetch(const SysLogsSourceCallback& callback) {
51 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
52 DCHECK(!callback.is_null());
54 SystemLogsResponse response;
56 chrome::VersionInfo version_info;
57 response[kChromeVersionTag] = version_info.CreateVersionString();
59 #if !defined(OS_CHROMEOS)
60 // On ChromeOS, this will be pulled in from the LSB_RELEASE.
61 std::string os_version = base::SysInfo::OperatingSystemName() + ": " +
62 base::SysInfo::OperatingSystemVersion();
63 response[kOsVersionTag] = os_version;
64 #endif
66 PopulateSyncLogs(&response);
67 PopulateExtensionInfoLogs(&response);
68 PopulateDataReductionProxyLogs(&response);
69 #if defined(OS_WIN)
70 PopulateUsbKeyboardDetected(&response);
71 #endif
73 if (ProfileManager::GetLastUsedProfile()->IsChild())
74 response["account_type"] = "child";
76 callback.Run(&response);
79 void ChromeInternalLogSource::PopulateSyncLogs(SystemLogsResponse* response) {
80 // We are only interested in sync logs for the primary user profile.
81 Profile* profile = ProfileManager::GetPrimaryUserProfile();
82 if (!profile ||
83 !ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService(profile))
84 return;
86 ProfileSyncService* service =
87 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile);
88 scoped_ptr<base::DictionaryValue> sync_logs(
89 sync_ui_util::ConstructAboutInformation(service));
91 // Remove identity section.
92 base::ListValue* details = NULL;
93 sync_logs->GetList(kDetailsKey, &details);
94 if (!details)
95 return;
96 for (base::ListValue::iterator it = details->begin();
97 it != details->end(); ++it) {
98 base::DictionaryValue* dict = NULL;
99 if ((*it)->GetAsDictionary(&dict)) {
100 std::string title;
101 dict->GetString("title", &title);
102 if (title == kIdentityTitle) {
103 details->Erase(it, NULL);
104 break;
109 // Add sync logs to logs.
110 std::string sync_logs_string;
111 JSONStringValueSerializer serializer(&sync_logs_string);
112 serializer.Serialize(*sync_logs.get());
114 (*response)[kSyncDataKey] = sync_logs_string;
117 void ChromeInternalLogSource::PopulateExtensionInfoLogs(
118 SystemLogsResponse* response) {
119 if (!ChromeMetricsServiceAccessor::IsCrashReportingEnabled())
120 return;
122 Profile* primary_profile =
123 g_browser_process->profile_manager()->GetPrimaryUserProfile();
124 if (!primary_profile)
125 return;
127 extensions::ExtensionRegistry* extension_registry =
128 extensions::ExtensionRegistry::Get(primary_profile);
129 std::string extensions_list;
130 for (const scoped_refptr<const extensions::Extension>& extension :
131 extension_registry->enabled_extensions()) {
132 if (extensions_list.empty()) {
133 extensions_list = extension->name();
134 } else {
135 extensions_list += ",\n" + extension->name();
138 if (!extensions_list.empty())
139 extensions_list += "\n";
141 if (!extensions_list.empty())
142 (*response)[kExtensionsListKey] = extensions_list;
145 void ChromeInternalLogSource::PopulateDataReductionProxyLogs(
146 SystemLogsResponse* response) {
147 PrefService* prefs = ProfileManager::GetActiveUserProfile()->GetPrefs();
148 bool is_data_reduction_proxy_enabled = prefs->HasPrefPath(
149 data_reduction_proxy::prefs::kDataReductionProxyEnabled) &&
150 prefs->GetBoolean(
151 data_reduction_proxy::prefs::kDataReductionProxyEnabled);
152 (*response)[kDataReductionProxyKey] = is_data_reduction_proxy_enabled ?
153 "enabled" : "disabled";
156 #if defined(OS_WIN)
157 void ChromeInternalLogSource::PopulateUsbKeyboardDetected(
158 SystemLogsResponse* response) {
159 std::string reason;
160 bool result = base::win::IsKeyboardPresentOnSlate(&reason);
161 (*response)[kUsbKeyboardDetected] = result ? "Keyboard Detected:\n" :
162 "No Keyboard:\n";
163 (*response)[kUsbKeyboardDetected] += reason;
165 #endif
167 } // namespace system_logs