Fix build break
[chromium-blink-merge.git] / ui / shell_dialogs / select_file_dialog_android.cc
blob740939f2f0969624c55a6e786ef37f07c127fcb2
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 "select_file_dialog_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "base/android/scoped_java_ref.h"
11 #include "base/logging.h"
12 #include "base/string_util.h"
13 #include "base/strings/string_split.h"
14 #include "base/utf_string_conversions.h"
15 #include "jni/SelectFileDialog_jni.h"
16 #include "ui/gfx/android/window_android.h"
18 namespace ui {
20 // static
21 SelectFileDialogImpl* SelectFileDialogImpl::Create(Listener* listener,
22 ui::SelectFilePolicy* policy) {
23 return new SelectFileDialogImpl(listener, policy);
26 void SelectFileDialogImpl::OnFileSelected(JNIEnv* env,
27 jobject java_object,
28 jstring filepath) {
29 if (listener_) {
30 std::string path = base::android::ConvertJavaStringToUTF8(env, filepath);
31 listener_->FileSelected(base::FilePath(path), 0, NULL);
34 is_running_ = false;
37 void SelectFileDialogImpl::OnFileNotSelected(
38 JNIEnv* env,
39 jobject java_object) {
40 if (listener_)
41 listener_->FileSelectionCanceled(NULL);
43 is_running_ = false;
46 bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow) const {
47 return is_running_;
50 void SelectFileDialogImpl::ListenerDestroyed() {
51 listener_ = NULL;
54 void SelectFileDialogImpl::SelectFileImpl(
55 ui::SelectFileDialog::Type type,
56 const string16& title,
57 const base::FilePath& default_path,
58 const SelectFileDialog::FileTypeInfo* file_types,
59 int file_type_index,
60 const std::string& default_extension,
61 gfx::NativeWindow owning_window,
62 void* params) {
63 JNIEnv* env = base::android::AttachCurrentThread();
65 std::vector<string16> accept_types =
66 *(reinterpret_cast<std::vector<string16>*>(params));
68 // The last string in params is expected to be the string with capture value.
69 ScopedJavaLocalRef<jstring> capture_value =
70 base::android::ConvertUTF16ToJavaString(env,
71 StringToLowerASCII(accept_types.back()));
72 base::android::CheckException(env);
73 accept_types.pop_back();
75 // The rest params elements are expected to be accept_types.
76 ScopedJavaLocalRef<jobjectArray> accept_types_java =
77 base::android::ToJavaArrayOfStrings(env, accept_types);
79 Java_SelectFileDialog_selectFile(env, java_object_.obj(),
80 accept_types_java.obj(),
81 capture_value.obj(),
82 owning_window->GetJavaObject().obj());
83 is_running_ = true;
86 bool SelectFileDialogImpl::RegisterSelectFileDialog(JNIEnv* env) {
87 return RegisterNativesImpl(env);
90 SelectFileDialogImpl::~SelectFileDialogImpl() {
93 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener,
94 ui::SelectFilePolicy* policy)
95 : ui::SelectFileDialog(listener, policy),
96 is_running_(false) {
97 JNIEnv* env = base::android::AttachCurrentThread();
98 java_object_.Reset(
99 Java_SelectFileDialog_create(env, reinterpret_cast<jint>(this)));
102 bool SelectFileDialogImpl::HasMultipleFileTypeChoicesImpl() {
103 NOTIMPLEMENTED();
104 return false;
107 SelectFileDialog* CreateAndroidSelectFileDialog(
108 SelectFileDialog::Listener* listener,
109 SelectFilePolicy* policy) {
110 return SelectFileDialogImpl::Create(listener, policy);
113 } // namespace ui