Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/feedback/feedback_data.h" | 5 #include "chrome/browser/feedback/feedback_data.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | |
| 7 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
| 8 #include "base/values.h" | 9 #include "base/values.h" |
| 9 #include "chrome/browser/browser_process.h" | 10 #include "chrome/browser/browser_process.h" |
| 10 #include "chrome/browser/chromeos/settings/cros_settings.h" | 11 #include "chrome/browser/chromeos/settings/cros_settings.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | |
| 12 #include "chrome/browser/extensions/extension_system.h" | |
| 13 #include "chrome/browser/feedback/feedback_util.h" | 12 #include "chrome/browser/feedback/feedback_util.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" | 13 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "chrome/browser/sync/about_sync_util.h" | |
| 16 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 17 #include "chrome/common/extensions/extension.h" | |
| 18 #include "chrome/common/extensions/extension_set.h" | |
| 19 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 20 | 15 |
| 16 #if defined(USE_ASH) | |
| 17 #include "ash/shell.h" | |
| 18 #include "ash/shell_delegate.h" | |
| 19 #endif | |
| 20 | |
| 21 using content::BrowserThread; | 21 using content::BrowserThread; |
| 22 | 22 |
| 23 FeedbackData::FeedbackData() : profile_(NULL), | |
| 24 feedback_page_data_complete_(false) { | |
|
xiyuan
2013/03/19 21:15:09
Should we initialize attached_filedata_(NULL) here
rkc
2013/03/19 21:20:47
Done.
| |
| 23 #if defined(OS_CHROMEOS) | 25 #if defined(OS_CHROMEOS) |
| 24 // TODO(rkc): Remove all the code that gather sync data and move it to a | 26 sys_info_.reset(NULL); |
| 25 // log data source once crbug.com/138582 is fixed. | 27 send_sys_info_ = true; |
| 26 namespace { | 28 syslogs_collection_complete_ = false; |
| 27 | 29 read_attached_file_complete_ = false; |
|
xiyuan
2013/03/19 21:15:09
nit: 2-space indent
rkc
2013/03/19 21:20:47
Done.
| |
| 28 const char kExtensionsListKey[] = "extensions"; | |
| 29 | |
| 30 void AddSyncLogs(chromeos::system::LogDictionaryType* logs) { | |
| 31 Profile* profile = ProfileManager::GetDefaultProfile(); | |
| 32 if (!ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( | |
| 33 profile)) | |
| 34 return; | |
| 35 | |
| 36 ProfileSyncService* service = | |
| 37 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile); | |
| 38 scoped_ptr<DictionaryValue> sync_logs( | |
| 39 sync_ui_util::ConstructAboutInformation(service)); | |
| 40 | |
| 41 // Remove credentials section. | |
| 42 ListValue* details = NULL; | |
| 43 sync_logs->GetList(kDetailsKey, &details); | |
| 44 if (!details) | |
| 45 return; | |
| 46 for (ListValue::iterator it = details->begin(); | |
| 47 it != details->end(); ++it) { | |
| 48 DictionaryValue* dict = NULL; | |
| 49 if ((*it)->GetAsDictionary(&dict)) { | |
| 50 std::string title; | |
| 51 dict->GetString("title", &title); | |
| 52 if (title == kCredentialsTitle) { | |
| 53 details->Erase(it, NULL); | |
| 54 break; | |
| 55 } | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 // Add sync logs to logs. | |
| 60 std::string sync_logs_string; | |
| 61 JSONStringValueSerializer serializer(&sync_logs_string); | |
| 62 serializer.Serialize(*sync_logs.get()); | |
| 63 (*logs)[kSyncDataKey] = sync_logs_string; | |
| 64 } | |
| 65 | |
| 66 void AddExtensionInfoLogs(chromeos::system::LogDictionaryType* logs) { | |
| 67 bool reporting_enabled = false; | |
| 68 chromeos::CrosSettings::Get()->GetBoolean(chromeos::kStatsReportingPref, | |
| 69 &reporting_enabled); | |
| 70 if (!reporting_enabled) | |
| 71 return; | |
| 72 | |
| 73 Profile* default_profile = | |
| 74 g_browser_process->profile_manager()->GetDefaultProfile(); | |
| 75 if (!default_profile) | |
| 76 return; | |
| 77 | |
| 78 ExtensionService* service = | |
| 79 extensions::ExtensionSystem::Get(default_profile)->extension_service(); | |
| 80 if (!service) | |
| 81 return; | |
| 82 | |
| 83 std::string extensions_list; | |
| 84 const ExtensionSet* extensions = service->extensions(); | |
| 85 for (ExtensionSet::const_iterator it = extensions->begin(); | |
| 86 it != extensions->end(); | |
| 87 ++it) { | |
| 88 const extensions::Extension* extension = *it; | |
| 89 if (extensions_list.empty()) { | |
| 90 extensions_list = extension->name(); | |
| 91 } else { | |
| 92 extensions_list += ", " + extension->name(); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 if (!extensions_list.empty()) | |
| 97 (*logs)[kExtensionsListKey] = extensions_list; | |
| 98 } | |
| 99 | |
| 100 } | |
| 101 #endif // OS_CHROMEOS | |
| 102 | |
| 103 FeedbackData::FeedbackData() | |
| 104 : profile_(NULL) | |
| 105 #if defined(OS_CHROMEOS) | |
| 106 , sys_info_(NULL) | |
| 107 , zip_content_(NULL) | |
| 108 , sent_report_(false) | |
| 109 , send_sys_info_(false) | |
| 110 #endif | |
| 111 { | |
| 112 } | |
| 113 | |
| 114 FeedbackData::FeedbackData(Profile* profile, | |
| 115 const std::string& category_tag, | |
| 116 const std::string& page_url, | |
| 117 const std::string& description, | |
| 118 const std::string& user_email, | |
| 119 ScreenshotDataPtr image | |
| 120 #if defined(OS_CHROMEOS) | |
| 121 , chromeos::system::LogDictionaryType* sys_info | |
| 122 , std::string* zip_content | |
| 123 , const std::string& timestamp | |
| 124 , const std::string& attached_filename | |
| 125 , std::string* attached_filedata | |
| 126 #endif | |
| 127 ) { | |
| 128 UpdateData(profile, | |
| 129 category_tag, | |
| 130 page_url, | |
| 131 description, | |
| 132 user_email, | |
| 133 image | |
| 134 #if defined(OS_CHROMEOS) | |
| 135 , sys_info | |
| 136 , true | |
| 137 , timestamp | |
| 138 , attached_filename | |
| 139 , attached_filedata | |
| 140 #endif | |
| 141 ); | |
| 142 #if defined(OS_CHROMEOS) | |
| 143 sys_info_ = sys_info; | |
| 144 zip_content_ = zip_content; | |
| 145 #endif | 30 #endif |
| 146 } | 31 } |
| 147 | 32 |
| 148 | |
| 149 FeedbackData::~FeedbackData() { | 33 FeedbackData::~FeedbackData() { |
| 150 } | 34 } |
| 151 | 35 |
| 152 void FeedbackData::UpdateData(Profile* profile, | 36 bool FeedbackData::DataCollectionComplete() { |
| 153 const std::string& category_tag, | |
| 154 const std::string& page_url, | |
| 155 const std::string& description, | |
| 156 const std::string& user_email, | |
| 157 ScreenshotDataPtr image | |
| 158 #if defined(OS_CHROMEOS) | 37 #if defined(OS_CHROMEOS) |
| 159 , const bool send_sys_info | 38 return (syslogs_collection_complete_ || !send_sys_info_) && |
| 160 , const bool sent_report | 39 read_attached_file_complete_ && |
| 161 , const std::string& timestamp | 40 feedback_page_data_complete_; |
| 162 , const std::string& attached_filename | 41 #else |
| 163 , std::string* attached_filedata | 42 return feedback_page_data_complete_; |
| 164 #endif | |
| 165 ) { | |
| 166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 167 profile_ = profile; | |
| 168 category_tag_ = category_tag; | |
| 169 page_url_ = page_url; | |
| 170 description_ = description; | |
| 171 user_email_ = user_email; | |
| 172 image_ = image; | |
| 173 #if defined(OS_CHROMEOS) | |
| 174 send_sys_info_ = send_sys_info; | |
| 175 sent_report_ = sent_report; | |
| 176 timestamp_ = timestamp; | |
| 177 attached_filename_ = attached_filename; | |
| 178 attached_filedata_ = attached_filedata; | |
| 179 #endif | 43 #endif |
| 180 } | 44 } |
| 181 | |
| 182 void FeedbackData::SendReport() { | 45 void FeedbackData::SendReport() { |
| 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 47 if (DataCollectionComplete()) { | |
| 48 FeedbackUtil::SendReport(this); | |
| 49 } | |
|
xiyuan
2013/03/19 21:15:09
nit: no enclosing braces for one-line branch
rkc
2013/03/19 21:20:47
Done.
| |
| 50 } | |
| 51 | |
| 52 void FeedbackData::FeedbackPageDataComplete() { | |
| 184 #if defined(OS_CHROMEOS) | 53 #if defined(OS_CHROMEOS) |
| 185 if (sent_report_) | 54 if (attached_filename_.size() && |
| 186 return; // We already received the syslogs and sent the report. | 55 base::FilePath::IsSeparator(attached_filename_[0]) && |
| 56 !attached_filedata_) { | |
| 57 // Read the attached file and then send this report. | |
| 58 attached_filedata_ = new std::string; | |
|
xiyuan
2013/03/19 21:15:09
I assume this is going to released later.
rkc
2013/03/19 21:20:47
Yep, in FeedbackUtil::SendReport. Added a comment
| |
| 187 | 59 |
| 188 // Set send_report_ to ensure that we only send one report. | 60 base::FilePath root = |
| 189 sent_report_ = true; | 61 ash::Shell::GetInstance()->delegate()-> |
| 62 GetCurrentBrowserContext()->GetPath(); | |
| 63 base::FilePath filepath = root.Append(attached_filename_.substr(1)); | |
| 64 attached_filename_ = filepath.BaseName().value(); | |
| 65 | |
| 66 // Read the file into file_data, then call send report again with the | |
| 67 // stripped filename and file data (which will skip this code path). | |
| 68 content::BrowserThread::PostTaskAndReply( | |
| 69 content::BrowserThread::FILE, FROM_HERE, | |
| 70 base::Bind(&FeedbackData::ReadAttachedFile, this, filepath), | |
| 71 base::Bind(&FeedbackData::ReadFileComplete, this)); | |
| 72 } else { | |
| 73 read_attached_file_complete_ = true; | |
| 74 } | |
| 190 #endif | 75 #endif |
| 191 | 76 feedback_page_data_complete_ = true; |
| 192 FeedbackUtil::SendReport(*this); | 77 SendReport(); |
| 193 | |
| 194 // Either the report is sent, and and this object may delete itself, or the | |
| 195 // report is pending the attached file read, and another FeedbackData has | |
| 196 // been created to hold this data - hence delete this object. | |
| 197 delete this; | |
| 198 } | 78 } |
| 199 | 79 |
| 200 #if defined(OS_CHROMEOS) | 80 #if defined(OS_CHROMEOS) |
| 201 // SyslogsComplete may be called before UpdateData, in which case, we do not | 81 void FeedbackData::SyslogsComplete( |
| 202 // want to delete the logs that were gathered, and we do not want to send the | 82 scoped_ptr<chromeos::SystemLogsResponse> sys_info) { |
| 203 // report either. Instead simply populate |sys_info_| and |zip_content_|. | |
| 204 void FeedbackData::SyslogsComplete(chromeos::system::LogDictionaryType* logs, | |
| 205 std::string* zip_content) { | |
| 206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 207 if (sent_report_) { | |
| 208 // We already sent the report, just delete the data. | |
| 209 if (logs) | |
| 210 delete logs; | |
| 211 if (zip_content) | |
| 212 delete zip_content; | |
| 213 } else { | |
| 214 | 84 |
| 215 // TODO(rkc): Move to the correct place once crbug.com/138582 is done. | 85 if (send_sys_info_) { |
| 216 AddSyncLogs(logs); | 86 sys_info_ = sys_info.Pass(); |
| 217 AddExtensionInfoLogs(logs); | 87 syslogs_collection_complete_ = true; |
| 218 | 88 SendReport(); |
| 219 // Will get deleted when SendReport() is called. | |
| 220 zip_content_ = zip_content; | |
| 221 sys_info_ = logs; | |
| 222 | |
| 223 if (send_sys_info_) { | |
| 224 // We already prepared the report, send it now. | |
| 225 this->SendReport(); | |
| 226 } | |
| 227 } | 89 } |
| 228 } | 90 } |
| 91 | |
| 92 void FeedbackData::ReadFileComplete() { | |
| 93 read_attached_file_complete_ = true; | |
| 94 SendReport(); | |
| 95 } | |
| 96 | |
| 97 void FeedbackData::StartSyslogsCollection() { | |
| 98 chromeos::SystemLogsFetcher* fetcher = new chromeos::SystemLogsFetcher(); | |
| 99 fetcher->Fetch(base::Bind(&FeedbackData::SyslogsComplete, this)); | |
| 100 } | |
| 101 | |
| 102 // private | |
| 103 void FeedbackData::ReadAttachedFile(const base::FilePath& from) { | |
| 104 if (!file_util::ReadFileToString(from, attached_filedata_)) | |
|
xiyuan
2013/03/19 21:15:09
nit: add enclosing {} for the nested if.
rkc
2013/03/19 21:20:47
Done.
| |
| 105 if (attached_filedata_) | |
| 106 attached_filedata_->clear(); | |
| 107 } | |
| 229 #endif | 108 #endif |
| OLD | NEW |