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.
4
5 #include "chrome/browser/extensions/api/developer_private/entry_picker.h"
6
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"
15
16 namespace {
17
18 bool g_skip_picker_for_test = false;
19 base::FilePath* g_path_to_be_picked_for_test = NULL;
20
21 } // namespace
22
23 namespace extensions {
24
25 namespace api {
26
EntryPicker(EntryPickerClient * client,content::WebContents * web_contents,ui::SelectFileDialog::Type picker_type,const base::FilePath & last_directory,const base::string16 & select_title,const ui::SelectFileDialog::FileTypeInfo & info,int file_type_index)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 select_file_dialog_ = ui::SelectFileDialog::Create(
36 this, new ChromeSelectFilePolicy(web_contents));
37
38 gfx::NativeWindow owning_window = web_contents ?
39 platform_util::GetTopLevel(web_contents->GetNativeView()) :
40 NULL;
41
42 if (g_skip_picker_for_test) {
43 if (g_path_to_be_picked_for_test) {
44 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
45 base::Bind(
46 &EntryPicker::FileSelected,
47 base::Unretained(this), *g_path_to_be_picked_for_test, 1,
48 static_cast<void*>(NULL)));
49 } else {
50 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
51 base::Bind(
52 &EntryPicker::FileSelectionCanceled,
53 base::Unretained(this), static_cast<void*>(NULL)));
54 }
55 return;
56 }
57
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 NULL);
66 }
67
~EntryPicker()68 EntryPicker::~EntryPicker() {}
69
FileSelected(const base::FilePath & path,int index,void * params)70 void EntryPicker::FileSelected(const base::FilePath& path,
71 int index,
72 void* params) {
73 client_->FileSelected(path);
74 delete this;
75 }
76
FileSelectionCanceled(void * params)77 void EntryPicker::FileSelectionCanceled(void* params) {
78 client_->FileSelectionCanceled();
79 delete this;
80 }
81
82 // static
SkipPickerAndAlwaysSelectPathForTest(base::FilePath * path)83 void EntryPicker::SkipPickerAndAlwaysSelectPathForTest(
84 base::FilePath* path) {
85 g_skip_picker_for_test = true;
86 g_path_to_be_picked_for_test = path;
87 }
88
89 // static
SkipPickerAndAlwaysCancelForTest()90 void EntryPicker::SkipPickerAndAlwaysCancelForTest() {
91 g_skip_picker_for_test = true;
92 g_path_to_be_picked_for_test = NULL;
93 }
94
95 // static
StopSkippingPickerForTest()96 void EntryPicker::StopSkippingPickerForTest() {
97 g_skip_picker_for_test = false;
98 }
99
100 } // namespace api
101
102 } // namespace extensions
103