Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ios / chrome / browser / infobars / infobar_manager_impl.cc
blob68902557f8d616e3bd3b2d948a6e3c095b6ca3b1
1 // Copyright 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 "ios/chrome/browser/infobars/infobar_manager_impl.h"
7 #include "components/infobars/core/confirm_infobar_delegate.h"
8 #include "components/infobars/core/infobar.h"
9 #include "components/infobars/core/infobar_delegate.h"
10 #include "ios/chrome/browser/infobars/infobar_utils.h"
11 #include "ios/web/public/load_committed_details.h"
12 #include "ios/web/public/navigation_item.h"
13 #include "ios/web/public/navigation_manager.h"
14 #include "ios/web/public/web_state/web_state.h"
15 #include "ui/base/page_transition_types.h"
17 DEFINE_WEB_STATE_USER_DATA_KEY(InfoBarManagerImpl);
19 namespace {
21 infobars::InfoBarDelegate::NavigationDetails
22 NavigationDetailsFromLoadCommittedDetails(
23 const web::LoadCommittedDetails& load_details) {
24 infobars::InfoBarDelegate::NavigationDetails navigation_details;
25 navigation_details.entry_id = load_details.item->GetUniqueID();
26 const ui::PageTransition transition = load_details.item->GetTransitionType();
27 navigation_details.is_navigation_to_different_page =
28 ui::PageTransitionIsMainFrame(transition) && !load_details.is_in_page;
29 // web::LoadCommittedDetails doesn't store this information, default to false.
30 navigation_details.did_replace_entry = false;
31 navigation_details.is_reload =
32 ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_RELOAD);
33 navigation_details.is_redirect = ui::PageTransitionIsRedirect(transition);
35 return navigation_details;
38 } // namespace
40 // static
41 web::WebState* InfoBarManagerImpl::WebStateFromInfoBar(
42 infobars::InfoBar* infobar) {
43 if (!infobar || !infobar->owner())
44 return nullptr;
45 return static_cast<InfoBarManagerImpl*>(infobar->owner())->web_state();
48 InfoBarManagerImpl::InfoBarManagerImpl(web::WebState* web_state)
49 : web::WebStateObserver(web_state) {
50 DCHECK(web_state);
53 InfoBarManagerImpl::~InfoBarManagerImpl() {
54 ShutDown();
57 int InfoBarManagerImpl::GetActiveEntryID() {
58 web::NavigationItem* visible_item =
59 web_state()->GetNavigationManager()->GetVisibleItem();
60 return visible_item ? visible_item->GetUniqueID() : 0;
63 scoped_ptr<infobars::InfoBar> InfoBarManagerImpl::CreateConfirmInfoBar(
64 scoped_ptr<ConfirmInfoBarDelegate> delegate) {
65 return ::CreateConfirmInfoBar(delegate.Pass());
68 void InfoBarManagerImpl::NavigationItemCommitted(
69 const web::LoadCommittedDetails& load_details) {
70 OnNavigation(NavigationDetailsFromLoadCommittedDetails(load_details));
73 void InfoBarManagerImpl::WebStateDestroyed() {
74 // The WebState is going away; be aggressively paranoid and delete this
75 // InfoBarManagerImpl lest other parts of the system attempt to add infobars
76 // or use it otherwise during the destruction.
77 web_state()->RemoveUserData(UserDataKey());
78 // That was the equivalent of "delete this". This object is now destroyed;
79 // returning from this function is the only safe thing to do.