1 // Copyright 2015 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.
5 #include "chrome/browser/ui/extensions/hosted_app_browser_controller.h"
7 #include "base/command_line.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ssl/security_state_model.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/browser/ui/host_desktop.h"
13 #include "chrome/browser/ui/location_bar/location_bar.h"
14 #include "chrome/browser/ui/tabs/tab_strip_model.h"
15 #include "chrome/browser/web_applications/web_app.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
18 #include "content/public/browser/web_contents.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/common/extension.h"
21 #include "net/base/net_util.h"
24 namespace extensions
{
28 bool IsSameOriginOrMoreSecure(const GURL
& app_url
, const GURL
& page_url
) {
29 const std::string
www("www.");
30 return (app_url
.scheme() == page_url
.scheme() ||
31 page_url
.scheme() == url::kHttpsScheme
) &&
32 (app_url
.host() == page_url
.host() ||
33 www
+ app_url
.host() == page_url
.host()) &&
34 app_url
.port() == page_url
.port();
37 bool IsWebAppFrameEnabled() {
38 return base::CommandLine::ForCurrentProcess()->HasSwitch(
39 switches::kEnableWebAppFrame
);
45 bool HostedAppBrowserController::IsForHostedApp(Browser
* browser
) {
46 const std::string extension_id
=
47 web_app::GetExtensionIdFromApplicationName(browser
->app_name());
48 const Extension
* extension
=
49 ExtensionRegistry::Get(browser
->profile())->GetExtensionById(
50 extension_id
, ExtensionRegistry::EVERYTHING
);
51 return extension
&& extension
->is_hosted_app();
54 HostedAppBrowserController::HostedAppBrowserController(Browser
* browser
)
57 web_app::GetExtensionIdFromApplicationName(browser
->app_name())) {
58 const Extension
* extension
=
59 ExtensionRegistry::Get(browser
->profile())->GetExtensionById(
60 extension_id_
, ExtensionRegistry::EVERYTHING
);
62 should_use_web_app_frame_
=
63 extension
->from_bookmark() &&
64 browser
->host_desktop_type() == chrome::HOST_DESKTOP_TYPE_ASH
&&
65 IsWebAppFrameEnabled();
68 HostedAppBrowserController::~HostedAppBrowserController() {
71 bool HostedAppBrowserController::SupportsLocationBar() const {
72 return !should_use_web_app_frame();
75 bool HostedAppBrowserController::ShouldShowLocationBar() const {
76 const Extension
* extension
=
77 ExtensionRegistry::Get(browser_
->profile())->GetExtensionById(
78 extension_id_
, ExtensionRegistry::EVERYTHING
);
80 const content::WebContents
* web_contents
=
81 browser_
->tab_strip_model()->GetActiveWebContents();
83 // Default to not showing the location bar if either |extension| or
84 // |web_contents| are null. |extension| is null for the dev tools.
85 if (!extension
|| !web_contents
)
88 if (!extension
->is_hosted_app())
91 // Don't show a location bar until a navigation has occurred.
92 if (web_contents
->GetLastCommittedURL().is_empty())
95 const SecurityStateModel
* model
=
96 SecurityStateModel::FromWebContents(web_contents
);
98 model
->GetSecurityInfo().security_level
==
99 SecurityStateModel::SECURITY_ERROR
)
102 GURL launch_url
= AppLaunchInfo::GetLaunchWebURL(extension
);
103 return !(IsSameOriginOrMoreSecure(launch_url
,
104 web_contents
->GetVisibleURL()) &&
105 IsSameOriginOrMoreSecure(launch_url
,
106 web_contents
->GetLastCommittedURL()));
109 void HostedAppBrowserController::UpdateLocationBarVisibility(
110 bool animate
) const {
111 if (!SupportsLocationBar())
114 browser_
->window()->GetLocationBar()->UpdateLocationBarVisibility(
115 ShouldShowLocationBar(), animate
);
118 } // namespace extensions