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/browser/ui/views/toolbar/wrench_toolbar_button.h"
18 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.h"
19 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/browser_url_handler.h"
21 #include "content/public/browser/navigation_entry.h"
23 namespace extensions
{
27 void ShowSettingsApiBubble(SettingsApiOverrideType type
,
28 const std::string
& extension_id
,
30 views::View
* anchor_view
,
31 views::BubbleBorder::Arrow arrow
) {
32 scoped_ptr
<SettingsApiBubbleController
> settings_api_bubble(
33 new SettingsApiBubbleController(profile
, type
));
34 if (!settings_api_bubble
->ShouldShow(extension_id
))
37 SettingsApiBubbleController
* controller
= settings_api_bubble
.get();
38 ExtensionMessageBubbleView
* bubble_delegate
= new ExtensionMessageBubbleView(
39 anchor_view
, arrow
, settings_api_bubble
.Pass());
40 views::BubbleDelegateView::CreateBubble(bubble_delegate
);
41 controller
->Show(bubble_delegate
);
46 void MaybeShowExtensionControlledHomeNotification(Browser
* browser
) {
51 const Extension
* extension
=
52 GetExtensionOverridingHomepage(browser
->profile());
54 // The bubble will try to anchor itself against the home button
55 views::View
* anchor_view
= BrowserView::GetBrowserViewForBrowser(browser
)->
56 toolbar()->home_button();
57 ShowSettingsApiBubble(BUBBLE_TYPE_HOME_PAGE
,
61 views::BubbleBorder::TOP_LEFT
);
65 void MaybeShowExtensionControlledSearchNotification(
67 content::WebContents
* web_contents
,
68 const AutocompleteMatch
& match
) {
73 if (AutocompleteMatch::IsSearchType(match
.type
) &&
74 match
.type
!= AutocompleteMatchType::SEARCH_OTHER_ENGINE
) {
75 const Extension
* extension
= GetExtensionOverridingSearchEngine(profile
);
77 ToolbarView
* toolbar
=
78 BrowserView::GetBrowserViewForBrowser(
79 chrome::FindBrowserWithWebContents(web_contents
))->toolbar();
80 ShowSettingsApiBubble(BUBBLE_TYPE_SEARCH_ENGINE
,
84 views::BubbleBorder::TOP_RIGHT
);
89 void MaybeShowExtensionControlledNewTabPage(
90 Browser
* browser
, content::WebContents
* web_contents
) {
95 content::NavigationEntry
* entry
=
96 web_contents
->GetController().GetActiveEntry();
99 GURL active_url
= entry
->GetURL();
100 if (!active_url
.SchemeIs("chrome-extension"))
101 return; // Not a URL that we care about.
103 // See if the current active URL matches a transformed NewTab URL.
104 GURL
ntp_url(chrome::kChromeUINewTabURL
);
106 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
108 web_contents
->GetBrowserContext(),
110 if (ntp_url
!= active_url
)
111 return; // Not being overridden by an extension.
113 scoped_ptr
<NtpOverriddenBubbleController
> ntp_overridden_bubble(
114 new NtpOverriddenBubbleController(browser
->profile()));
115 if (!ntp_overridden_bubble
->ShouldShow(ntp_url
.host()))
118 NtpOverriddenBubbleController
* controller
= ntp_overridden_bubble
.get();
119 ExtensionMessageBubbleView
* bubble_delegate
= new ExtensionMessageBubbleView(
120 BrowserView::GetBrowserViewForBrowser(browser
)->toolbar()->app_menu(),
121 views::BubbleBorder::TOP_RIGHT
,
122 ntp_overridden_bubble
.Pass());
123 views::BubbleDelegateView::CreateBubble(bubble_delegate
);
124 controller
->Show(bubble_delegate
);
127 } // namespace extensions