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