summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/clear_server_data.cc
blob: 6a9d54d0092c8f22164847b0a5e73aea074f6b30 (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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright (c) 2011 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/ui/views/clear_server_data.h"

#include "app/l10n_util.h"
#include "app/resource_bundle.h"
#include "base/command_line.h"
#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_window.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_model.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "gfx/insets.h"
#include "grit/generated_resources.h"
#include "grit/locale_settings.h"
#include "net/url_request/url_request_context.h"
#include "views/background.h"
#include "views/controls/button/checkbox.h"
#include "views/controls/label.h"
#include "views/controls/separator.h"
#include "views/controls/throbber.h"
#include "views/grid_layout.h"
#include "views/standard_layout.h"
#include "views/widget/widget.h"
#include "views/window/dialog_client_view.h"
#include "views/window/window.h"

using views::GridLayout;
using views::ColumnSet;

// The combo box is vertically aligned to the 'time-period' label, which makes
// the combo box look a little too close to the check box above it when we use
// standard layout to separate them. We therefore add a little extra margin to
// the label, giving it a little breathing space.
static const int kExtraMarginForTimePeriodLabel = 3;

////////////////////////////////////////////////////////////////////////////////
// ClearServerDataView, public:

ClearServerDataView::ClearServerDataView(Profile* profile,
    ClearDataView* clear_data_view)
    : profile_(profile),
      clear_data_parent_window_(clear_data_view),
      allow_clear_(true) {
  DCHECK(profile);
  DCHECK(clear_data_view);

  // Always show preferences for the original profile. Most state when off
  // the record comes from the original profile, but we explicitly use
  // the original profile to avoid potential problems.
  profile_ = profile->GetOriginalProfile();
  sync_service_ = profile_->GetProfileSyncService();

  if (NULL != sync_service_) {
    sync_service_->ResetClearServerDataState();
    sync_service_->AddObserver(this);
  }

  Init();
  InitControlLayout();
  InitControlVisibility();
}

ClearServerDataView::~ClearServerDataView(void) {
  if (NULL != sync_service_) {
    sync_service_->RemoveObserver(this);
  }
}

void ClearServerDataView::Init() {
  ResourceBundle& rb = ResourceBundle::GetSharedInstance();
  gfx::Font title_font =
      rb.GetFont(ResourceBundle::BaseFont).DeriveFont(0, gfx::Font::BOLD);

  flash_title_label_ = new views::Label(
      UTF16ToWide(l10n_util::GetStringUTF16(IDS_CLEAR_DATA_ADOBE_FLASH_TITLE)));
  flash_title_label_->SetFont(title_font);

  flash_description_label_= new views::Label(UTF16ToWide(
      l10n_util::GetStringUTF16(IDS_CLEAR_DATA_ADOBE_FLASH_DESCRIPTION)));
  flash_link_ = new views::Link(
      UTF16ToWide(l10n_util::GetStringUTF16(IDS_FLASH_STORAGE_SETTINGS)));
  flash_link_->SetController(this);

  chrome_sync_title_label_= new views::Label(
      UTF16ToWide(l10n_util::GetStringUTF16(IDS_CLEAR_DATA_CHROME_SYNC_TITLE)));
  chrome_sync_title_label_->SetFont(title_font);

  chrome_sync_description_label_ = new views::Label(UTF16ToWide(
      l10n_util::GetStringUTF16(IDS_CLEAR_DATA_CLEAR_SERVER_DATA_DESCRIPTION)));

  clear_server_data_button_= new views::NativeButton(
      this,
      UTF16ToWide(l10n_util::GetStringUTF16(IDS_CLEAR_DATA_CLEAR_BUTTON)));

  dashboard_label_ = new views::Label(
      UTF16ToWide(l10n_util::GetStringUTF16(IDS_CLEAR_DASHBOARD_DESCRIPTION)));

  dashboard_link_ = new views::Link();
  dashboard_link_->SetController(this);
  dashboard_link_->SetText(UTF16ToWide(
      l10n_util::GetStringUTF16(IDS_SYNC_PRIVACY_DASHBOARD_LINK_LABEL)));

  status_label_ = new views::Label(
      UTF16ToWide(l10n_util::GetStringUTF16(IDS_CLEAR_DATA_DELETING)));
  throbber_ = new views::Throbber(50, true);
}

void ClearServerDataView::InitControlLayout() {
  GridLayout* layout = CreatePanelGridLayout(this);
  this->SetLayoutManager(layout);

  int centered_column_set_id = 0;
  ColumnSet * column_set = layout->AddColumnSet(centered_column_set_id);
  column_set->AddColumn(GridLayout::FILL, GridLayout::FILL, 1,
                        GridLayout::USE_PREF, 0, 0);

  int left_aligned_column_set_id = 1;
  column_set = layout->AddColumnSet(left_aligned_column_set_id);
  column_set->AddColumn(GridLayout::LEADING, GridLayout::FILL, 1,
                        GridLayout::USE_PREF, 0, 0);

  const int three_column_set_id = 2;
  column_set = layout->AddColumnSet(three_column_set_id);
  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 0,
                        GridLayout::USE_PREF, 0, 0);
  column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
                        GridLayout::USE_PREF, 0, 0);
  column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
                        GridLayout::USE_PREF, 0, 0);

  AddWrappingLabelRow(layout, flash_title_label_,
                      centered_column_set_id, true);
  AddWrappingLabelRow(layout, flash_description_label_,
                      centered_column_set_id, true);

  layout->StartRow(0, left_aligned_column_set_id);
  layout->AddView(flash_link_);
  layout->AddPaddingRow(0, kPanelVertMargin);

  AddWrappingLabelRow(layout, chrome_sync_title_label_,
                      centered_column_set_id, true);
  AddWrappingLabelRow(layout, chrome_sync_description_label_,
                      centered_column_set_id, true);

  layout->StartRow(0, three_column_set_id);
  layout->AddView(clear_server_data_button_, 1, 1,
                  GridLayout::LEADING, GridLayout::CENTER);
  layout->AddView(status_label_, 1, 1,
                  GridLayout::LEADING, GridLayout::CENTER);
  layout->AddView(throbber_, 1, 1,
                  GridLayout::TRAILING, GridLayout::CENTER);
  layout->AddPaddingRow(0, kPanelVertMargin);

  AddWrappingLabelRow(layout, dashboard_label_, centered_column_set_id, true);

  layout->StartRow(0, left_aligned_column_set_id);
  layout->AddView(dashboard_link_);
}

