summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/net_error_diagnostics_dialog_win.cc
blob: 925be7d7106ba1ebd591e1d7af3508a5f6367ef5 (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
// Copyright 2015 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/net/net_error_diagnostics_dialog.h"

// Winsock.h must be included before ndfapi.h.
#include <winsock2.h>  // NOLINT
#include <ndfapi.h>    // NOLINT
#include <windows.h>   // NOLINT

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/native_library.h"
#include "base/scoped_native_library.h"
#include "base/strings/string16.h"
#include "base/task_runner.h"
#include "base/threading/thread.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/windows_version.h"
#include "content/public/browser/web_contents.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/shell_dialogs/base_shell_dialog_win.h"
#include "ui/views/win/hwnd_util.h"

namespace {

class NetErrorDiagnosticsDialog : public ui::BaseShellDialogImpl {
 public:
  NetErrorDiagnosticsDialog() {}
  ~NetErrorDiagnosticsDialog() override {}

  // NetErrorDiagnosticsDialog implementation.
  void Show(content::WebContents* web_contents,
            const std::string& failed_url,
            const base::Closure& callback) {
    DCHECK(!callback.is_null());

    HWND parent =
        views::HWNDForNativeWindow(web_contents->GetTopLevelNativeWindow());
    if (IsRunningDialogForOwner(parent))
      return;

    RunState run_state = BeginRun(parent);
    run_state.dialog_thread->task_runner()->PostTaskAndReply(
        FROM_HERE,
        base::Bind(&NetErrorDiagnosticsDialog::ShowDialogOnPrivateThread,
                   base::Unretained(this), parent, failed_url),
        base::Bind(&NetErrorDiagnosticsDialog::DiagnosticsDone,
                   base::Unretained(this), run_state, callback));
  }

 private:
  void ShowDialogOnPrivateThread(HWND parent, const std::string& failed_url) {
    NDFHANDLE incident_handle;
    base::string16 failed_url_wide = base::UTF8ToUTF16(failed_url);
    if (!SUCCEEDED(NdfCreateWebIncident(failed_url_wide.c_str(),
                                        &incident_handle))) {
      return;
    }
    NdfExecuteDiagnosis(incident_handle, parent);
    NdfCloseIncident(incident_handle);
  }

  void DiagnosticsDone(RunState run_state, const base::Closure& callback) {
    EndRun(run_state);
    callback.Run();
  }

  DISALLOW_COPY_AND_ASSIGN(NetErrorDiagnosticsDialog);
};

}  // namespace

bool CanShowNetworkDiagnosticsDialog() {
  // Not present on XP and Windows 2003.
  // TODO(mmenke): Remove this logic once Chrome requires Vista or later.
  return base::win::OSInfo::GetInstance()->version() >=
         base::win::VERSION_VISTA;
}

void ShowNetworkDiagnosticsDialog(content::WebContents* web_contents,
                                  const std::string& failed_url) {
  DCHECK(CanShowNetworkDiagnosticsDialog());

  NetErrorDiagnosticsDialog* dialog = new NetErrorDiagnosticsDialog();
  dialog->Show(
      web_contents, failed_url,
      base::Bind(&base::DeletePointer<NetErrorDiagnosticsDialog>, dialog));
}