Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / android / shell / java / src / org / chromium / chrome / shell / ChromeShellTab.java
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 package org.chromium.chrome.shell;
6
7 import android.content.Context;
8 import android.text.TextUtils;
9
10 import org.chromium.chrome.browser.Tab;
11 import org.chromium.chrome.browser.UrlUtilities;
12 import org.chromium.chrome.browser.contextmenu.ChromeContextMenuPopulator;
13 import org.chromium.chrome.browser.contextmenu.ContextMenuPopulator;
14 import org.chromium.chrome.browser.infobar.AutoLoginProcessor;
15 import org.chromium.content.browser.ContentViewClient;
16 import org.chromium.content_public.browser.LoadUrlParams;
17 import org.chromium.content_public.browser.NavigationController;
18 import org.chromium.content_public.browser.WebContents;
19 import org.chromium.content_public.common.Referrer;
20 import org.chromium.ui.base.WindowAndroid;
21
22 /**
23  * ChromeShell's implementation of a tab. This mirrors how Chrome for Android subclasses
24  * and extends {@link Tab}.
25  */
26 public class ChromeShellTab extends Tab {
27     // Tab state
28     private boolean mIsLoading;
29
30     /**
31      * @param context           The Context the view is running in.
32      * @param url               The URL to start this tab with.
33      * @param window            The WindowAndroid should represent this tab.
34      * @param contentViewClient The client for the {@link ContentViewCore}s of this Tab.
35      */
36     public ChromeShellTab(Context context, String url, WindowAndroid window,
37             ContentViewClient contentViewClient) {
38         super(false, context, window);
39         initialize();
40         initContentViewCore();
41         setContentViewClient(contentViewClient);
42         loadUrlWithSanitization(url);
43     }
44
45     /**
46      * @return Whether or not the tab is currently loading.
47      */
48     public boolean isLoading() {
49         return mIsLoading;
50     }
51
52     /**
53      * Navigates this Tab to a sanitized version of {@code url}.
54      * @param url The potentially unsanitized URL to navigate to.
55      * @param postData Optional data to be sent via POST.
56      */
57     public void loadUrlWithSanitization(String url, byte[] postData) {
58         if (url == null) return;
59
60         // Sanitize the URL.
61         url = UrlUtilities.fixupUrl(url);
62
63         // Invalid URLs will just return empty.
64         if (TextUtils.isEmpty(url)) return;
65
66         WebContents webContents = getWebContents();
67         NavigationController navigationController = webContents.getNavigationController();
68         if (TextUtils.equals(url, webContents.getUrl())) {
69             navigationController.reload(true);
70         } else {
71             if (postData == null) {
72                 navigationController.loadUrl(new LoadUrlParams(url));
73             } else {
74                 navigationController.loadUrl(LoadUrlParams.createLoadHttpPostParams(url, postData));
75             }
76         }
77     }
78
79     /**
80      * Navigates this Tab to a sanitized version of {@code url}.
81      * @param url The potentially unsanitized URL to navigate to.
82      */
83     public void loadUrlWithSanitization(String url) {
84         loadUrlWithSanitization(url, null);
85     }
86
87     @Override
88     protected TabChromeWebContentsDelegateAndroid createWebContentsDelegate() {
89         return new ChromeShellTabChromeWebContentsDelegateAndroid();
90     }
91
92     @Override
93     protected AutoLoginProcessor createAutoLoginProcessor() {
94         return new AutoLoginProcessor() {
95             @Override
96             public void processAutoLoginResult(String accountName,
97                     String authToken, boolean success, String result) {
98                 getInfoBarContainer().processAutoLogin(accountName, authToken,
99                         success, result);
100             }
101         };
102     }
103
104     @Override
105     protected ContextMenuPopulator createContextMenuPopulator() {
106         return new ChromeContextMenuPopulator(new TabChromeContextMenuItemDelegate() {
107             @Override
108             public void onOpenImageUrl(String url, Referrer referrer) {
109                 loadUrlWithSanitization(url);
110             }
111         });
112     }
113
114     private class ChromeShellTabChromeWebContentsDelegateAndroid
115             extends TabChromeWebContentsDelegateAndroid {
116         @Override
117         public void onLoadStarted() {
118             mIsLoading = true;
119         }
120
121         @Override
122         public void onLoadStopped() {
123             mIsLoading = false;
124         }
125     }
126 }