Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / net / net_error_diagnostics_dialog_win.cc
blob925be7d7106ba1ebd591e1d7af3508a5f6367ef5
1 // Copyright 2015 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.
5 #include "chrome/browser/net/net_error_diagnostics_dialog.h"
7 // Winsock.h must be included before ndfapi.h.
8 #include <winsock2.h> // NOLINT
9 #include <ndfapi.h> // NOLINT
10 #include <windows.h> // NOLINT
12 #include "base/bind.h"
13 #include "base/bind_helpers.h"
14 #include "base/files/file_path.h"
15 #include "base/location.h"
16 #include "base/logging.h"
17 #include "base/macros.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/native_library.h"
21 #include "base/scoped_native_library.h"
22 #include "base/strings/string16.h"
23 #include "base/task_runner.h"
24 #include "base/threading/thread.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/win/windows_version.h"
27 #include "content/public/browser/web_contents.h"
28 #include "ui/gfx/native_widget_types.h"
29 #include "ui/shell_dialogs/base_shell_dialog_win.h"
30 #include "ui/views/win/hwnd_util.h"
32 namespace {
34 class NetErrorDiagnosticsDialog : public ui::BaseShellDialogImpl {
35 public:
36 NetErrorDiagnosticsDialog() {}
37 ~NetErrorDiagnosticsDialog() override {}
39 // NetErrorDiagnosticsDialog implementation.
40 void Show(content::WebContents* web_contents,
41 const std::string& failed_url,
42 const base::Closure& callback) {
43 DCHECK(!callback.is_null());
45 HWND parent =
46 views::HWNDForNativeWindow(web_contents->GetTopLevelNativeWindow());
47 if (IsRunningDialogForOwner(parent))
48 return;
50 RunState run_state = BeginRun(parent);
51 run_state.dialog_thread->task_runner()->PostTaskAndReply(
52 FROM_HERE,
53 base::Bind(&NetErrorDiagnosticsDialog::ShowDialogOnPrivateThread,
54 base::Unretained(this), parent, failed_url),
55 base::Bind(&NetErrorDiagnosticsDialog::DiagnosticsDone,
56 base::Unretained(this), run_state, callback));
59 private:
60 void ShowDialogOnPrivateThread(HWND parent, const std::string& failed_url) {
61 NDFHANDLE incident_handle;
62 base::string16 failed_url_wide = base::UTF8ToUTF16(failed_url);
63 if (!SUCCEEDED(NdfCreateWebIncident(failed_url_wide.c_str(),
64 &incident_handle))) {
65 return;
67 NdfExecuteDiagnosis(incident_handle, parent);
68 NdfCloseIncident(incident_handle);
71 void DiagnosticsDone(RunState run_state, const base::Closure& callback) {
72 EndRun(run_state);
73 callback.Run();
76 DISALLOW_COPY_AND_ASSIGN(NetErrorDiagnosticsDialog);
79 } // namespace
81 bool CanShowNetworkDiagnosticsDialog() {
82 // Not present on XP and Windows 2003.
83 // TODO(mmenke): Remove this logic once Chrome requires Vista or later.
84 return base::win::OSInfo::GetInstance()->version() >=
85 base::win::VERSION_VISTA;
88 void ShowNetworkDiagnosticsDialog(content::WebContents* web_contents,
89 const std::string& failed_url) {
90 DCHECK(CanShowNetworkDiagnosticsDialog());
92 NetErrorDiagnosticsDialog* dialog = new NetErrorDiagnosticsDialog();
93 dialog->Show(
94 web_contents, failed_url,
95 base::Bind(&base::DeletePointer<NetErrorDiagnosticsDialog>, dialog));