~matzipan/capnet-assist/fix-on-no-network

« back to all changes in this revision

Viewing changes to src/TabbedWebView.vala

  • Committer: RabbitBot
  • Author(s): felescoto95 at hotmail
  • Date: 2016-12-06 21:05:49 UTC
  • mfrom: (118.1.1 tabs-n-downloads)
  • Revision ID: rabbitbot-20161206210549-1tflhab0du73kivz
- Pass Downloads to a web browser
- New Window & tab requests are put into their own tabs

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* Copyright (c) 2016 elementary LLC (http://launchpad.net/capnet-assist)
 
3
*
 
4
* This program is free software; you can redistribute it and/or
 
5
* modify it under the terms of the GNU General Public
 
6
* License as published by the Free Software Foundation; either
 
7
* version 2 of the License, or (at your option) any later version.
 
8
*
 
9
* This program is distributed in the hope that it will be useful,
 
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
* General Public License for more details.
 
13
*
 
14
* You should have received a copy of the GNU General Public
 
15
* License along with this program; if not, write to the
 
16
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
* Boston, MA 02111-1307, USA.
 
18
*
 
19
*/
 
20
 
 
21
public class TabbedWebView : Granite.Widgets.Tab {
 
22
    public WebKit.WebView web_view;
 
23
    public CertButton.Security security { get; private set; }
 
24
 
 
25
    public TabbedWebView (string uri, bool load_cookies) {
 
26
        web_view = new WebKit.WebView ();
 
27
 
 
28
        page = web_view;
 
29
 
 
30
        setup_web_view (load_cookies);
 
31
 
 
32
        web_view.insecure_content_detected.connect (() => {
 
33
            security = CertButton.Security.MIXED_CONTENT;
 
34
        });
 
35
 
 
36
        web_view.notify["title"].connect ((view, param_spec) => {
 
37
            label = web_view.get_title ();
 
38
        });
 
39
 
 
40
        web_view.load_changed.connect ((view, event) => {
 
41
            switch (event) {
 
42
                case WebKit.LoadEvent.STARTED:
 
43
                    security = CertButton.Security.LOADING;
 
44
                    break;
 
45
                case WebKit.LoadEvent.COMMITTED:
 
46
                    update_tls_info ();
 
47
                    break;
 
48
            }
 
49
        });
 
50
 
 
51
        web_view.load_uri (uri);
 
52
    }
 
53
 
 
54
    private void update_tls_info () {
 
55
        TlsCertificate cert;
 
56
        TlsCertificateFlags cert_flags;
 
57
        bool is_secure;
 
58
 
 
59
        if (!web_view.get_tls_info (out cert, out cert_flags)) {
 
60
            // The page is served over HTTP
 
61
            is_secure = false;
 
62
        } else {
 
63
            // The page is served over HTTPS, if cert_flags is set then there's
 
64
            // some problem with the certificate provided by the website.
 
65
            is_secure = (cert_flags == 0);
 
66
        }
 
67
 
 
68
        if (is_secure) {
 
69
            security = CertButton.Security.SECURE;
 
70
        } else {
 
71
            security = CertButton.Security.NONE;
 
72
        }
 
73
    }
 
74
 
 
75
    private void setup_web_view (bool load_cookies) {
 
76
        if (load_cookies) {
 
77
            var cookies_db_path = Path.build_path (Path.DIR_SEPARATOR_S,
 
78
                                                   Environment.get_user_config_dir (),
 
79
                                                   "epiphany",
 
80
                                                   "cookies.sqlite");
 
81
 
 
82
            if (!FileUtils.test (cookies_db_path, FileTest.IS_REGULAR)) {
 
83
                debug ("No cookies store found, not saving the cookies...\n");
 
84
                return;
 
85
            }
 
86
 
 
87
            var cookie_manager = web_view.get_context ().get_cookie_manager ();
 
88
 
 
89
            cookie_manager.set_accept_policy (WebKit.CookieAcceptPolicy.ALWAYS);
 
90
            cookie_manager.set_persistent_storage (cookies_db_path, WebKit.CookiePersistentStorage.SQLITE);
 
91
        }
 
92
    }
 
93
}