Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / ui / extensions / hosted_app_browser_controller.cc
blobf59a7fdfa128411c7f20f7e5a4de42462434c6eb
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"
22 #include "url/gurl.h"
24 namespace extensions {
26 namespace {
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);
42 } // namespace
44 // static
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)
55 : browser_(browser),
56 extension_id_(
57 web_app::GetExtensionIdFromApplicationName(browser->app_name())) {
58 const Extension* extension =
59 ExtensionRegistry::Get(browser->profile())->GetExtensionById(
60 extension_id_, ExtensionRegistry::EVERYTHING);
61 DCHECK(extension);
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 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)
86 return false;
88 if (!extension->is_hosted_app())
89 return false;
91 // Don't show a location bar until a navigation has occurred.
92 if (web_contents->GetLastCommittedURL().is_empty())
93 return false;
95 SecurityStateModel* model = SecurityStateModel::FromWebContents(web_contents);
96 if (model &&
97 model->security_info().security_level ==
98 SecurityStateModel::SECURITY_ERROR)
99 return true;
101 GURL launch_url = AppLaunchInfo::GetLaunchWebURL(extension);
102 return !(IsSameOriginOrMoreSecure(launch_url,
103 web_contents->GetVisibleURL()) &&
104 IsSameOriginOrMoreSecure(launch_url,
105 web_contents->GetLastCommittedURL()));
108 void HostedAppBrowserController::UpdateLocationBarVisibility(
109 bool animate) const {
110 if (!SupportsLocationBar())
111 return;
113 browser_->window()->GetLocationBar()->UpdateLocationBarVisibility(
114 ShouldShowLocationBar(), animate);
117 } // namespace extensions