Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / developer_private / entry_picker.cc
blob65d5638f54186be0f95b6b98b83c23fa13a4bfc4
1 // Copyright (c) 2012 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/extensions/api/developer_private/entry_picker.h"
7 #include "base/bind.h"
8 #include "base/files/file_path.h"
9 #include "base/strings/string_util.h"
10 #include "chrome/browser/extensions/api/developer_private/developer_private_api.h"
11 #include "chrome/browser/platform_util.h"
12 #include "chrome/browser/ui/chrome_select_file_policy.h"
13 #include "content/public/browser/web_contents.h"
14 #include "ui/shell_dialogs/select_file_dialog.h"
16 namespace {
18 bool g_skip_picker_for_test = false;
19 base::FilePath* g_path_to_be_picked_for_test = NULL;
21 } // namespace
23 namespace extensions {
25 namespace api {
27 EntryPicker::EntryPicker(EntryPickerClient* client,
28 content::WebContents* web_contents,
29 ui::SelectFileDialog::Type picker_type,
30 const base::FilePath& last_directory,
31 const base::string16& select_title,
32 const ui::SelectFileDialog::FileTypeInfo& info,
33 int file_type_index)
34 : client_(client) {
35 if (g_skip_picker_for_test) {
36 if (g_path_to_be_picked_for_test) {
37 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
38 base::Bind(
39 &EntryPicker::FileSelected,
40 base::Unretained(this), *g_path_to_be_picked_for_test, 1,
41 static_cast<void*>(nullptr)));
42 } else {
43 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
44 base::Bind(
45 &EntryPicker::FileSelectionCanceled,
46 base::Unretained(this), static_cast<void*>(nullptr)));
48 return;
51 select_file_dialog_ = ui::SelectFileDialog::Create(
52 this, new ChromeSelectFilePolicy(web_contents));
54 gfx::NativeWindow owning_window = web_contents ?
55 platform_util::GetTopLevel(web_contents->GetNativeView()) :
56 nullptr;
58 select_file_dialog_->SelectFile(picker_type,
59 select_title,
60 last_directory,
61 &info,
62 file_type_index,
63 base::FilePath::StringType(),
64 owning_window,
65 nullptr);
68 EntryPicker::~EntryPicker() {}
70 void EntryPicker::FileSelected(const base::FilePath& path,
71 int index,
72 void* params) {
73 client_->FileSelected(path);
74 delete this;
77 void EntryPicker::FileSelectionCanceled(void* params) {
78 client_->FileSelectionCanceled();
79 delete this;
82 void EntryPicker::MultiFilesSelected(const std::vector<base::FilePath>& files,
83 void* params) {
84 NOTREACHED();
85 client_->FileSelectionCanceled();
86 delete this;
89 // static
90 void EntryPicker::SkipPickerAndAlwaysSelectPathForTest(
91 base::FilePath* path) {
92 g_skip_picker_for_test = true;
93 g_path_to_be_picked_for_test = path;
96 // static
97 void EntryPicker::SkipPickerAndAlwaysCancelForTest() {
98 g_skip_picker_for_test = true;
99 g_path_to_be_picked_for_test = NULL;
102 // static
103 void EntryPicker::StopSkippingPickerForTest() {
104 g_skip_picker_for_test = false;
107 } // namespace api
109 } // namespace extensions