OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 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. | |
4 | |
5 #include "chrome/browser/extensions/sidebar_manager.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/command_line.h" | |
10 #include "chrome/browser/browser_process.h" | |
11 #include "chrome/browser/chrome_notification_types.h" | |
12 #include "chrome/browser/extensions/extension_service.h" | |
13 #include "chrome/browser/extensions/sidebar_container.h" | |
14 #include "chrome/browser/extensions/sidebar_manager_observer.h" | |
15 #include "content/public/browser/notification_service.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 #include "extensions/browser/extension_system.h" | |
18 #include "extensions/common/switches.h" | |
19 #include "url/gurl.h" | |
20 | |
21 using content::BrowserContext; | |
22 using content::WebContents; | |
23 | |
24 namespace extensions { | |
25 // static | |
26 SidebarManager* SidebarManager::GetFromContext(BrowserContext* context) { | |
27 return ExtensionSystem::Get(context)->sidebar_manager(); | |
28 } | |
29 | |
30 SidebarManager::SidebarManager() { | |
31 } | |
32 | |
33 SidebarContainer* SidebarManager::GetSidebarContainerFor( | |
34 content::WebContents* tab) { | |
35 TabToSidebarContainerMap::iterator it = tab_to_sidebar_container_.find(tab); | |
36 if (it == tab_to_sidebar_container_.end()) | |
37 return nullptr; | |
38 return it->second; | |
39 } | |
40 | |
41 void SidebarManager::NotifyStateChanges( | |
42 content::WebContents* was_active_sidebar_contents, | |
43 content::WebContents* active_sidebar_contents) { | |
44 if (was_active_sidebar_contents == active_sidebar_contents) | |
45 return; | |
46 | |
47 SidebarContainer* was_active_container = | |
48 was_active_sidebar_contents == nullptr | |
49 ? nullptr | |
50 : FindSidebarContainerFor(was_active_sidebar_contents); | |
51 SidebarContainer* active_container = | |
52 active_sidebar_contents == nullptr | |
53 ? nullptr | |
54 : FindSidebarContainerFor(active_sidebar_contents); | |
55 | |
56 content::WebContents* old_tab = was_active_container == nullptr | |
57 ? nullptr | |
58 : was_active_container->web_contents(); | |
59 content::WebContents* new_tab = | |
60 active_container == nullptr ? nullptr : active_container->web_contents(); | |
61 const std::string& old_content_id = | |
62 was_active_container == nullptr ? "" | |
63 : was_active_container->extension_id(); | |
64 const std::string& new_content_id = | |
65 active_container == nullptr ? "" : active_container->extension_id(); | |
66 | |
67 FOR_EACH_OBSERVER( | |
68 SidebarManagerObserver, observer_list_, | |
69 OnSidebarSwitched(old_tab, old_content_id, new_tab, new_content_id)); | |
70 } | |
71 | |
72 void SidebarManager::CreateSidebar(content::WebContents* tab, | |
73 const GURL& url, | |
74 Browser* browser) { | |
75 SidebarContainer* container = GetSidebarContainerFor(tab); | |
76 if (container) | |
77 HideSidebar(tab); | |
78 | |
79 container = new SidebarContainer(browser, tab, url); | |
80 BindSidebarContainer(tab, container); | |
81 | |
82 const std::string id = container->extension_id(); | |
83 FOR_EACH_OBSERVER(SidebarManagerObserver, observer_list_, | |
84 OnSidebarShown(tab, id)); | |
85 } | |
86 | |
87 void SidebarManager::HideSidebar(WebContents* tab) { | |
88 SidebarContainer* container = GetSidebarContainerFor(tab); | |
89 if (!container) | |
90 return; | |
91 | |
92 const std::string content_id = container->extension_id(); | |
93 UnbindSidebarContainer(tab, container); | |
94 delete container; | |
95 | |
96 FOR_EACH_OBSERVER(SidebarManagerObserver, observer_list_, | |
97 OnSidebarHidden(tab, content_id)); | |
98 } | |
99 | |
100 bool SidebarManager::HasSidebar(WebContents* tab) { | |
101 return GetSidebarContainerFor(tab) != nullptr; | |
102 } | |
103 | |
104 SidebarManager::~SidebarManager() { | |
105 DCHECK(tab_to_sidebar_container_.empty()); | |
106 DCHECK(sidebar_container_to_tab_.empty()); | |
107 } | |
108 | |
109 void SidebarManager::Observe(int type, | |
Devlin
2015/06/16 17:56:12
This should be accomplished without notifications.
| |
110 const content::NotificationSource& source, | |
111 const content::NotificationDetails& details) { | |
112 if (type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED) { | |
113 HideSidebar(content::Source<WebContents>(source).ptr()); | |
114 } else { | |
115 NOTREACHED() << "Got a notification we didn't register for!"; | |
116 } | |
117 } | |
118 | |
119 SidebarContainer* SidebarManager::FindSidebarContainerFor( | |
120 content::WebContents* sidebar_contents) { | |
121 for (SidebarContainerToTabMap::iterator it = | |
122 sidebar_container_to_tab_.begin(); | |
123 it != sidebar_container_to_tab_.end(); ++it) { | |
124 if (sidebar_contents == it->first->host_contents()) | |
125 return it->first; | |
126 } | |
127 return nullptr; | |
128 } | |
129 | |
130 void SidebarManager::BindSidebarContainer(WebContents* tab, | |
131 SidebarContainer* container) { | |
132 tab_to_sidebar_container_[tab] = container; | |
133 sidebar_container_to_tab_[container] = tab; | |
134 registrar_.Add(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
135 content::Source<WebContents>(tab)); | |
136 } | |
137 | |
138 void SidebarManager::UnbindSidebarContainer(WebContents* tab, | |
139 SidebarContainer* container) { | |
140 tab_to_sidebar_container_.erase(tab); | |
141 sidebar_container_to_tab_.erase(container); | |
142 registrar_.Remove(this, content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
143 content::Source<WebContents>(tab)); | |
144 } | |
145 | |
146 void SidebarManager::AddObserver(SidebarManagerObserver* observer) { | |
147 observer_list_.AddObserver(observer); | |
148 } | |
149 | |
150 void SidebarManager::RemoveObserver(SidebarManagerObserver* observer) { | |
151 observer_list_.RemoveObserver(observer); | |
152 } | |
153 | |
154 } // namespace extensions | |
OLD | NEW |