void ClearServerDataView::InitControlVisibility() {
  bool allow_clear_server_data_ui =
      CommandLine::ForCurrentProcess()->HasSwitch(
          switches::kEnableClearServerData);

  // Hide progress indicators
  throbber_->SetVisible(false);
  status_label_->SetVisible(false);

  // Only show the sync portion if not behind the flag
  chrome_sync_title_label_->SetVisible(allow_clear_server_data_ui);
  chrome_sync_description_label_->SetVisible(allow_clear_server_data_ui);
  clear_server_data_button_->SetVisible(allow_clear_server_data_ui);
  dashboard_label_->SetVisible(allow_clear_server_data_ui);
  dashboard_link_->SetVisible(allow_clear_server_data_ui);

  // Enable our clear button, set false for delete_in_progress
  UpdateClearButtonEnabledState(false);
}

void ClearServerDataView::SetAllowClear(bool allow) {
  allow_clear_ = allow;
  UpdateControlEnabledState();
}

////////////////////////////////////////////////////////////////////////////////
// ClearServerDataView, views::View implementation:

void ClearServerDataView::AddWrappingLabelRow(views::GridLayout* layout,
                                              views::Label* label,
                                              int id,
                                              bool related_follows) {
  label->SetMultiLine(true);
  label->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
  layout->StartRow(0, id);
  layout->AddView(label);
  AddSpacing(layout, related_follows);
}

void ClearServerDataView::AddSpacing(views::GridLayout* layout,
                                     bool related_follows) {
  layout->AddPaddingRow(0, related_follows ? kRelatedControlVerticalSpacing
                                           : kUnrelatedControlVerticalSpacing);
}

gfx::Size ClearServerDataView::GetPreferredSize() {
  // If we didn't return a preferred size, the containing view auto-sizes to
  // the width of the narrowest button, which is not what we want
  return gfx::Size(views::Window::GetLocalizedContentsSize(
      IDS_CLEARDATA_DIALOG_WIDTH_CHARS,
      IDS_CLEARDATA_DIALOG_HEIGHT_LINES));
}

////////////////////////////////////////////////////////////////////////////////
// ClearServerDataView, views::ButtonListener implementation:

