1// Copyright (c) 2011 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#ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_CERTIFICATE_MANAGER_HANDLER_H_
6#define CHROME_BROWSER_UI_WEBUI_OPTIONS_CERTIFICATE_MANAGER_HANDLER_H_
7#pragma once
8
9#include <string>
10
11#include "base/memory/scoped_ptr.h"
12#include "chrome/browser/ui/shell_dialogs.h"
13#include "chrome/browser/ui/webui/options/options_ui.h"
14#include "content/browser/cancelable_request.h"
15#include "content/browser/certificate_manager_model.h"
16#include "net/base/cert_database.h"
17#include "ui/gfx/native_widget_types.h"
18
19class FileAccessProvider;
20
21class CertificateManagerHandler : public OptionsPageUIHandler,
22    public CertificateManagerModel::Observer,
23    public SelectFileDialog::Listener {
24 public:
25  CertificateManagerHandler();
26  virtual ~CertificateManagerHandler();
27
28  // OptionsPageUIHandler implementation.
29  virtual void GetLocalizedValues(DictionaryValue* localized_strings);
30  virtual void RegisterMessages();
31
32  // CertificateManagerModel::Observer implementation.
33  virtual void CertificatesRefreshed();
34
35  // SelectFileDialog::Listener implementation.
36  virtual void FileSelected(const FilePath& path, int index, void* params);
37  virtual void FileSelectionCanceled(void* params);
38
39 private:
40  // View certificate.
41  void View(const ListValue* args);
42
43  // Edit server certificate trust values.
44  void EditServer(const ListValue* args);
45
46  // Edit certificate authority trust values.  The sequence goes like:
47  //  1. user clicks edit button -> CertificateEditCaTrustOverlay.show ->
48  //  GetCATrust -> CertificateEditCaTrustOverlay.populateTrust
49  //  2. user clicks ok -> EditCATrust -> CertificateEditCaTrustOverlay.dismiss
50  void GetCATrust(const ListValue* args);
51  void EditCATrust(const ListValue* args);
52
53  // Cleanup state stored during import or export process.
54  void CancelImportExportProcess(const ListValue* args);
55  void ImportExportCleanup();
56
57  // Export to PKCS #12 file.  The sequence goes like:
58  //  1a. user click on export button -> ExportPersonal -> launches file
59  //  selector
60  //  1b. user click on export all button -> ExportAllPersonal -> launches file
61  //  selector
62  //  2. user selects file -> ExportPersonalFileSelected -> launches password
63  //  dialog
64  //  3. user enters password -> ExportPersonalPasswordSelected -> unlock slots
65  //  4. slots unlocked -> ExportPersonalSlotsUnlocked -> exports to memory
66  //  buffer -> starts async write operation
67  //  5. write finishes (or fails) -> ExportPersonalFileWritten
68  void ExportPersonal(const ListValue* args);
69  void ExportAllPersonal(const ListValue* args);
70  void ExportPersonalFileSelected(const FilePath& path);
71  void ExportPersonalPasswordSelected(const ListValue* args);
72  void ExportPersonalSlotsUnlocked();
73  void ExportPersonalFileWritten(int write_errno, int bytes_written);
74
75  // Import from PKCS #12 file.  The sequence goes like:
76  //  1. user click on import button -> StartImportPersonal -> launches file
77  //  selector
78  //  2. user selects file -> ImportPersonalFileSelected -> launches password
79  //  dialog
80  //  3. user enters password -> ImportPersonalPasswordSelected -> starts async
81  //  read operation
82  //  4. read operation completes -> ImportPersonalFileRead -> unlock slot
83  //  5. slot unlocked -> ImportPersonalSlotUnlocked attempts to
84  //  import with previously entered password
85  //  6a. if import succeeds -> ImportExportCleanup
86  //  6b. if import fails -> show error, ImportExportCleanup
87  //  TODO(mattm): allow retrying with different password
88  void StartImportPersonal(const ListValue* args);
89  void ImportPersonalFileSelected(const FilePath& path);
90  void ImportPersonalPasswordSelected(const ListValue* args);
91  void ImportPersonalFileRead(int read_errno, std::string data);
92  void ImportPersonalSlotUnlocked();
93
94  // Import Server certificates from file.  Sequence goes like:
95  //  1. user clicks on import button -> ImportServer -> launches file selector
96  //  2. user selects file -> ImportServerFileSelected -> starts async read
97  //  3. read completes -> ImportServerFileRead -> parse certs -> attempt import
98  //  4a. if import succeeds -> ImportExportCleanup
99  //  4b. if import fails -> show error, ImportExportCleanup
100  void ImportServer(const ListValue* args);
101  void ImportServerFileSelected(const FilePath& path);
102  void ImportServerFileRead(int read_errno, std::string data);
103
104  // Import Certificate Authorities from file.  Sequence goes like:
105  //  1. user clicks on import button -> ImportCA -> launches file selector
106  //  2. user selects file -> ImportCAFileSelected -> starts async read
107  //  3. read completes -> ImportCAFileRead -> parse certs ->
108  //  CertificateEditCaTrustOverlay.showImport
109  //  4. user clicks ok -> ImportCATrustSelected -> attempt import
110  //  5a. if import succeeds -> ImportExportCleanup
111  //  5b. if import fails -> show error, ImportExportCleanup
112  void ImportCA(const ListValue* args);
113  void ImportCAFileSelected(const FilePath& path);
114  void ImportCAFileRead(int read_errno, std::string data);
115  void ImportCATrustSelected(const ListValue* args);
116
117  // Export a certificate.
118  void Export(const ListValue* args);
119
120  // Delete certificate and private key (if any).
121  void Delete(const ListValue* args);
122
123  // Populate the trees in all the tabs.
124  void Populate(const ListValue* args);
125
126  // Populate the given tab's tree.
127  void PopulateTree(const std::string& tab_name, net::CertType type);
128
129  // Display a WebUI error message box.
130  void ShowError(const std::string& title, const std::string& error) const;
131
132  // Display a WebUI error message box for import failures.
133  // Depends on |selected_cert_list_| being set to the imports that we
134  // attempted to import.
135  void ShowImportErrors(
136      const std::string& title,
137      const net::CertDatabase::ImportCertFailureList& not_imported) const;
138
139#if defined(OS_CHROMEOS)
140  // Check whether Tpm token is ready and notifiy JS side.
141  void CheckTpmTokenReady(const ListValue* args);
142#endif
143
144  gfx::NativeWindow GetParentWindow() const;
145
146  // The Certificates Manager model
147  scoped_ptr<CertificateManagerModel> certificate_manager_model_;
148
149  // For multi-step import or export processes, we need to store the path,
150  // password, etc the user chose while we wait for them to enter a password,
151  // wait for file to be read, etc.
152  FilePath file_path_;
153  string16 password_;
154  bool use_hardware_backed_;
155  std::string file_data_;
156  net::CertificateList selected_cert_list_;
157  scoped_refptr<SelectFileDialog> select_file_dialog_;
158  scoped_refptr<net::CryptoModule> module_;
159
160  // Used in reading and writing certificate files.
161  CancelableRequestConsumer consumer_;
162  scoped_refptr<FileAccessProvider> file_access_provider_;
163
164  DISALLOW_COPY_AND_ASSIGN(CertificateManagerHandler);
165};
166
167#endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_CERTIFICATE_MANAGER_HANDLER_H_
168