blob: 1c5dff3c07fe990c0ba4c95c9bbc45ffb2325c15 [file] [log] [blame]
Yining Wang4c6d1e02019-04-10 21:07:011// Copyright 2019 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/ui/manifest_web_app_browser_controller.h"
6
Jay Harris21836922019-06-11 23:33:327#include "chrome/browser/installable/installable_manager.h"
Yining Wang40eae8f2019-05-02 19:04:158#include "chrome/browser/profiles/profile.h"
Yining Wang40eae8f2019-05-02 19:04:159#include "chrome/browser/ssl/security_state_tab_helper.h"
Yining Wang4c6d1e02019-04-10 21:07:0110#include "chrome/browser/ui/browser.h"
11#include "content/public/browser/navigation_entry.h"
Lukasz Anforowicz60d1253d2019-05-08 16:31:3712#include "content/public/common/origin_util.h"
Yining Wang40eae8f2019-05-02 19:04:1513#include "content/public/common/url_constants.h"
14#include "extensions/common/constants.h"
Yining Wang4c6d1e02019-04-10 21:07:0115#include "ui/gfx/favicon_size.h"
16#include "ui/gfx/image/image_skia.h"
17#include "url/gurl.h"
18
19ManifestWebAppBrowserController::ManifestWebAppBrowserController(
20 Browser* browser)
Eric Willigers4a5f7a92019-05-10 19:19:2621 : AppBrowserController(browser), app_launch_url_(GURL()) {}
Yining Wang4c6d1e02019-04-10 21:07:0122
23ManifestWebAppBrowserController::~ManifestWebAppBrowserController() = default;
24
Yining Wang4c6d1e02019-04-10 21:07:0125base::Optional<std::string> ManifestWebAppBrowserController::GetAppId() const {
26 return base::nullopt;
27}
28
29bool ManifestWebAppBrowserController::ShouldShowToolbar() const {
Yining Wang40eae8f2019-05-02 19:04:1530 content::WebContents* web_contents =
31 browser()->tab_strip_model()->GetActiveWebContents();
32
33 // Don't show a toolbar until a navigation has occurred.
34 if (!web_contents || web_contents->GetLastCommittedURL().is_empty())
35 return false;
36
37 // Show toolbar if the web_contents is not on a secure origin.
Lukasz Anforowicz60d1253d2019-05-08 16:31:3738 if (!content::IsOriginSecure(app_launch_url_))
Yining Wang40eae8f2019-05-02 19:04:1539 return true;
Yining Wang40eae8f2019-05-02 19:04:1540
Jay Harris4e0a6c72019-07-08 23:29:0141 // Show toolbar if web_contents is not currently in scope.
42 if (!IsUrlInAppScope(web_contents->GetLastCommittedURL()) ||
43 !IsUrlInAppScope(web_contents->GetVisibleURL())) {
Yining Wang40eae8f2019-05-02 19:04:1544 return true;
45 }
46
47 // Show toolbar if on a insecure external website. This checks the security
48 // level, different from IsOriginSecure which just checks the origin itself.
Jay Harris21836922019-06-11 23:33:3249 if (!InstallableManager::IsContentSecure(web_contents))
Yining Wang40eae8f2019-05-02 19:04:1550 return true;
51
Yining Wang4c6d1e02019-04-10 21:07:0152 return false;
53}
54
55bool ManifestWebAppBrowserController::ShouldShowHostedAppButtonContainer()
56 const {
Yining Wang951b213a2019-04-16 23:22:0157 return true;
Yining Wang4c6d1e02019-04-10 21:07:0158}
59
60gfx::ImageSkia ManifestWebAppBrowserController::GetWindowAppIcon() const {
61 gfx::ImageSkia page_icon = browser()->GetCurrentPageIcon().AsImageSkia();
62 if (!page_icon.isNull())
63 return page_icon;
64
65 // The extension icon may be loading still. Return a transparent icon rather
66 // than using a placeholder to avoid flickering.
67 SkBitmap bitmap;
68 bitmap.allocN32Pixels(gfx::kFaviconSize, gfx::kFaviconSize);
69 bitmap.eraseColor(SK_ColorTRANSPARENT);
70 return gfx::ImageSkia::CreateFrom1xBitmap(bitmap);
71}
72
73gfx::ImageSkia ManifestWebAppBrowserController::GetWindowIcon() const {
74 return browser()->GetCurrentPageIcon().AsImageSkia();
75}
76
Yining Wang4c6d1e02019-04-10 21:07:0177std::string ManifestWebAppBrowserController::GetAppShortName() const {
78 return std::string();
79}
80
81base::string16 ManifestWebAppBrowserController::GetFormattedUrlOrigin() const {
82 return FormatUrlOrigin(GetAppLaunchURL());
83}
84
85GURL ManifestWebAppBrowserController::GetAppLaunchURL() const {
Yining Wang40eae8f2019-05-02 19:04:1586 return app_launch_url_;
87}
88
Jay Harris4e0a6c72019-07-08 23:29:0189bool ManifestWebAppBrowserController::IsUrlInAppScope(const GURL& url) const {
90 // TODO(981703): Use the scope in the manifest instead of same origin check.
91 return url::IsSameOriginWith(GetAppLaunchURL(), url);
92}
93
Yining Wang40eae8f2019-05-02 19:04:1594void ManifestWebAppBrowserController::OnTabInserted(
95 content::WebContents* contents) {
96 if (app_launch_url_.is_empty())
97 app_launch_url_ = contents->GetURL();
Eric Willigers4a5f7a92019-05-10 19:19:2698 AppBrowserController::OnTabInserted(contents);
Yining Wangc96d1932019-06-12 18:28:0099 UpdateToolbarVisibility(false);
Yining Wang4c6d1e02019-04-10 21:07:01100}