OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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/bug_report_util.h" |
| 6 |
| 7 #include "app/l10n_util.h" |
| 8 #include "base/file_version_info.h" |
| 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/browser_process_impl.h" |
| 11 #include "chrome/browser/net/url_fetcher.h" |
| 12 #include "chrome/browser/profile.h" |
| 13 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
| 14 #include "chrome/browser/tab_contents/tab_contents.h" |
| 15 #include "googleurl/src/gurl.h" |
| 16 #include "grit/locale_settings.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 static const int kBugReportVersion = 1; |
| 21 |
| 22 static const char kReportPhishingUrl[] = |
| 23 "http://www.google.com/safebrowsing/report_phish/"; |
| 24 } |
| 25 |
| 26 // Simple URLFetcher::Delegate to clean up URLFetcher on completion. |
| 27 class BugReportUtil::PostCleanup : public URLFetcher::Delegate { |
| 28 public: |
| 29 PostCleanup(); |
| 30 |
| 31 // Overridden from URLFetcher::Delegate. |
| 32 virtual void OnURLFetchComplete(const URLFetcher* source, |
| 33 const GURL& url, |
| 34 const URLRequestStatus& status, |
| 35 int response_code, |
| 36 const ResponseCookies& cookies, |
| 37 const std::string& data); |
| 38 private: |
| 39 DISALLOW_COPY_AND_ASSIGN(PostCleanup); |
| 40 }; |
| 41 |
| 42 BugReportUtil::PostCleanup::PostCleanup() { |
| 43 } |
| 44 |
| 45 void BugReportUtil::PostCleanup::OnURLFetchComplete( |
| 46 const URLFetcher* source, |
| 47 const GURL& url, |
| 48 const URLRequestStatus& status, |
| 49 int response_code, |
| 50 const ResponseCookies& cookies, |
| 51 const std::string& data) { |
| 52 // Delete the URLFetcher. |
| 53 delete source; |
| 54 // And then delete ourselves. |
| 55 delete this; |
| 56 } |
| 57 |
| 58 // static |
| 59 void BugReportUtil::SetOSVersion(std::string *os_version) { |
| 60 #if defined(OS_WIN) |
| 61 OSVERSIONINFO osvi; |
| 62 ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); |
| 63 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); |
| 64 |
| 65 if (GetVersionEx(&osvi)) { |
| 66 *os_version = StringPrintf("%d.%d.%d %S", |
| 67 osvi.dwMajorVersion, |
| 68 osvi.dwMinorVersion, |
| 69 osvi.dwBuildNumber, |
| 70 osvi.szCSDVersion); |
| 71 } else { |
| 72 *os_version = "unknown"; |
| 73 } |
| 74 #else |
| 75 // Right now, we only get the OS Version for Windows. |
| 76 // TODO(mirandac): make this cross-platform. |
| 77 *os_version = "unknown"; |
| 78 #endif |
| 79 } |
| 80 |
| 81 // Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). |
| 82 void BugReportUtil::CreateMimeBoundary(std::string *out) { |
| 83 int r1 = rand(); |
| 84 int r2 = rand(); |
| 85 SStringPrintf(out, "---------------------------%08X%08X", r1, r2); |
| 86 } |
| 87 |
| 88 // static |
| 89 void BugReportUtil::SendReport(Profile* profile, |
| 90 std::string page_title_text, |
| 91 int problem_type, |
| 92 std::string page_url_text, |
| 93 std::string description, |
| 94 const char* png_data, |
| 95 int png_data_length) { |
| 96 GURL post_url(WideToUTF8(l10n_util::GetString(IDS_BUGREPORT_POST_URL))); |
| 97 std::string mime_boundary; |
| 98 CreateMimeBoundary(&mime_boundary); |
| 99 |
| 100 // Create a request body and add the mandatory parameters. |
| 101 std::string post_body; |
| 102 |
| 103 // Add the protocol version: |
| 104 post_body.append("--" + mime_boundary + "\r\n"); |
| 105 post_body.append("Content-Disposition: form-data; " |
| 106 "name=\"data_version\"\r\n\r\n"); |
| 107 post_body.append(StringPrintf("%d\r\n", kBugReportVersion)); |
| 108 |
| 109 // Add the page title. |
| 110 post_body.append("--" + mime_boundary + "\r\n"); |
| 111 post_body.append(page_title_text + "\r\n"); |
| 112 |
| 113 // Add the problem type. |
| 114 post_body.append("--" + mime_boundary + "\r\n"); |
| 115 post_body.append("Content-Disposition: form-data; " |
| 116 "name=\"problem\"\r\n\r\n"); |
| 117 post_body.append(StringPrintf("%d\r\n", problem_type)); |
| 118 |
| 119 // Add in the URL, if we have one. |
| 120 post_body.append("--" + mime_boundary + "\r\n"); |
| 121 post_body.append("Content-Disposition: form-data; " |
| 122 "name=\"url\"\r\n\r\n"); |
| 123 |
| 124 // Convert URL to UTF8. |
| 125 if (page_url_text.empty()) |
| 126 post_body.append("n/a\r\n"); |
| 127 else |
| 128 post_body.append(page_url_text + "\r\n"); |
| 129 |
| 130 // Add Chrome version. |
| 131 post_body.append("--" + mime_boundary + "\r\n"); |
| 132 post_body.append("Content-Disposition: form-data; " |
| 133 "name=\"chrome_version\"\r\n\r\n"); |
| 134 |
| 135 std::string chrome_version; |
| 136 scoped_ptr<FileVersionInfo> version_info( |
| 137 FileVersionInfo::CreateFileVersionInfoForCurrentModule()); |
| 138 if (version_info.get()) { |
| 139 chrome_version = WideToUTF8(version_info->product_name()) + " - " + |
| 140 WideToUTF8(version_info->file_version()) + |
| 141 " (" + WideToUTF8(version_info->last_change()) + ")"; |
| 142 } |
| 143 |
| 144 if (chrome_version.empty()) |
| 145 post_body.append("n/a\r\n"); |
| 146 else |
| 147 post_body.append(chrome_version + "\r\n"); |
| 148 |
| 149 // Add OS version (eg, for WinXP SP2: "5.1.2600 Service Pack 2"). |
| 150 std::string os_version = ""; |
| 151 post_body.append("--" + mime_boundary + "\r\n"); |
| 152 post_body.append("Content-Disposition: form-data; " |
| 153 "name=\"os_version\"\r\n\r\n"); |
| 154 SetOSVersion(&os_version); |
| 155 post_body.append(os_version + "\r\n"); |
| 156 |
| 157 // Add locale. |
| 158 #if defined(OS_MACOSX) |
| 159 std::string chrome_locale = g_browser_process->GetApplicationLocale(); |
| 160 #else |
| 161 icu::Locale locale = icu::Locale::getDefault(); |
| 162 const char *lang = locale.getLanguage(); |
| 163 std::string chrome_locale = (lang)? lang:"en"; |
| 164 #endif |
| 165 |
| 166 post_body.append("--" + mime_boundary + "\r\n"); |
| 167 post_body.append("Content-Disposition: form-data; " |
| 168 "name=\"chrome_locale\"\r\n\r\n"); |
| 169 post_body.append(chrome_locale + "\r\n"); |
| 170 |
| 171 // Add a description if we have one. |
| 172 post_body.append("--" + mime_boundary + "\r\n"); |
| 173 post_body.append("Content-Disposition: form-data; " |
| 174 "name=\"description\"\r\n\r\n"); |
| 175 |
| 176 if (description.empty()) |
| 177 post_body.append("n/a\r\n"); |
| 178 else |
| 179 post_body.append(description + "\r\n"); |
| 180 |
| 181 // Include the page image if we have one. |
| 182 if (png_data != NULL && png_data_length > 0) { |
| 183 post_body.append("--" + mime_boundary + "\r\n"); |
| 184 post_body.append("Content-Disposition: form-data; name=\"screenshot\"; " |
| 185 "filename=\"screenshot.png\"\r\n"); |
| 186 post_body.append("Content-Type: application/octet-stream\r\n"); |
| 187 post_body.append(StringPrintf("Content-Length: %lu\r\n\r\n", |
| 188 png_data_length)); |
| 189 post_body.append(png_data, png_data_length); |
| 190 post_body.append("\r\n"); |
| 191 } |
| 192 |
| 193 // TODO(awalker): include the page source if we can get it. |
| 194 // if (include_page_source_checkbox_->checked()) { |
| 195 // } |
| 196 |
| 197 // Terminate the body. |
| 198 post_body.append("--" + mime_boundary + "--\r\n"); |
| 199 |
| 200 // We have the body of our POST, so send it off to the server. |
| 201 URLFetcher* fetcher = new URLFetcher(post_url, URLFetcher::POST, |
| 202 new BugReportUtil::PostCleanup); |
| 203 fetcher->set_request_context(profile->GetRequestContext()); |
| 204 std::string mime_type("multipart/form-data; boundary="); |
| 205 mime_type += mime_boundary; |
| 206 fetcher->set_upload_data(mime_type, post_body); |
| 207 fetcher->Start(); |
| 208 } |
| 209 |
| 210 // static |
| 211 std::string BugReportUtil::GetMimeType() { |
| 212 std::string mime_type("multipart/form-data; boundary="); |
| 213 std::string mime_boundary; |
| 214 CreateMimeBoundary(&mime_boundary); |
| 215 mime_type += mime_boundary; |
| 216 return mime_type; |
| 217 } |
| 218 |
| 219 // static |
| 220 void BugReportUtil::ReportPhishing(TabContents* currentTab, |
| 221 const std::string& phishing_url) { |
| 222 currentTab->controller().LoadURL( |
| 223 safe_browsing_util::GeneratePhishingReportUrl( |
| 224 kReportPhishingUrl, phishing_url), |
| 225 GURL(), |
| 226 PageTransition::LINK); |
| 227 } |
| 228 |
OLD | NEW |