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
,
29 views::View
* anchor_view
,
30 views::BubbleBorder::Arrow arrow
) {
31 scoped_ptr
<SettingsApiBubbleController
> settings_api_bubble(
32 new SettingsApiBubbleController(browser
, type
));
33 if (!settings_api_bubble
->ShouldShow())
36 SettingsApiBubbleController
* controller
= settings_api_bubble
.get();
37 ExtensionMessageBubbleView
* bubble_delegate
= new ExtensionMessageBubbleView(
38 anchor_view
, arrow
, settings_api_bubble
.Pass());
39 views::BubbleDelegateView::CreateBubble(bubble_delegate
);
40 controller
->Show(bubble_delegate
);
45 void MaybeShowExtensionControlledHomeNotification(Browser
* browser
) {
50 // The bubble will try to anchor itself against the home button
51 views::View
* anchor_view
= BrowserView::GetBrowserViewForBrowser(browser
)->
52 toolbar()->home_button();
53 ShowSettingsApiBubble(BUBBLE_TYPE_HOME_PAGE
,
56 views::BubbleBorder::TOP_LEFT
);
59 void MaybeShowExtensionControlledSearchNotification(
61 content::WebContents
* web_contents
,
62 const AutocompleteMatch
& match
) {
67 if (AutocompleteMatch::IsSearchType(match
.type
) &&
68 match
.type
!= AutocompleteMatchType::SEARCH_OTHER_ENGINE
) {
69 Browser
* browser
= chrome::FindBrowserWithWebContents(web_contents
);
70 ToolbarView
* toolbar
=
71 BrowserView::GetBrowserViewForBrowser(browser
)->toolbar();
72 ShowSettingsApiBubble(BUBBLE_TYPE_SEARCH_ENGINE
,
75 views::BubbleBorder::TOP_RIGHT
);
79 void MaybeShowExtensionControlledNewTabPage(
80 Browser
* browser
, content::WebContents
* web_contents
) {
85 content::NavigationEntry
* entry
=
86 web_contents
->GetController().GetActiveEntry();
89 GURL active_url
= entry
->GetURL();
90 if (!active_url
.SchemeIs("chrome-extension"))
91 return; // Not a URL that we care about.
93 // See if the current active URL matches a transformed NewTab URL.
94 GURL
ntp_url(chrome::kChromeUINewTabURL
);
96 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
98 web_contents
->GetBrowserContext(),
100 if (ntp_url
!= active_url
)
101 return; // Not being overridden by an extension.
103 scoped_ptr
<NtpOverriddenBubbleController
> ntp_overridden_bubble(
104 new NtpOverriddenBubbleController(browser
));
105 if (!ntp_overridden_bubble
->ShouldShow(ntp_url
.host()))
108 NtpOverriddenBubbleController
* controller
= ntp_overridden_bubble
.get();
109 ExtensionMessageBubbleView
* bubble_delegate
= new ExtensionMessageBubbleView(
110 BrowserView::GetBrowserViewForBrowser(browser
)->toolbar()->app_menu(),
111 views::BubbleBorder::TOP_RIGHT
,
112 ntp_overridden_bubble
.Pass());
113 views::BubbleDelegateView::CreateBubble(bubble_delegate
);
114 controller
->Show(bubble_delegate
);
117 } // namespace extensions