summaryrefslogtreecommitdiffstats
path: root/chrome/browser/cocoa/bug_report_window_controller.mm
blob: 0750710f9bdba9eb138435e641d4022d32215cef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// 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.

#import "chrome/browser/cocoa/bug_report_window_controller.h"

#include "app/l10n_util_mac.h"
#include "base/mac_util.h"
#include "base/sys_string_conversions.h"
#include "chrome/browser/bug_report_util.h"
#include "chrome/browser/tab_contents/tab_contents.h"
#include "chrome/browser/tab_contents/tab_contents_view.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"

@implementation BugReportWindowController

@synthesize bugDescription = bugDescription_;
@synthesize bugType = bugType_;
@synthesize pageURL = pageURL_;
@synthesize pageTitle = pageTitle_;
@synthesize sendReportButton = sendReportButton_;
@synthesize sendScreenshot = sendScreenshot_;
@synthesize disableScreenshot = disableScreenshot_;
@synthesize bugTypeList = bugTypeList_;

- (id)initWithTabContents:(TabContents*)currentTab
                  profile:(Profile*)profile {
  NSString* nibpath = [mac_util::MainAppBundle() pathForResource:@"ReportBug"
                                                          ofType:@"nib"];
  if ((self = [super initWithWindowNibPath:nibpath owner:self])) {
    currentTab_ = currentTab;
    profile_ = profile;
    [self setBugDescription:@""];

    if (currentTab_ != NULL) {
      // Get data from current tab, if one exists. This dialog could be called
      // from the main menu with no tab contents, so currentTab_ is not
      // guaranteed to be non-NULL.
      // TODO(mirandac): This dialog should be a tab-modal sheet if a browser
      // window exists.
      [self setSendScreenshot:YES];
      [self setDisableScreenshot:NO];
      bugTypeList_ = [[NSArray alloc] initWithObjects:
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_PAGE_WONT_LOAD),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_PAGE_LOOKS_ODD),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_PHISHING_PAGE),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_CANT_SIGN_IN),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_CHROME_MISBEHAVES),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_SOMETHING_MISSING),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_BROWSER_CRASH),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_OTHER_PROBLEM),
          nil];
      [self setPageURL:base::SysUTF8ToNSString(
          currentTab_->controller().GetActiveEntry()->url().spec())];
      [self setPageTitle:base::SysUTF16ToNSString(currentTab_->GetTitle())];
      mac_util::GrabWindowSnapshot(
          currentTab_->view()->GetTopLevelNativeWindow(), &pngData_);
    } else {
      // If no current tab exists, create a menu without the "broken page"
      // options, with page URL and title empty, and screenshot disabled.
      [self setSendScreenshot:NO];
      [self setDisableScreenshot:YES];
      bugTypeList_ = [[NSArray alloc] initWithObjects:
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_CHROME_MISBEHAVES),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_SOMETHING_MISSING),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_BROWSER_CRASH),
          l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_OTHER_PROBLEM),
          nil];
      // Because "Report Bug" is being called with no browser open in this
      // case, make URL and title empty.
      [self setPageURL:@""];
      [self setPageTitle:@""];
    }
  }
  return self;
}

- (void)dealloc {
  [pageURL_ release];
  [pageTitle_ release];
  [bugDescription_ release];
  [bugTypeList_ release];
  [super dealloc];
}

// Delegate callback so that closing the window deletes the controller.
- (void)windowWillClose:(NSNotification*)notification {
  [self autorelease];
}

- (void)closeDialog {
  [NSApp stopModal];
  [[self window] close];
}

- (void)runModalDialog {
  NSWindow* bugReportWindow = [self window];
  [bugReportWindow center];
  [NSApp runModalForWindow:bugReportWindow];
}

- (IBAction)sendReport:(id)sender {
  if ([self isPhishingReport]) {
    BugReportUtil::ReportPhishing(currentTab_,
                                  base::SysNSStringToUTF8(pageURL_));
  } else {
    BugReportUtil::SendReport(
        profile_,
        base::SysNSStringToUTF8(pageTitle_),
        bugType_,
        base::SysNSStringToUTF8(pageURL_),
        base::SysNSStringToUTF8(bugDescription_),
        sendScreenshot_ && !pngData_.empty() ?
            reinterpret_cast<const char *>(&(pngData_[0])) : NULL,
        pngData_.size());
  }
  [self closeDialog];
}

- (IBAction)cancel:(id)sender {
  [self closeDialog];
}

- (BOOL)isPhishingReport {
  return bugType_ == [bugTypeList_ indexOfObject:
      l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_PHISHING_PAGE)];
}

- (void)menu:(NSMenu *)menu willHighlightItem:(NSMenuItem *)item {
  NSString* buttonTitle = [[item title] isEqualToString:
      l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_PHISHING_PAGE)] ?
      l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_SEND_PHISHING_REPORT) :
      l10n_util::GetNSStringWithFixup(IDS_BUGREPORT_SEND_REPORT);
  if (![buttonTitle isEqualToString:[sendReportButton_ title]]) {
    [sendReportButton_ setTitle:buttonTitle];
    CGFloat deltaWidth =
        [GTMUILocalizerAndLayoutTweaker sizeToFitView:sendReportButton_].width;
    NSRect newButtonFrame = [sendReportButton_ frame];
    newButtonFrame.origin.x -= deltaWidth;
    [sendReportButton_ setFrame:newButtonFrame];
  }
}

// BugReportWindowController needs to change the title of the Send Report
// button when the user chooses the phishing bug type, so we need to bind
// the function that changes the button title to the bug type key.
+ (NSSet*)keyPathsForValuesAffectingValueForKey:(NSString*)key {
  NSSet* paths = [super keyPathsForValuesAffectingValueForKey:key];
  if ([key isEqualToString:@"isPhishingReport"]) {
    paths = [paths setByAddingObject:@"bugType"];
  }
  return paths;
}

@end