Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(200)

Side by Side Diff: chrome/browser/extensions/extension_toolbar_model.cc

Issue 10533086: Action box menu (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Action box menu Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/extension_toolbar_model.h" 5 #include "chrome/browser/extensions/extension_toolbar_model.h"
6 6
7 #include "chrome/browser/extensions/extension_browser_event_router.h" 7 #include "chrome/browser/extensions/extension_browser_event_router.h"
8 #include "chrome/browser/extensions/extension_prefs.h" 8 #include "chrome/browser/extensions/extension_prefs.h"
9 #include "chrome/browser/extensions/extension_service.h" 9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/tab_helper.h" 10 #include "chrome/browser/extensions/tab_helper.h"
11 #include "chrome/browser/extensions/extension_tab_util.h" 11 #include "chrome/browser/extensions/extension_tab_util.h"
12 #include "chrome/browser/prefs/pref_service.h" 12 #include "chrome/browser/prefs/pref_service.h"
13 #include "chrome/browser/profiles/profile.h" 13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/browser.h" 14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_tabstrip.h" 15 #include "chrome/browser/ui/browser_tabstrip.h"
16 #include "chrome/browser/ui/tab_contents/tab_contents.h" 16 #include "chrome/browser/ui/tab_contents/tab_contents.h"
17 #include "chrome/common/chrome_notification_types.h" 17 #include "chrome/common/chrome_notification_types.h"
18 #include "chrome/common/extensions/extension.h" 18 #include "chrome/common/extensions/extension.h"
19 #include "chrome/common/extensions/extension_switch_utils.h"
19 #include "chrome/common/pref_names.h" 20 #include "chrome/common/pref_names.h"
20 #include "content/public/browser/notification_details.h" 21 #include "content/public/browser/notification_details.h"
21 #include "content/public/browser/notification_source.h" 22 #include "content/public/browser/notification_source.h"
22 #include "content/public/browser/web_contents.h" 23 #include "content/public/browser/web_contents.h"
23 24
24 using extensions::Extension; 25 using extensions::Extension;
25 using extensions::ExtensionList; 26 using extensions::ExtensionList;
26 27
28 namespace {
29
30 // Returns true if an |extension| is in an |extension_list|.
31 bool IsInExtensionList(const Extension* extension,
32 const extensions::ExtensionList& extension_list) {
33 for (size_t i = 0; i < extension_list.size(); i++) {
34 if (extension_list[i].get() == extension)
35 return true;
36 }
37 return false;
38 }
39
40 } // namespace
41
27 ExtensionToolbarModel::ExtensionToolbarModel(ExtensionService* service) 42 ExtensionToolbarModel::ExtensionToolbarModel(ExtensionService* service)
28 : service_(service), 43 : service_(service),
29 prefs_(service->profile()->GetPrefs()), 44 prefs_(service->profile()->GetPrefs()),
30 extensions_initialized_(false) { 45 extensions_initialized_(false) {
31 DCHECK(service_); 46 DCHECK(service_);
32 47
33 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, 48 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED,
34 content::Source<Profile>(service_->profile())); 49 content::Source<Profile>(service_->profile()));
35 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, 50 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
36 content::Source<Profile>(service_->profile())); 51 content::Source<Profile>(service_->profile()));
(...skipping 17 matching lines...) Expand all
54 observers_.RemoveObserver(observer); 69 observers_.RemoveObserver(observer);
55 } 70 }
56 71
57 void ExtensionToolbarModel::MoveBrowserAction(const Extension* extension, 72 void ExtensionToolbarModel::MoveBrowserAction(const Extension* extension,
58 int index) { 73 int index) {
59 ExtensionList::iterator pos = std::find(begin(), end(), extension); 74 ExtensionList::iterator pos = std::find(begin(), end(), extension);
60 if (pos == end()) { 75 if (pos == end()) {
61 NOTREACHED(); 76 NOTREACHED();
62 return; 77 return;
63 } 78 }
64 toolitems_.erase(pos); 79 toolbar_items_.erase(pos);
65 80
66 int i = 0; 81 int i = 0;
67 bool inserted = false; 82 bool inserted = false;
68 for (ExtensionList::iterator iter = begin(); iter != end(); ++iter, ++i) { 83 for (ExtensionList::iterator iter = begin(); iter != end(); ++iter, ++i) {
69 if (i == index) { 84 if (i == index) {
70 toolitems_.insert(iter, make_scoped_refptr(extension)); 85 toolbar_items_.insert(iter, make_scoped_refptr(extension));
71 inserted = true; 86 inserted = true;
72 break; 87 break;
73 } 88 }
74 } 89 }
75 90
76 if (!inserted) { 91 if (!inserted) {
77 DCHECK_EQ(index, static_cast<int>(toolitems_.size())); 92 DCHECK_EQ(index, static_cast<int>(toolbar_items_.size()));
78 index = toolitems_.size(); 93 index = toolbar_items_.size();
79 94
80 toolitems_.push_back(make_scoped_refptr(extension)); 95 toolbar_items_.push_back(make_scoped_refptr(extension));
81 } 96 }
82 97
83 FOR_EACH_OBSERVER(Observer, observers_, BrowserActionMoved(extension, index)); 98 FOR_EACH_OBSERVER(Observer, observers_, BrowserActionMoved(extension, index));
84 99
85 UpdatePrefs(); 100 UpdatePrefs();
86 } 101 }
87 102
88 ExtensionToolbarModel::Action ExtensionToolbarModel::ExecuteBrowserAction( 103 ExtensionToolbarModel::Action ExtensionToolbarModel::ExecuteBrowserAction(
89 const Extension* extension, 104 const Extension* extension,
90 Browser* browser, 105 Browser* browser,
(...skipping 29 matching lines...) Expand all
120 void ExtensionToolbarModel::SetVisibleIconCount(int count) { 135 void ExtensionToolbarModel::SetVisibleIconCount(int count) {
121 visible_icon_count_ = count == static_cast<int>(size()) ? -1 : count; 136 visible_icon_count_ = count == static_cast<int>(size()) ? -1 : count;
122 prefs_->SetInteger(prefs::kExtensionToolbarSize, visible_icon_count_); 137 prefs_->SetInteger(prefs::kExtensionToolbarSize, visible_icon_count_);
123 } 138 }
124 139
125 void ExtensionToolbarModel::Observe( 140 void ExtensionToolbarModel::Observe(
126 int type, 141 int type,
127 const content::NotificationSource& source, 142 const content::NotificationSource& source,
128 const content::NotificationDetails& details) { 143 const content::NotificationDetails& details) {
129 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) { 144 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) {
130 InitializeExtensionList(); 145 InitializeExtensionLists();
131 return; 146 return;
132 } 147 }
133 148
134 if (!service_->is_ready()) 149 if (!service_->is_ready())
135 return; 150 return;
136 151
137 const Extension* extension = NULL; 152 const Extension* extension = NULL;
138 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { 153 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
139 extension = content::Details<extensions::UnloadedExtensionInfo>( 154 extension = content::Details<extensions::UnloadedExtensionInfo>(
140 details)->extension; 155 details)->extension;
141 } else { 156 } else {
142 extension = content::Details<const Extension>(details).ptr(); 157 extension = content::Details<const Extension>(details).ptr();
143 } 158 }
159 ExtensionList* list_with_extension = FindListWithExtension(extension);
144 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) { 160 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) {
145 // We don't want to add the same extension twice. It may have already been 161 // We don't want to add the same extension twice. It may have already been
146 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user 162 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user
147 // hides the browser action and then disables and enables the extension. 163 // hides the browser action and then disables and enables the extension.
148 for (size_t i = 0; i < toolitems_.size(); i++) { 164 if (list_with_extension)
149 if (toolitems_[i].get() == extension) 165 return;
150 return; // Already exists. 166 if (extensions::switch_utils::IsActionBoxEnabled())
167 AddExtension(extension, &action_box_menu_items_);
168 else if (service_->extension_prefs()->GetBrowserActionVisibility(extension))
169 AddExtension(extension, &toolbar_items_);
170 } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
171 if (list_with_extension)
172 RemoveExtension(extension, list_with_extension);
173 } else if (type ==
174 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED) {
175 if (extensions::switch_utils::IsActionBoxEnabled()) {
176 // TODO(yefim): Implement this when implementing drag & drop
177 // for action box menu.
178 } else {
179 if (service_->extension_prefs()->GetBrowserActionVisibility(extension))
180 AddExtension(extension, &toolbar_items_);
181 else
182 RemoveExtension(extension, &toolbar_items_);
151 } 183 }
152 if (service_->extension_prefs()->GetBrowserActionVisibility(extension))
153 AddExtension(extension);
154 } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) {
155 RemoveExtension(extension);
156 } else if (
157 type ==
158 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED) {
159 if (service_->extension_prefs()->GetBrowserActionVisibility(extension))
160 AddExtension(extension);
161 else
162 RemoveExtension(extension);
163 } else { 184 } else {
164 NOTREACHED() << "Received unexpected notification"; 185 NOTREACHED() << "Received unexpected notification";
165 } 186 }
166 } 187 }
167 188
168 void ExtensionToolbarModel::AddExtension(const Extension* extension) { 189 void ExtensionToolbarModel::AddExtension(const Extension* extension,
190 ExtensionList* list) {
169 // We only care about extensions with browser actions. 191 // We only care about extensions with browser actions.
170 if (!extension->browser_action()) 192 if (!extension->browser_action())
171 return; 193 return;
172 194
173 if (extension->id() == last_extension_removed_ && 195 if (extension->id() == last_extension_removed_ &&
174 last_extension_removed_index_ < toolitems_.size()) { 196 last_extension_removed_index_ < list->size()) {
175 toolitems_.insert(begin() + last_extension_removed_index_, 197 list->insert(list->begin() + last_extension_removed_index_,
176 make_scoped_refptr(extension)); 198 make_scoped_refptr(extension));
177 FOR_EACH_OBSERVER(Observer, observers_, 199 FOR_EACH_OBSERVER(Observer, observers_,
178 BrowserActionAdded(extension, last_extension_removed_index_)); 200 BrowserActionAdded(extension, last_extension_removed_index_));
179 } else { 201 } else {
180 toolitems_.push_back(make_scoped_refptr(extension)); 202 list->push_back(make_scoped_refptr(extension));
181 FOR_EACH_OBSERVER(Observer, observers_, 203 FOR_EACH_OBSERVER(Observer, observers_,
182 BrowserActionAdded(extension, toolitems_.size() - 1)); 204 BrowserActionAdded(extension, list->size() - 1));
183 } 205 }
184 206
185 last_extension_removed_ = ""; 207 last_extension_removed_ = "";
186 last_extension_removed_index_ = -1; 208 last_extension_removed_index_ = -1;
187 209
188 UpdatePrefs(); 210 UpdatePrefs();
189 } 211 }
190 212
191 void ExtensionToolbarModel::RemoveExtension(const Extension* extension) { 213 void ExtensionToolbarModel::RemoveExtension(const Extension* extension,
192 ExtensionList::iterator pos = std::find(begin(), end(), extension); 214 ExtensionList* list) {
193 if (pos == end()) { 215 ExtensionList::iterator pos =
216 std::find(list->begin(), list->end(), extension);
217 if (pos == end())
194 return; 218 return;
195 }
196 219
197 last_extension_removed_ = extension->id(); 220 last_extension_removed_ = extension->id();
198 last_extension_removed_index_ = pos - begin(); 221 last_extension_removed_index_ = pos - list->begin();
199 222
200 toolitems_.erase(pos); 223 list->erase(pos);
201 FOR_EACH_OBSERVER(Observer, observers_, 224 FOR_EACH_OBSERVER(Observer, observers_,
202 BrowserActionRemoved(extension)); 225 BrowserActionRemoved(extension));
203 226
204 UpdatePrefs(); 227 UpdatePrefs();
205 } 228 }
206 229
230 extensions::ExtensionList* ExtensionToolbarModel::FindListWithExtension(
231 const Extension* extension) {
232 if (IsInExtensionList(extension, toolbar_items_))
233 return &toolbar_items_;
234 if (IsInExtensionList(extension, action_box_menu_items_))
235 return &action_box_menu_items_;
236 return NULL;
237 }
238
239
207 // Combine the currently enabled extensions that have browser actions (which 240 // Combine the currently enabled extensions that have browser actions (which
208 // we get from the ExtensionService) with the ordering we get from the 241 // we get from the ExtensionService) with the ordering we get from the
209 // pref service. For robustness we use a somewhat inefficient process: 242 // pref service. For robustness we use a somewhat inefficient process:
210 // 1. Create a vector of extensions sorted by their pref values. This vector may 243 // 1. Create a vector of extensions sorted by their pref values. This vector may
211 // have holes. 244 // have holes.
212 // 2. Create a vector of extensions that did not have a pref value. 245 // 2. Create a vector of extensions that did not have a pref value.
213 // 3. Remove holes from the sorted vector and append the unsorted vector. 246 // 3. Remove holes from the sorted vector and append the unsorted vector.
214 void ExtensionToolbarModel::InitializeExtensionList() { 247 void ExtensionToolbarModel::InitializeExtensionLists() {
215 DCHECK(service_->is_ready()); 248 DCHECK(service_->is_ready());
216 249
217 std::vector<std::string> pref_order = service_->extension_prefs()-> 250 if (extensions::switch_utils::IsActionBoxEnabled())
218 GetToolbarOrder(); 251 PopulateForActionBoxMode();
252 else
253 PopulateForNonActionBoxMode();
254
255 UpdatePrefs();
256
257 extensions_initialized_ = true;
258 FOR_EACH_OBSERVER(Observer, observers_, ModelLoaded());
259 }
260
261 void ExtensionToolbarModel::PopulateForActionBoxMode() {
262 const extensions::ExtensionPrefs::ExtensionIdSet toolbar_order =
263 service_->extension_prefs()->GetToolbarOrder();
264 extensions::ExtensionPrefs::ExtensionIdSet action_box_order =
265 service_->extension_prefs()->GetActionBoxOrder();
266
267 // Add all browser actions not already in the toolbar or action box
268 // to the action box (the prefs list may omit some extensions).
269 for (ExtensionSet::const_iterator iter = service_->extensions()->begin();
270 iter != service_->extensions()->end(); ++iter) {
271 const Extension* extension = *iter;
272 if (!extension->browser_action())
273 continue;
274
275 extensions::ExtensionPrefs::ExtensionIdSet::const_iterator toolbar_pos =
276 std::find(toolbar_order.begin(), toolbar_order.end(), extension->id());
277 if (toolbar_pos != toolbar_order.end())
278 continue;
279
280 extensions::ExtensionPrefs::ExtensionIdSet::const_iterator action_box_pos =
281 std::find(action_box_order.begin(), action_box_order.end(),
282 extension->id());
283 if (action_box_pos != action_box_order.end())
284 continue;
285
286 action_box_order.push_back(extension->id());
287 }
288
289 FillExtensionList(action_box_order, &action_box_menu_items_);
290 FillExtensionList(toolbar_order, &toolbar_items_);
291 }
292
293 void ExtensionToolbarModel::PopulateForNonActionBoxMode() {
294 const extensions::ExtensionPrefs::ExtensionIdSet pref_order =
295 service_->extension_prefs()->GetToolbarOrder();
219 // Items that have a pref for their position. 296 // Items that have a pref for their position.
220 ExtensionList sorted; 297 ExtensionList sorted;
221 sorted.resize(pref_order.size(), NULL); 298 sorted.resize(pref_order.size(), NULL);
222 // The items that don't have a pref for their position. 299 // The items that don't have a pref for their position.
223 ExtensionList unsorted; 300 ExtensionList unsorted;
224 301
225 // Create the lists. 302 // Create the lists.
226 for (ExtensionSet::const_iterator it = service_->extensions()->begin(); 303 for (ExtensionSet::const_iterator it = service_->extensions()->begin();
227 it != service_->extensions()->end(); ++it) { 304 it != service_->extensions()->end(); ++it) {
228 const Extension* extension = *it; 305 const Extension* extension = *it;
229 if (!extension->browser_action()) 306 if (!extension->browser_action())
230 continue; 307 continue;
231 if (!service_->extension_prefs()->GetBrowserActionVisibility(extension)) 308 if (!service_->extension_prefs()->GetBrowserActionVisibility(extension))
232 continue; 309 continue;
233 310
234 std::vector<std::string>::iterator pos = 311 extensions::ExtensionPrefs::ExtensionIdSet::const_iterator pos =
235 std::find(pref_order.begin(), pref_order.end(), extension->id()); 312 std::find(pref_order.begin(), pref_order.end(), extension->id());
236 if (pos != pref_order.end()) { 313 if (pos != pref_order.end())
237 int index = std::distance(pref_order.begin(), pos); 314 sorted[pos - pref_order.begin()] = extension;
238 sorted[index] = extension; 315 else
239 } else {
240 unsorted.push_back(make_scoped_refptr(extension)); 316 unsorted.push_back(make_scoped_refptr(extension));
241 }
242 } 317 }
243 318
244 // Merge the lists. 319 // Merge the lists.
245 toolitems_.reserve(sorted.size() + unsorted.size()); 320 toolbar_items_.clear();
246 for (ExtensionList::iterator iter = sorted.begin(); 321 toolbar_items_.reserve(sorted.size() + unsorted.size());
322 for (ExtensionList::const_iterator iter = sorted.begin();
247 iter != sorted.end(); ++iter) { 323 iter != sorted.end(); ++iter) {
324 // It's possible for the extension order to contain items that aren't
325 // actually loaded on this machine. For example, when extension sync is on,
326 // we sync the extension order as-is but double-check with the user before
327 // syncing NPAPI-containing extensions, so if one of those is not actually
328 // synced, we'll get a NULL in the list. This sort of case can also happen
329 // if some error prevents an extension from loading.
248 if (*iter != NULL) 330 if (*iter != NULL)
249 toolitems_.push_back(*iter); 331 toolbar_items_.push_back(*iter);
250 } 332 }
251 toolitems_.insert(toolitems_.end(), unsorted.begin(), unsorted.end()); 333 toolbar_items_.insert(toolbar_items_.end(), unsorted.begin(),
334 unsorted.end());
252 335
253 // Inform observers. 336 // Inform observers.
254 for (size_t i = 0; i < toolitems_.size(); i++) { 337 for (size_t i = 0; i < toolbar_items_.size(); i++) {
255 FOR_EACH_OBSERVER(Observer, observers_, 338 FOR_EACH_OBSERVER(Observer, observers_,
256 BrowserActionAdded(toolitems_[i], i)); 339 BrowserActionAdded(toolbar_items_[i], i));
257 } 340 }
341 }
258 342
259 UpdatePrefs(); 343 void ExtensionToolbarModel::FillExtensionList(
260 344 const extensions::ExtensionPrefs::ExtensionIdSet& order,
261 extensions_initialized_ = true; 345 ExtensionList* list) {
262 FOR_EACH_OBSERVER(Observer, observers_, ModelLoaded()); 346 list->clear();
347 list->reserve(order.size());
348 for (size_t i = 0; i < order.size(); ++i) {
349 const extensions::Extension* extension =
350 service_->GetExtensionById(order[i], false);
351 if (extension)
352 AddExtension(extension, list);
353 }
263 } 354 }
264 355
265 void ExtensionToolbarModel::UpdatePrefs() { 356 void ExtensionToolbarModel::UpdatePrefs() {
266 if (!service_->extension_prefs()) 357 if (!service_->extension_prefs())
267 return; 358 return;
268 359
269 std::vector<std::string> ids; 360 extensions::ExtensionPrefs::ExtensionIdSet toolbar_ids;
270 ids.reserve(toolitems_.size()); 361 toolbar_ids.reserve(toolbar_items_.size());
271 for (ExtensionList::iterator iter = begin(); iter != end(); ++iter) 362 for (size_t i = 0; i < toolbar_items_.size(); ++i)
272 ids.push_back((*iter)->id()); 363 toolbar_ids.push_back(toolbar_items_[i]->id());
273 service_->extension_prefs()->SetToolbarOrder(ids); 364 service_->extension_prefs()->SetToolbarOrder(toolbar_ids);
274 }
275 365
276 const Extension* ExtensionToolbarModel::GetExtensionByIndex(int index) const { 366 extensions::ExtensionPrefs::ExtensionIdSet action_box_ids;
277 return toolitems_[index]; 367 action_box_ids.reserve(action_box_menu_items_.size());
368 for (size_t i = 0; i < action_box_menu_items_.size(); ++i)
369 action_box_ids.push_back(action_box_menu_items_[i]->id());
370 service_->extension_prefs()->SetActionBoxOrder(action_box_ids);
278 } 371 }
279 372
280 int ExtensionToolbarModel::IncognitoIndexToOriginal(int incognito_index) { 373 int ExtensionToolbarModel::IncognitoIndexToOriginal(int incognito_index) {
281 int original_index = 0, i = 0; 374 int original_index = 0, i = 0;
282 for (ExtensionList::iterator iter = begin(); iter != end(); 375 for (ExtensionList::iterator iter = begin(); iter != end();
283 ++iter, ++original_index) { 376 ++iter, ++original_index) {
284 if (service_->IsIncognitoEnabled((*iter)->id())) { 377 if (service_->IsIncognitoEnabled((*iter)->id())) {
285 if (incognito_index == i) 378 if (incognito_index == i)
286 break; 379 break;
287 ++i; 380 ++i;
288 } 381 }
289 } 382 }
290 return original_index; 383 return original_index;
291 } 384 }
292 385
293 int ExtensionToolbarModel::OriginalIndexToIncognito(int original_index) { 386 int ExtensionToolbarModel::OriginalIndexToIncognito(int original_index) {
294 int incognito_index = 0, i = 0; 387 int incognito_index = 0, i = 0;
295 for (ExtensionList::iterator iter = begin(); iter != end(); 388 for (ExtensionList::iterator iter = begin(); iter != end();
296 ++iter, ++i) { 389 ++iter, ++i) {
297 if (original_index == i) 390 if (original_index == i)
298 break; 391 break;
299 if (service_->IsIncognitoEnabled((*iter)->id())) 392 if (service_->IsIncognitoEnabled((*iter)->id()))
300 ++incognito_index; 393 ++incognito_index;
301 } 394 }
302 return incognito_index; 395 return incognito_index;
303 } 396 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698