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