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 "components/web_contents_delegate_android/color_chooser_android.h"
7 #include "base/android/jni_array.h"
8 #include "base/android/jni_string.h"
9 #include "content/public/browser/android/content_view_core.h"
10 #include "content/public/browser/web_contents.h"
11 #include "content/public/common/color_suggestion.h"
12 #include "jni/ColorChooserAndroid_jni.h"
14 using base::android::ConvertUTF16ToJavaString;
16 namespace web_contents_delegate_android {
18 ColorChooserAndroid::ColorChooserAndroid(
19 content::WebContents* web_contents,
20 SkColor initial_color,
21 const std::vector<content::ColorSuggestion>& suggestions)
22 : web_contents_(web_contents) {
23 JNIEnv* env = AttachCurrentThread();
24 content::ContentViewCore* content_view_core =
25 content::ContentViewCore::FromWebContents(web_contents);
26 DCHECK(content_view_core);
28 ScopedJavaLocalRef<jobjectArray> suggestions_array;
30 if (suggestions.size() > 0) {
31 suggestions_array = Java_ColorChooserAndroid_createColorSuggestionArray(
32 env, suggestions.size());
34 for (size_t i = 0; i < suggestions.size(); ++i) {
35 const content::ColorSuggestion& suggestion = suggestions[i];
36 ScopedJavaLocalRef<jstring> label = ConvertUTF16ToJavaString(
37 env, suggestion.label);
38 Java_ColorChooserAndroid_addToColorSuggestionArray(
40 suggestions_array.obj(),
46 j_color_chooser_.Reset(Java_ColorChooserAndroid_createColorChooserAndroid(
48 reinterpret_cast<intptr_t>(this),
49 content_view_core->GetJavaObject().obj(),
51 suggestions_array.obj()));
54 ColorChooserAndroid::~ColorChooserAndroid() {
57 void ColorChooserAndroid::End() {
58 if (!j_color_chooser_.is_null()) {
59 JNIEnv* env = AttachCurrentThread();
60 Java_ColorChooserAndroid_closeColorChooser(env, j_color_chooser_.obj());
64 void ColorChooserAndroid::SetSelectedColor(SkColor color) {
65 // Not implemented since the color is set on the java side only, in theory
66 // it can be set from JS which would override the user selection but
67 // we don't support that for now.
70 void ColorChooserAndroid::OnColorChosen(JNIEnv* env, jobject obj, jint color) {
71 web_contents_->DidChooseColorInColorChooser(color);
72 web_contents_->DidEndColorChooser();
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
78 bool RegisterColorChooserAndroid(JNIEnv* env) {
79 return RegisterNativesImpl(env);
82 } // namespace web_contents_delegate_android