Yining Wang | 4c6d1e0 | 2019-04-10 21:07:01 | [diff] [blame] | 1 | // 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 Harris | 2183692 | 2019-06-11 23:33:32 | [diff] [blame] | 7 | #include "chrome/browser/installable/installable_manager.h" |
Yining Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 8 | #include "chrome/browser/profiles/profile.h" |
Yining Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 9 | #include "chrome/browser/ssl/security_state_tab_helper.h" |
Yining Wang | 4c6d1e0 | 2019-04-10 21:07:01 | [diff] [blame] | 10 | #include "chrome/browser/ui/browser.h" |
| 11 | #include "content/public/browser/navigation_entry.h" |
Lukasz Anforowicz | 60d1253d | 2019-05-08 16:31:37 | [diff] [blame] | 12 | #include "content/public/common/origin_util.h" |
Yining Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 13 | #include "content/public/common/url_constants.h" |
| 14 | #include "extensions/common/constants.h" |
Yining Wang | 4c6d1e0 | 2019-04-10 21:07:01 | [diff] [blame] | 15 | #include "ui/gfx/favicon_size.h" |
| 16 | #include "ui/gfx/image/image_skia.h" |
| 17 | #include "url/gurl.h" |
| 18 | |
| 19 | ManifestWebAppBrowserController::ManifestWebAppBrowserController( |
| 20 | Browser* browser) |
Eric Willigers | 4a5f7a9 | 2019-05-10 19:19:26 | [diff] [blame] | 21 | : AppBrowserController(browser), app_launch_url_(GURL()) {} |
Yining Wang | 4c6d1e0 | 2019-04-10 21:07:01 | [diff] [blame] | 22 | |
| 23 | ManifestWebAppBrowserController::~ManifestWebAppBrowserController() = default; |
| 24 | |
Yining Wang | 4c6d1e0 | 2019-04-10 21:07:01 | [diff] [blame] | 25 | base::Optional<std::string> ManifestWebAppBrowserController::GetAppId() const { |
| 26 | return base::nullopt; |
| 27 | } |
| 28 | |
| 29 | bool ManifestWebAppBrowserController::ShouldShowToolbar() const { |
Yining Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 30 | 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 Anforowicz | 60d1253d | 2019-05-08 16:31:37 | [diff] [blame] | 38 | if (!content::IsOriginSecure(app_launch_url_)) |
Yining Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 39 | return true; |
Yining Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 40 | |
Jay Harris | 4e0a6c7 | 2019-07-08 23:29:01 | [diff] [blame^] | 41 | // Show toolbar if web_contents is not currently in scope. |
| 42 | if (!IsUrlInAppScope(web_contents->GetLastCommittedURL()) || |
| 43 | !IsUrlInAppScope(web_contents->GetVisibleURL())) { |
Yining Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 44 | 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 Harris | 2183692 | 2019-06-11 23:33:32 | [diff] [blame] | 49 | if (!InstallableManager::IsContentSecure(web_contents)) |
Yining Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 50 | return true; |
| 51 | |
Yining Wang | 4c6d1e0 | 2019-04-10 21:07:01 | [diff] [blame] | 52 | return false; |
| 53 | } |
| 54 | |
| 55 | bool ManifestWebAppBrowserController::ShouldShowHostedAppButtonContainer() |
| 56 | const { |
Yining Wang | 951b213a | 2019-04-16 23:22:01 | [diff] [blame] | 57 | return true; |
Yining Wang | 4c6d1e0 | 2019-04-10 21:07:01 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | gfx::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 | |
| 73 | gfx::ImageSkia ManifestWebAppBrowserController::GetWindowIcon() const { |
| 74 | return browser()->GetCurrentPageIcon().AsImageSkia(); |
| 75 | } |
| 76 | |
Yining Wang | 4c6d1e0 | 2019-04-10 21:07:01 | [diff] [blame] | 77 | std::string ManifestWebAppBrowserController::GetAppShortName() const { |
| 78 | return std::string(); |
| 79 | } |
| 80 | |
| 81 | base::string16 ManifestWebAppBrowserController::GetFormattedUrlOrigin() const { |
| 82 | return FormatUrlOrigin(GetAppLaunchURL()); |
| 83 | } |
| 84 | |
| 85 | GURL ManifestWebAppBrowserController::GetAppLaunchURL() const { |
Yining Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 86 | return app_launch_url_; |
| 87 | } |
| 88 | |
Jay Harris | 4e0a6c7 | 2019-07-08 23:29:01 | [diff] [blame^] | 89 | bool 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 Wang | 40eae8f | 2019-05-02 19:04:15 | [diff] [blame] | 94 | void ManifestWebAppBrowserController::OnTabInserted( |
| 95 | content::WebContents* contents) { |
| 96 | if (app_launch_url_.is_empty()) |
| 97 | app_launch_url_ = contents->GetURL(); |
Eric Willigers | 4a5f7a9 | 2019-05-10 19:19:26 | [diff] [blame] | 98 | AppBrowserController::OnTabInserted(contents); |
Yining Wang | c96d193 | 2019-06-12 18:28:00 | [diff] [blame] | 99 | UpdateToolbarVisibility(false); |
Yining Wang | 4c6d1e0 | 2019-04-10 21:07:01 | [diff] [blame] | 100 | } |