• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 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/dom_distiller/tab_utils_android.h"
6 
7 #include <string>
8 
9 #include "base/android/jni_string.h"
10 #include "base/prefs/pref_service.h"
11 #include "chrome/browser/dom_distiller/tab_utils.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/pref_names.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/content_constants.h"
17 #include "jni/DomDistillerTabUtils_jni.h"
18 #include "net/base/net_util.h"
19 #include "url/gurl.h"
20 
21 namespace android {
22 
DistillCurrentPageAndView(JNIEnv * env,jclass clazz,jobject j_web_contents)23 void DistillCurrentPageAndView(JNIEnv* env,
24                                jclass clazz,
25                                jobject j_web_contents) {
26   content::WebContents* web_contents =
27       content::WebContents::FromJavaWebContents(j_web_contents);
28   ::DistillCurrentPageAndView(web_contents);
29 }
30 
GetFormattedUrlFromOriginalDistillerUrl(JNIEnv * env,jclass clazz,jstring j_url)31 jstring GetFormattedUrlFromOriginalDistillerUrl(JNIEnv* env,
32                                                 jclass clazz,
33                                                 jstring j_url) {
34   GURL url(base::android::ConvertJavaStringToUTF8(env, j_url));
35   Profile* profile = ProfileManager::GetLastUsedProfile();
36   std::string languages;  // Empty if Profile cannot be retrieved.
37   if (profile) {
38     languages = profile->GetPrefs()->GetString(prefs::kAcceptLanguages);
39   }
40 
41   if (url.spec().length() > content::kMaxURLDisplayChars)
42     url = url.IsStandard() ? url.GetOrigin() : GURL(url.scheme() + ":");
43 
44   // Note that we can't unescape spaces here, because if the user copies this
45   // and pastes it into another program, that program may think the URL ends at
46   // the space.
47   return base::android::ConvertUTF16ToJavaString(
48              env,
49              net::FormatUrl(url,
50                             languages,
51                             net::kFormatUrlOmitAll,
52                             net::UnescapeRule::NORMAL,
53                             NULL,
54                             NULL,
55                             NULL)).Release();
56 }
57 
58 }  // namespace android
59 
RegisterDomDistillerTabUtils(JNIEnv * env)60 bool RegisterDomDistillerTabUtils(JNIEnv* env) {
61   return android::RegisterNativesImpl(env);
62 }
63