diff options
author | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-29 18:47:17 +0000 |
---|---|---|
committer | mirandac@chromium.org <mirandac@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-29 18:47:17 +0000 |
commit | 56cfe04013dda41c589bef72667b9f5edf3e65f9 (patch) | |
tree | 3d43568783c88ba870854cb01d430b076744ad49 /chrome | |
parent | 8345371c9a46402eafccbacc6d9053447b217643 (diff) | |
download | chromium_src-56cfe04013dda41c589bef72667b9f5edf3e65f9.zip chromium_src-56cfe04013dda41c589bef72667b9f5edf3e65f9.tar.gz chromium_src-56cfe04013dda41c589bef72667b9f5edf3e65f9.tar.bz2 |
Hoist bug reporting code out of bug_report_view so that it can be used cross-platform.
BUG= none
TEST= bug reporting should work the same on Windows.
Review URL: http://codereview.chromium.org/339051
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30486 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/bug_report_util.cc | 228 | ||||
-rw-r--r-- | chrome/browser/bug_report_util.h | 57 | ||||
-rw-r--r-- | chrome/browser/views/bug_report_view.cc | 203 | ||||
-rwxr-xr-x | chrome/chrome.gyp | 2 |
4 files changed, 298 insertions, 192 deletions
diff --git a/chrome/browser/bug_report_util.cc b/chrome/browser/bug_report_util.cc new file mode 100644 index 0000000..f18b2cd5 --- /dev/null +++ b/chrome/browser/bug_report_util.cc @@ -0,0 +1,228 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/bug_report_util.h" + +#include "app/l10n_util.h" +#include "base/file_version_info.h" +#include "base/string_util.h" +#include "chrome/browser/browser_process_impl.h" +#include "chrome/browser/net/url_fetcher.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/safe_browsing/safe_browsing_util.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "googleurl/src/gurl.h" +#include "grit/locale_settings.h" + +namespace { + +static const int kBugReportVersion = 1; + +static const char kReportPhishingUrl[] = + "http://www.google.com/safebrowsing/report_phish/"; +} + +// Simple URLFetcher::Delegate to clean up URLFetcher on completion. +class BugReportUtil::PostCleanup : public URLFetcher::Delegate { + public: + PostCleanup(); + + // Overridden from URLFetcher::Delegate. + virtual void OnURLFetchComplete(const URLFetcher* source, + const GURL& url, + const URLRequestStatus& status, + int response_code, + const ResponseCookies& cookies, + const std::string& data); + private: + DISALLOW_COPY_AND_ASSIGN(PostCleanup); +}; + +BugReportUtil::PostCleanup::PostCleanup() { +} + +void BugReportUtil::PostCleanup::OnURLFetchComplete( + const URLFetcher* source, + const GURL& url, + const URLRequestStatus& status, + int response_code, + const ResponseCookies& cookies, + const std::string& data) { + // Delete the URLFetcher. + delete source; + // And then delete ourselves. + delete this; +} + +// static +void BugReportUtil::SetOSVersion(std::string *os_version) { +#if defined(OS_WIN) + OSVERSIONINFO osvi; + ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); + osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); + + if (GetVersionEx(&osvi)) { + *os_version = StringPrintf("%d.%d.%d %S", + osvi.dwMajorVersion, + osvi.dwMinorVersion, + osvi.dwBuildNumber, + osvi.szCSDVersion); + } else { + *os_version = "unknown"; + } +#else + // Right now, we only get the OS Version for Windows. + // TODO(mirandac): make this cross-platform. + *os_version = "unknown"; +#endif +} + +// Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). +void BugReportUtil::CreateMimeBoundary(std::string *out) { + int r1 = rand(); + int r2 = rand(); + SStringPrintf(out, "---------------------------%08X%08X", r1, r2); +} + +// static +void BugReportUtil::SendReport(Profile* profile, + std::string page_title_text, + int problem_type, + std::string page_url_text, + std::string description, + const char* png_data, + int png_data_length) { + GURL post_url(WideToUTF8(l10n_util::GetString(IDS_BUGREPORT_POST_URL))); + std::string mime_boundary; + CreateMimeBoundary(&mime_boundary); + + // Create a request body and add the mandatory parameters. + std::string post_body; + + // Add the protocol version: + post_body.append("--" + mime_boundary + "\r\n"); + post_body.append("Content-Disposition: form-data; " + "name=\"data_version\"\r\n\r\n"); + post_body.append(StringPrintf("%d\r\n", kBugReportVersion)); + + // Add the page title. + post_body.append("--" + mime_boundary + "\r\n"); + post_body.append(page_title_text + "\r\n"); + + // Add the problem type. + post_body.append("--" + mime_boundary + "\r\n"); + post_body.append("Content-Disposition: form-data; " + "name=\"problem\"\r\n\r\n"); + post_body.append(StringPrintf("%d\r\n", problem_type)); + + // Add in the URL, if we have one. + post_body.append("--" + mime_boundary + "\r\n"); + post_body.append("Content-Disposition: form-data; " + "name=\"url\"\r\n\r\n"); + + // Convert URL to UTF8. + if (page_url_text.empty()) + post_body.append("n/a\r\n"); + else + post_body.append(page_url_text + "\r\n"); + + // Add Chrome version. + post_body.append("--" + mime_boundary + "\r\n"); + post_body.append("Content-Disposition: form-data; " + "name=\"chrome_version\"\r\n\r\n"); + + std::string chrome_version; + scoped_ptr<FileVersionInfo> version_info( + FileVersionInfo::CreateFileVersionInfoForCurrentModule()); + if (version_info.get()) { + chrome_version = WideToUTF8(version_info->product_name()) + " - " + + WideToUTF8(version_info->file_version()) + + " (" + WideToUTF8(version_info->last_change()) + ")"; + } + + if (chrome_version.empty()) + post_body.append("n/a\r\n"); + else + post_body.append(chrome_version + "\r\n"); + + // Add OS version (eg, for WinXP SP2: "5.1.2600 Service Pack 2"). + std::string os_version = ""; + post_body.append("--" + mime_boundary + "\r\n"); + post_body.append("Content-Disposition: form-data; " + "name=\"os_version\"\r\n\r\n"); + SetOSVersion(&os_version); + post_body.append(os_version + "\r\n"); + + // Add locale. +#if defined(OS_MACOSX) + std::string chrome_locale = g_browser_process->GetApplicationLocale(); +#else + icu::Locale locale = icu::Locale::getDefault(); + const char *lang = locale.getLanguage(); + std::string chrome_locale = (lang)? lang:"en"; +#endif + + post_body.append("--" + mime_boundary + "\r\n"); + post_body.append("Content-Disposition: form-data; " + "name=\"chrome_locale\"\r\n\r\n"); + post_body.append(chrome_locale + "\r\n"); + + // Add a description if we have one. + post_body.append("--" + mime_boundary + "\r\n"); + post_body.append("Content-Disposition: form-data; " + "name=\"description\"\r\n\r\n"); + + if (description.empty()) + post_body.append("n/a\r\n"); + else + post_body.append(description + "\r\n"); + + // Include the page image if we have one. + if (png_data != NULL && png_data_length > 0) { + post_body.append("--" + mime_boundary + "\r\n"); + post_body.append("Content-Disposition: form-data; name=\"screenshot\"; " + "filename=\"screenshot.png\"\r\n"); + post_body.append("Content-Type: application/octet-stream\r\n"); + post_body.append(StringPrintf("Content-Length: %lu\r\n\r\n", + png_data_length)); + post_body.append(png_data, png_data_length); + post_body.append("\r\n"); + } + + // TODO(awalker): include the page source if we can get it. + // if (include_page_source_checkbox_->checked()) { + // } + + // Terminate the body. + post_body.append("--" + mime_boundary + "--\r\n"); + + // We have the body of our POST, so send it off to the server. + URLFetcher* fetcher = new URLFetcher(post_url, URLFetcher::POST, + new BugReportUtil::PostCleanup); + fetcher->set_request_context(profile->GetRequestContext()); + std::string mime_type("multipart/form-data; boundary="); + mime_type += mime_boundary; + fetcher->set_upload_data(mime_type, post_body); + fetcher->Start(); +} + +// static +std::string BugReportUtil::GetMimeType() { + std::string mime_type("multipart/form-data; boundary="); + std::string mime_boundary; + CreateMimeBoundary(&mime_boundary); + mime_type += mime_boundary; + return mime_type; +} + +// static +void BugReportUtil::ReportPhishing(TabContents* currentTab, + const std::string& phishing_url) { + currentTab->controller().LoadURL( + safe_browsing_util::GeneratePhishingReportUrl( + kReportPhishingUrl, phishing_url), + GURL(), + PageTransition::LINK); +} + diff --git a/chrome/browser/bug_report_util.h b/chrome/browser/bug_report_util.h new file mode 100644 index 0000000..53feecf --- /dev/null +++ b/chrome/browser/bug_report_util.h @@ -0,0 +1,57 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_BUG_REPORT_UTIL_H_ +#define CHROME_BROWSER_BUG_REPORT_UTIL_H_ + +#include <string> +#include <vector> + +#include "base/basictypes.h" +#if defined(OS_MACOSX) +#include "base/mac_util.h" +#endif +#include "base/scoped_ptr.h" + +class Profile; +class TabContents; + +class BugReportUtil { + public: + // SetOSVersion copies the maj.minor.build + servicePack_string + // into a string (for Windows only). We currently have: + // win_util::GetWinVersion returns WinVersion, which is just + // an enum of 2000, XP, 2003, or VISTA. Not enough detail for + // bug reports. + // base::SysInfo::OperatingSystemVersion returns an std::string + // but doesn't include the build or service pack. That function + // is probably the right one to extend, but will require changing + // all the call sites or making it a wrapper around another util. + static void SetOSVersion(std::string *os_version); + + // Generates bug report data. + static void SendReport(Profile* profile, + std::string page_title_text, + int problem_type, + std::string page_url_text, + std::string description, + const char* png_data, + int png_data_length); + + // Redirects the user to Google's phishing reporting page. + static void ReportPhishing(TabContents* currentTab, + const std::string& phishing_url); + + static std::string GetMimeType(); + + class PostCleanup; + + private: + static void CreateMimeBoundary(std::string *out); + + DISALLOW_IMPLICIT_CONSTRUCTORS(BugReportUtil); +}; + +#endif // CHROME_BROWSER_BUG_REPORT_UTIL_H_ + diff --git a/chrome/browser/views/bug_report_view.cc b/chrome/browser/views/bug_report_view.cc index c048bfc..162e4c5 100644 --- a/chrome/browser/views/bug_report_view.cc +++ b/chrome/browser/views/bug_report_view.cc @@ -9,6 +9,7 @@ #include "app/win_util.h" #include "base/file_version_info.h" #include "base/string_util.h" +#include "chrome/browser/bug_report_util.h" #include "chrome/browser/net/url_fetcher.h" #include "chrome/browser/profile.h" #include "chrome/browser/safe_browsing/safe_browsing_util.h" @@ -39,10 +40,6 @@ static const int kBugReportVersion = 1; // Number of lines description field can display at one time. static const int kDescriptionLines = 5; -// Google's phishing reporting URL. -static const char kReportPhishingUrl[] = - "http://www.google.com/safebrowsing/report_phish/"; - class BugReportComboBoxModel : public ComboboxModel { public: BugReportComboBoxModel() {} @@ -95,22 +92,6 @@ class BugReportComboBoxModel : public ComboboxModel { DISALLOW_COPY_AND_ASSIGN(BugReportComboBoxModel); }; -// Simple URLFetcher::Delegate to clean up URLFetcher on completion -// (since the BugReportView will be gone by then). -class BugReportView::PostCleanup : public URLFetcher::Delegate { - public: - PostCleanup(); - // Overridden from URLFetcher::Delegate. - virtual void OnURLFetchComplete(const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data); - private: - DISALLOW_COPY_AND_ASSIGN(PostCleanup); -}; - namespace browser { // Global "display this dialog" function declared in browser_dialogs.h. @@ -134,22 +115,6 @@ void ShowBugReportView(views::Widget* parent, } // namespace browser -BugReportView::PostCleanup::PostCleanup() { -} - -void BugReportView::PostCleanup::OnURLFetchComplete( - const URLFetcher* source, - const GURL& url, - const URLRequestStatus& status, - int response_code, - const ResponseCookies& cookies, - const std::string& data) { - // Delete the URLFetcher. - delete source; - // And then delete ourselves. - delete this; -} - // BugReportView - create and submit a bug report from the user. // This is separate from crash reporting, which is handled by Breakpad. // @@ -348,9 +313,17 @@ std::wstring BugReportView::GetWindowTitle() const { bool BugReportView::Accept() { if (IsDialogButtonEnabled(MessageBoxFlags::DIALOGBUTTON_OK)) { if (problem_type_ == BugReportComboBoxModel::PHISHING_PAGE) - ReportPhishing(); + BugReportUtil::ReportPhishing(tab_, + WideToUTF8(page_url_text_->text())); else - SendReport(); + BugReportUtil::SendReport(profile_, + WideToUTF8(page_title_text_->GetText()), + problem_type_, + WideToUTF8(page_url_text_->text()), + WideToUTF8(description_text_->text()), + include_page_image_checkbox_->checked() && png_data_.get() ? + reinterpret_cast<const char *>(&((*png_data_.get())[0])) : NULL, + png_data_->size()); } return true; } @@ -358,157 +331,3 @@ bool BugReportView::Accept() { views::View* BugReportView::GetContentsView() { return this; } - -// SetOSVersion copies the maj.minor.build + servicePack_string -// into a string (for Windows only). This should probably be -// in a util somewhere. We currently have: -// win_util::GetWinVersion returns WinVersion, which is just -// an enum of 2000, XP, 2003, or VISTA. Not enough detail for -// bug reports. -// base::SysInfo::OperatingSystemVersion returns an std::string -// but doesn't include the build or service pack. That function -// is probably the right one to extend, but will require changing -// all the call sites or making it a wrapper around another util. -void BugReportView::SetOSVersion(std::string *os_version) { - OSVERSIONINFO osvi; - ZeroMemory(&osvi, sizeof(OSVERSIONINFO)); - osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); - - if (GetVersionEx(&osvi)) { - *os_version = StringPrintf("%d.%d.%d %S", - osvi.dwMajorVersion, - osvi.dwMinorVersion, - osvi.dwBuildNumber, - osvi.szCSDVersion); - } else { - *os_version = "unknown"; - } -} - -// Create a MIME boundary marker (27 '-' characters followed by 16 hex digits). -void BugReportView::CreateMimeBoundary(std::string *out) { - int r1 = rand(); - int r2 = rand(); - SStringPrintf(out, "---------------------------%08X%08X", r1, r2); -} - -void BugReportView::SendReport() { - std::wstring post_url = l10n_util::GetString(IDS_BUGREPORT_POST_URL); - std::string mime_boundary; - CreateMimeBoundary(&mime_boundary); - - // Create a request body and add the mandatory parameters. - std::string post_body; - - // Add the protocol version: - post_body.append("--" + mime_boundary + "\r\n"); - post_body.append("Content-Disposition: form-data; " - "name=\"data_version\"\r\n\r\n"); - post_body.append(StringPrintf("%d\r\n", kBugReportVersion)); - - // Add the page title. - post_body.append("--" + mime_boundary + "\r\n"); - std::string page_title = WideToUTF8(page_title_text_->GetText()); - post_body.append("Content-Disposition: form-data; " - "name=\"title\"\r\n\r\n"); - post_body.append(page_title + "\r\n"); - - // Add the problem type. - post_body.append("--" + mime_boundary + "\r\n"); - post_body.append("Content-Disposition: form-data; " - "name=\"problem\"\r\n\r\n"); - post_body.append(StringPrintf("%d\r\n", problem_type_)); - - // Add in the URL, if we have one. - post_body.append("--" + mime_boundary + "\r\n"); - post_body.append("Content-Disposition: form-data; " - "name=\"url\"\r\n\r\n"); - - // Convert URL to UTF8. - std::string report_url = WideToUTF8(page_url_text_->text()); - if (report_url.empty()) { - post_body.append("n/a\r\n"); - } else { - post_body.append(report_url + "\r\n"); - } - - // Add Chrome version. - post_body.append("--" + mime_boundary + "\r\n"); - post_body.append("Content-Disposition: form-data; " - "name=\"chrome_version\"\r\n\r\n"); - - std::string version = WideToUTF8(version_); - if (version.empty()) { - post_body.append("n/a\r\n"); - } else { - post_body.append(version + "\r\n"); - } - - // Add OS version (eg, for WinXP SP2: "5.1.2600 Service Pack 2"). - std::string os_version = ""; - post_body.append("--" + mime_boundary + "\r\n"); - post_body.append("Content-Disposition: form-data; " - "name=\"os_version\"\r\n\r\n"); - SetOSVersion(&os_version); - post_body.append(os_version + "\r\n"); - - // Add locale. - icu::Locale locale = icu::Locale::getDefault(); - const char *lang = locale.getLanguage(); - std::string chrome_locale = (lang)? lang:"en"; - post_body.append("--" + mime_boundary + "\r\n"); - post_body.append("Content-Disposition: form-data; " - "name=\"chrome_locale\"\r\n\r\n"); - post_body.append(chrome_locale + "\r\n"); - - // Add a description if we have one. - post_body.append("--" + mime_boundary + "\r\n"); - post_body.append("Content-Disposition: form-data; " - "name=\"description\"\r\n\r\n"); - - std::string description = WideToUTF8(description_text_->text()); - if (description.empty()) { - post_body.append("n/a\r\n"); - } else { - post_body.append(description + "\r\n"); - } - - // Include the page image if we have one. - if (include_page_image_checkbox_->checked() && png_data_.get()) { - post_body.append("--" + mime_boundary + "\r\n"); - post_body.append("Content-Disposition: form-data; name=\"screenshot\"; " - "filename=\"screenshot.png\"\r\n"); - post_body.append("Content-Type: application/octet-stream\r\n"); - post_body.append(StringPrintf("Content-Length: %lu\r\n\r\n", - png_data_->size())); - // The following relies on the fact that STL vectors are guaranteed to - // be stored contiguously. - post_body.append(reinterpret_cast<const char *>(&((*png_data_)[0])), - png_data_->size()); - post_body.append("\r\n"); - } - - // TODO(awalker): include the page source if we can get it. - if (include_page_source_checkbox_->checked()) { - } - - // Terminate the body. - post_body.append("--" + mime_boundary + "--\r\n"); - - // We have the body of our POST, so send it off to the server. - URLFetcher* fetcher = new URLFetcher(post_url_, URLFetcher::POST, - new BugReportView::PostCleanup); - fetcher->set_request_context(profile_->GetRequestContext()); - std::string mime_type("multipart/form-data; boundary="); - mime_type += mime_boundary; - fetcher->set_upload_data(mime_type, post_body); - fetcher->Start(); -} - -void BugReportView::ReportPhishing() { - tab_->controller().LoadURL( - safe_browsing_util::GeneratePhishingReportUrl( - kReportPhishingUrl, WideToUTF8(page_url_text_->text())), - GURL(), - PageTransition::LINK); -} diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index f536735..096b254 100755 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -943,6 +943,8 @@ 'browser/browsing_instance.cc', 'browser/browsing_instance.h', 'browser/bubble_positioner.h', + 'browser/bug_report_util.cc', + 'browser/bug_report_util.h', 'browser/cancelable_request.cc', 'browser/cancelable_request.h', 'browser/cert_store.cc', |