void ClearServerDataView::ButtonPressed(
    views::Button* sender, const views::Event& event) {

  if (sender == clear_server_data_button_) {
    // Protect against the unlikely case where the server received a
    // message, and the syncer syncs and resets itself before the
    // user tries pressing the Clear button in this dialog again.
    // TODO(raz) Confirm whether we have an issue here
    if (sync_service_->HasSyncSetupCompleted()) {
      ConfirmMessageBoxDialog::Run(
          GetWindow()->GetNativeWindow(),
          this,
          UTF16ToWide(l10n_util::GetStringUTF16(IDS_CONFIRM_CLEAR_DESCRIPTION)),
          UTF16ToWide(l10n_util::GetStringUTF16(IDS_CONFIRM_CLEAR_TITLE)));
      }
  }

  UpdateControlEnabledState();
}

void ClearServerDataView::LinkActivated(views::Link* source,
                                          int event_flags) {
  Browser* browser = Browser::Create(profile_);
  if (source == flash_link_) {
    browser->OpenURL(GURL(l10n_util::GetStringUTF8(IDS_FLASH_STORAGE_URL)),
                     GURL(), NEW_FOREGROUND_TAB, PageTransition::LINK);
    browser->window()->Show();
  } else {
    browser->OpenPrivacyDashboardTabAndActivate();
  }
}

////////////////////////////////////////////////////////////////////////////////
// ClearServerDataView, ConfirmMessageBoxObserver implementation:

void ClearServerDataView::OnConfirmMessageAccept() {
  clear_data_parent_window_->StartClearingServerData();
  sync_service_->ClearServerData();
  UpdateControlEnabledState();
}

void ClearServerDataView::OnConfirmMessageCancel() {
  UpdateControlEnabledState();
}

////////////////////////////////////////////////////////////////////////////////
// ClearServerDataView, ProfileSyncServiceObserver implementation:

void ClearServerDataView::OnStateChanged() {
  // Note that we are listening for all notifications here.  Clearing from
  // another browser should cause the "clear" button to go gray (if a
  // notification nudges the syncer, causing OnStateChanged to be called with
  // sync disabled)
  UpdateControlEnabledState();
}

////////////////////////////////////////////////////////////////////////////////
// ClearServerDataView, private:

void ClearServerDataView::UpdateControlEnabledState() {
  bool delete_in_progress = false;

  // We only want to call Suceeded/FailedClearingServerData once, not every
  // time the view is refreshed.  As such, on success/failure we handle that
  // state and immediately reset things back to CLEAR_NOT_STARTED.
  ProfileSyncService::ClearServerDataState clear_state =
    profile_->GetProfileSyncService()->GetClearServerDataState();
  profile_->GetProfileSyncService()->ResetClearServerDataState();

  if (NULL != sync_service_) {
    sync_service_->ResetClearServerDataState();
  }

  switch (clear_state) {
    case ProfileSyncService::CLEAR_NOT_STARTED:
      // We can get here when we first start and after a failed clear (which
      // does not close the tab), do nothing.
      break;
    case ProfileSyncService::CLEAR_CLEARING:
        // Clearing buttons on all tabs are disabled at this
        // point, throbber is going
        status_label_->SetText(
            UTF16ToWide(l10n_util::GetStringUTF16(IDS_CLEAR_DATA_SENDING)));
        status_label_->SetVisible(true);
        delete_in_progress = true;
      break;
    case ProfileSyncService::CLEAR_FAILED:
        // Show an error and reallow clearing
        clear_data_parent_window_->FailedClearingServerData();
        status_label_->SetText(
            UTF16ToWide(l10n_util::GetStringUTF16(IDS_CLEAR_DATA_ERROR)));
        status_label_->SetVisible(true);
        delete_in_progress = false;
      break;
    case ProfileSyncService::CLEAR_SUCCEEDED:
        // Close the dialog box, success!
        status_label_->SetVisible(false);
        delete_in_progress = false;
        clear_data_parent_window_->SucceededClearingServerData();
      break;
    }

  // allow_clear can be false when a local browsing data clear is happening
  // from the neighboring tab.  delete_in_progress means that a clear is
  // pending in the current tab.
  UpdateClearButtonEnabledState(delete_in_progress);

  throbber_->SetVisible(delete_in_progress);
  if (delete_in_progress)
    throbber_->Start();
  else
    throbber_->Stop();
}

void ClearServerDataView::UpdateClearButtonEnabledState(
    bool delete_in_progress) {
  this->clear_server_data_button_->SetEnabled(
      sync_service_ != NULL &&
      sync_service_->HasSyncSetupCompleted() &&
      !delete_in_progress && allow_clear_);
}