summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/views/options/exception_editor_view.h
blob: 571e29e4b351e27f3d01cc3432e8c7ba5f867d65 (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
// Copyright (c) 2010 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_UI_VIEWS_OPTIONS_EXCEPTION_EDITOR_VIEW_H_
#define CHROME_BROWSER_UI_VIEWS_OPTIONS_EXCEPTION_EDITOR_VIEW_H_
#pragma once

#include <string>

#include "chrome/browser/content_setting_combo_model.h"
#include "chrome/browser/host_content_settings_map.h"
#include "chrome/common/content_settings.h"
#include "chrome/common/content_settings_types.h"
#include "views/window/dialog_delegate.h"
#include "views/controls/button/checkbox.h"
#include "views/controls/combobox/combobox.h"
#include "views/controls/textfield/textfield.h"

namespace views {
class ImageView;
class Label;
}

class ContentExceptionsTableModel;

// ExceptionEditorView is responsible for showing a dialog that allows the user
// to create or edit a single content exception. If the user clicks ok the
// delegate is notified and completes the edit.
//
// To use an ExceptionEditorView create one and invoke Show on it.
// ExceptionEditorView is deleted when the dialog closes.
class ExceptionEditorView : public views::View,
                            public views::Textfield::Controller,
                            public views::DialogDelegate {
 public:
  class Delegate {
   public:
    // Invoked when the user accepts the edit.
    virtual void AcceptExceptionEdit(
        const HostContentSettingsMap::Pattern& pattern,
        ContentSetting setting,
        bool is_off_the_record,
        int index,
        bool is_new) = 0;

   protected:
    virtual ~Delegate() {}
  };

  // Creates a new ExceptionEditorView with the supplied args. |index| is the
  // index into the ContentExceptionsTableModel of the exception. This is not
  // used by ExceptionEditorView but instead passed to the delegate.
  ExceptionEditorView(Delegate* delegate,
                      ContentExceptionsTableModel* model,
                      bool allow_off_the_record,
                      int index,
                      const HostContentSettingsMap::Pattern& pattern,
                      ContentSetting setting,
                      bool is_off_the_record);
  virtual ~ExceptionEditorView() {}

  void Show(gfx::NativeWindow parent);

  // views::DialogDelegate overrides.
  virtual bool IsModal() const;
  virtual std::wstring GetWindowTitle() const;
  virtual bool IsDialogButtonEnabled(
      MessageBoxFlags::DialogButton button) const;
  virtual bool Cancel();
  virtual bool Accept();
  virtual views::View* GetContentsView();

  // views::Textfield::Controller overrides. Updates whether the user can
  // accept the dialog as well as updating image views showing whether value is
  // valid.
  virtual void ContentsChanged(views::Textfield* sender,
                               const std::wstring& new_contents);
  virtual bool HandleKeystroke(views::Textfield* sender,
                               const views::Textfield::Keystroke& key);

 private:
  void Init();

  views::Label* CreateLabel(int message_id);

  // Returns true if we're adding a new item.
  bool is_new() const { return index_ == -1; }

  bool IsPatternValid(const HostContentSettingsMap::Pattern& pattern,
                      bool is_off_the_record) const;

  void UpdateImageView(views::ImageView* image_view, bool is_valid);

  Delegate* delegate_;
  ContentExceptionsTableModel* model_;
  ContentSettingComboModel cb_model_;

  // Index of the item being edited. If -1, indices this is a new entry.
  const bool allow_off_the_record_;
  const int index_;
  const HostContentSettingsMap::Pattern pattern_;
  const ContentSetting setting_;
  const bool is_off_the_record_;

  views::Textfield* pattern_tf_;
  views::ImageView* pattern_iv_;
  views::Combobox* action_cb_;
  views::Checkbox* incognito_cb_;

  DISALLOW_COPY_AND_ASSIGN(ExceptionEditorView);
};

#endif  // CHROME_BROWSER_UI_VIEWS_OPTIONS_EXCEPTION_EDITOR_VIEW_H_