1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/dom_distiller/core/dom_distiller_service_android.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/android/jni_android.h"
86e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/android/jni_string.h"
96e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/android/scoped_java_ref.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/dom_distiller/core/distilled_page_prefs.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/dom_distiller/core/distilled_page_prefs_android.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/dom_distiller/core/dom_distiller_service.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "jni/DomDistillerService_jni.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)using base::android::ConvertUTF8ToJavaString;
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)using base::android::ScopedJavaLocalRef;
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace dom_distiller {
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace android {
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
21116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDomDistillerServiceAndroid::DomDistillerServiceAndroid(
22116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    DomDistillerService* service)
23116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : service_(service) {
24116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  JNIEnv* env = base::android::AttachCurrentThread();
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  base::android::ScopedJavaLocalRef<jobject> local_java_ref =
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      Java_DomDistillerService_create(env, reinterpret_cast<intptr_t>(this));
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  java_ref_.Reset(env, local_java_ref.obj());
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
30116680a4aac90f2aa7413d9095a592090648e557Ben MurdochDomDistillerServiceAndroid::~DomDistillerServiceAndroid() {
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)bool DomDistillerServiceAndroid::HasEntry(JNIEnv* env,
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                          jobject obj,
356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                          jstring j_entry_id) {
366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  const std::string entry_id =
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      base::android::ConvertJavaStringToUTF8(env, j_entry_id);
386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return service_->HasEntry(entry_id);
396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)ScopedJavaLocalRef<jstring> DomDistillerServiceAndroid::GetUrlForEntry(
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    JNIEnv* env,
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    jobject obj,
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    jstring j_entry_id) {
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  const std::string entry_id =
466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      base::android::ConvertJavaStringToUTF8(env, j_entry_id);
476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return ConvertUTF8ToJavaString(env, service_->GetUrlForEntry(entry_id));
486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdochjlong DomDistillerServiceAndroid::GetDistilledPagePrefsPtr(JNIEnv* env,
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                                           jobject obj) {
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return reinterpret_cast<intptr_t>(service_->GetDistilledPagePrefs());
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool DomDistillerServiceAndroid::Register(JNIEnv* env) {
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return RegisterNativesImpl(env);
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace android
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace dom_distiller
61