1 // Copyright (c) 2014 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/views/settings_api_bubble_helper_views.h"
7 #include "chrome/browser/extensions/ntp_overridden_bubble_controller.h"
8 #include "chrome/browser/extensions/settings_api_bubble_controller.h"
9 #include "chrome/browser/extensions/settings_api_helpers.h"
10 #include "chrome/browser/ui/browser_finder.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/browser/ui/views/extensions/extension_message_bubble_view.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/settings_api_bubble_helper_views.h"
15 #include "chrome/browser/ui/views/toolbar/home_button.h"
16 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
17 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.h"
18 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/browser_url_handler.h"
20 #include "content/public/browser/navigation_entry.h"
22 namespace extensions
{
26 void ShowSettingsApiBubble(SettingsApiOverrideType type
,
27 const std::string
& extension_id
,
29 views::View
* anchor_view
,
30 views::BubbleBorder::Arrow arrow
) {
31 scoped_ptr
<SettingsApiBubbleController
> settings_api_bubble(
32 new SettingsApiBubbleController(profile
, type
));
33 if (!settings_api_bubble
->ShouldShow(extension_id
))
36 SettingsApiBubbleController
* controller
= settings_api_bubble
.get();
37 ExtensionMessageBubbleView
* bubble_delegate
=
38 new ExtensionMessageBubbleView(
41 settings_api_bubble
.PassAs
<
42 ExtensionMessageBubbleController
>());
43 views::BubbleDelegateView::CreateBubble(bubble_delegate
);
44 controller
->Show(bubble_delegate
);
49 void MaybeShowExtensionControlledHomeNotification(Browser
* browser
) {
54 const Extension
* extension
=
55 GetExtensionOverridingHomepage(browser
->profile(), NULL
);
57 // The bubble will try to anchor itself against the home button
58 views::View
* anchor_view
= BrowserView::GetBrowserViewForBrowser(browser
)->
59 toolbar()->home_button();
60 ShowSettingsApiBubble(BUBBLE_TYPE_HOME_PAGE
,
64 views::BubbleBorder::TOP_LEFT
);
68 void MaybeShowExtensionControlledSearchNotification(
70 content::WebContents
* web_contents
,
71 const AutocompleteMatch
& match
) {
76 if (AutocompleteMatch::IsSearchType(match
.type
) &&
77 match
.type
!= AutocompleteMatchType::SEARCH_OTHER_ENGINE
) {
78 const Extension
* extension
=
79 GetExtensionOverridingSearchEngine(profile
, NULL
);
81 ToolbarView
* toolbar
=
82 BrowserView::GetBrowserViewForBrowser(
83 chrome::FindBrowserWithWebContents(web_contents
))->toolbar();
84 ShowSettingsApiBubble(BUBBLE_TYPE_SEARCH_ENGINE
,
88 views::BubbleBorder::TOP_RIGHT
);
93 void MaybeShowExtensionControlledNewTabPage(
94 Browser
* browser
, content::WebContents
* web_contents
) {
99 content::NavigationEntry
* entry
=
100 web_contents
->GetController().GetActiveEntry();
103 GURL active_url
= entry
->GetURL();
104 if (!active_url
.SchemeIs("chrome-extension"))
105 return; // Not a URL that we care about.
107 // See if the current active URL matches a transformed NewTab URL.
108 GURL
ntp_url(chrome::kChromeUINewTabURL
);
110 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
112 web_contents
->GetBrowserContext(),
114 if (ntp_url
!= active_url
)
115 return; // Not being overridden by an extension.
117 scoped_ptr
<NtpOverriddenBubbleController
> ntp_overridden_bubble(
118 new NtpOverriddenBubbleController(browser
->profile()));
119 if (!ntp_overridden_bubble
->ShouldShow(ntp_url
.host()))
122 NtpOverriddenBubbleController
* controller
= ntp_overridden_bubble
.get();
123 ExtensionMessageBubbleView
* bubble_delegate
=
124 new ExtensionMessageBubbleView(
125 BrowserView::GetBrowserViewForBrowser(browser
)->toolbar()->app_menu(),
126 views::BubbleBorder::TOP_RIGHT
,
127 ntp_overridden_bubble
.PassAs
<
128 ExtensionMessageBubbleController
>());
129 views::BubbleDelegateView::CreateBubble(bubble_delegate
);
130 controller
->Show(bubble_delegate
);
133 } // namespace extensions