OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_browser_event_router.h" | 5 #include "chrome/browser/extensions/extension_browser_event_router.h" |
6 | 6 |
7 #include "base/json/json_writer.h" | 7 #include "base/json/json_writer.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/extensions/extension_event_names.h" | 9 #include "chrome/browser/extensions/extension_event_names.h" |
10 #include "chrome/browser/extensions/extension_event_router.h" | 10 #include "chrome/browser/extensions/extension_event_router.h" |
11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" | 11 #include "chrome/browser/extensions/extension_page_actions_module_constants.h" |
12 #include "chrome/browser/extensions/extension_tabs_module_constants.h" | 12 #include "chrome/browser/extensions/extension_tabs_module_constants.h" |
13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/tabs/tab_strip_model.h" | 14 #include "chrome/browser/tabs/tab_strip_model.h" |
15 #include "chrome/browser/ui/browser.h" | 15 #include "chrome/browser/ui/browser.h" |
16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.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_constants.h" | 19 #include "chrome/common/extensions/extension_constants.h" |
20 #include "content/browser/tab_contents/navigation_entry.h" | 20 #include "content/browser/tab_contents/navigation_entry.h" |
21 #include "content/browser/tab_contents/tab_contents.h" | 21 #include "content/browser/tab_contents/tab_contents.h" |
22 #include "content/common/notification_service.h" | 22 #include "content/common/notification_service.h" |
| 23 #include "chrome/browser/download/download_manager.h" |
| 24 #include "chrome/browser/download/download_query.h" |
23 | 25 |
24 namespace events = extension_event_names; | 26 namespace events = extension_event_names; |
25 namespace tab_keys = extension_tabs_module_constants; | 27 namespace tab_keys = extension_tabs_module_constants; |
26 namespace page_action_keys = extension_page_actions_module_constants; | 28 namespace page_action_keys = extension_page_actions_module_constants; |
27 | 29 |
28 ExtensionBrowserEventRouter::TabEntry::TabEntry() | 30 ExtensionBrowserEventRouter::TabEntry::TabEntry() |
29 : complete_waiting_on_load_(false), | 31 : complete_waiting_on_load_(false), |
30 url_() { | 32 url_() { |
31 } | 33 } |
32 | 34 |
(...skipping 22 matching lines...) Expand all Loading... |
55 tab_keys::kStatusValueLoading); | 57 tab_keys::kStatusValueLoading); |
56 | 58 |
57 if (contents->GetURL() != url_) { | 59 if (contents->GetURL() != url_) { |
58 url_ = contents->GetURL(); | 60 url_ = contents->GetURL(); |
59 changed_properties->SetString(tab_keys::kUrlKey, url_.spec()); | 61 changed_properties->SetString(tab_keys::kUrlKey, url_.spec()); |
60 } | 62 } |
61 | 63 |
62 return changed_properties; | 64 return changed_properties; |
63 } | 65 } |
64 | 66 |
| 67 class ExtensionBrowserEventRouter::DownloadManagerObserver |
| 68 : public DownloadManager::Observer { |
| 69 public: |
| 70 DownloadManagerObserver(ExtensionBrowserEventRouter* router) |
| 71 : router_(router) { |
| 72 DVLOG(1) << __PRETTY_FUNCTION__ << " " << this << " " << router_; |
| 73 DCHECK(router_); |
| 74 manager_ = router_->profile_->GetDownloadManager(); |
| 75 DCHECK(manager_); |
| 76 manager_->AddObserver(this); |
| 77 } |
| 78 virtual ~DownloadManagerObserver() { |
| 79 DVLOG(1) << __PRETTY_FUNCTION__ << " " << this << " " << router_; |
| 80 if (manager_) manager_->RemoveObserver(this); |
| 81 } |
| 82 |
| 83 virtual void ModelChanged() { |
| 84 DCHECK(manager_); |
| 85 DVLOG(1) << __FUNCTION__ << " " << manager_ << " " << router_; |
| 86 std::vector<DownloadItem*> items; |
| 87 ListValue json_items; |
| 88 manager_->Search(download_util::DownloadQuery(), &items, true, NULL, &json_i
tems); |
| 89 DVLOG(1) << __FUNCTION__ << " " << items.size(); |
| 90 base::hash_map<int64, DownloadItem*> updated; |
| 91 for (std::vector<DownloadItem*>::const_iterator iter = items.begin(); |
| 92 iter != items.end(); ++iter) { |
| 93 updated[(*iter)->id()] = *iter; |
| 94 } |
| 95 |
| 96 // An item is new if it's in items but not downloads_. |
| 97 for (ListValue::const_iterator iter = json_items.begin(); |
| 98 iter != json_items.end(); ++iter) { |
| 99 int item_id = -1; |
| 100 DCHECK(((DictionaryValue*)(*iter))->GetInteger("id", &item_id)); |
| 101 base::hash_map<int64, DownloadItem*>::const_iterator found = downloads_.fi
nd(item_id); |
| 102 if (found == downloads_.end()) { |
| 103 DispatchEvent(events::kOnDownloadCreated, (*iter)); |
| 104 downloads_[item_id] = updated[item_id]; |
| 105 } |
| 106 } |
| 107 |
| 108 // An item was erased if it's in downloads_ but not updated. |
| 109 for (base::hash_map<int64, DownloadItem*>::iterator iter = downloads_.begin(
); |
| 110 iter != downloads_.end(); ++iter) { |
| 111 if (updated.find(iter->first) == updated.end()) { |
| 112 DispatchEvent(events::kOnDownloadErased, Value::CreateIntegerValue(iter-
>first)); |
| 113 downloads_.erase(iter); |
| 114 } |
| 115 } |
| 116 |
| 117 // TODO assert that updated == downloads_ |
| 118 } |
| 119 |
| 120 virtual void ManagerGoingDown() { |
| 121 DVLOG(1) << __PRETTY_FUNCTION__ << " " << this << " " << router_ << " " << m
anager_; |
| 122 manager_ = NULL; |
| 123 } |
| 124 |
| 125 private: |
| 126 void DispatchEvent(const char* const event, Value* arg) { |
| 127 ListValue args; |
| 128 args.Append(arg); |
| 129 std::string json_args; |
| 130 base::JSONWriter::Write(&args, false, &json_args); |
| 131 router_->DispatchEvent(router_->profile_, event, json_args); |
| 132 } |
| 133 |
| 134 ExtensionBrowserEventRouter* router_; |
| 135 DownloadManager* manager_; |
| 136 base::hash_map<int64, DownloadItem*> downloads_; |
| 137 |
| 138 DISALLOW_COPY_AND_ASSIGN(DownloadManagerObserver); |
| 139 }; |
| 140 |
65 void ExtensionBrowserEventRouter::Init() { | 141 void ExtensionBrowserEventRouter::Init() { |
66 if (initialized_) | 142 if (initialized_) |
67 return; | 143 return; |
68 BrowserList::AddObserver(this); | 144 BrowserList::AddObserver(this); |
69 #if defined(TOOLKIT_VIEWS) | 145 #if defined(TOOLKIT_VIEWS) |
70 views::FocusManager::GetWidgetFocusManager()->AddFocusChangeListener(this); | 146 views::FocusManager::GetWidgetFocusManager()->AddFocusChangeListener(this); |
71 #elif defined(TOOLKIT_GTK) | 147 #elif defined(TOOLKIT_GTK) |
72 ui::ActiveWindowWatcherX::AddObserver(this); | 148 ui::ActiveWindowWatcherX::AddObserver(this); |
73 #elif defined(OS_MACOSX) | 149 #elif defined(OS_MACOSX) |
74 // Needed for when no suitable window can be passed to an extension as the | 150 // Needed for when no suitable window can be passed to an extension as the |
(...skipping 11 matching lines...) Expand all Loading... |
86 // Also catch up our internal bookkeeping of tab entries. | 162 // Also catch up our internal bookkeeping of tab entries. |
87 Browser* browser = *iter; | 163 Browser* browser = *iter; |
88 if (browser->tabstrip_model()) { | 164 if (browser->tabstrip_model()) { |
89 for (int i = 0; i < browser->tabstrip_model()->count(); ++i) { | 165 for (int i = 0; i < browser->tabstrip_model()->count(); ++i) { |
90 TabContents* contents = browser->GetTabContentsAt(i); | 166 TabContents* contents = browser->GetTabContentsAt(i); |
91 int tab_id = ExtensionTabUtil::GetTabId(contents); | 167 int tab_id = ExtensionTabUtil::GetTabId(contents); |
92 tab_entries_[tab_id] = TabEntry(); | 168 tab_entries_[tab_id] = TabEntry(); |
93 } | 169 } |
94 } | 170 } |
95 } | 171 } |
| 172 download_manager_observer_.reset(new DownloadManagerObserver(this)); |
96 | 173 |
97 initialized_ = true; | 174 initialized_ = true; |
98 } | 175 } |
99 | 176 |
100 ExtensionBrowserEventRouter::ExtensionBrowserEventRouter(Profile* profile) | 177 ExtensionBrowserEventRouter::ExtensionBrowserEventRouter(Profile* profile) |
101 : initialized_(false), | 178 : initialized_(false), |
102 profile_(profile), | 179 profile_(profile), |
103 focused_profile_(NULL), | 180 focused_profile_(NULL), |
104 focused_window_id_(extension_misc::kUnknownWindowId) { | 181 focused_window_id_(extension_misc::kUnknownWindowId) { |
105 DCHECK(!profile->IsOffTheRecord()); | 182 DCHECK(!profile->IsOffTheRecord()); |
(...skipping 482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 | 665 |
589 void ExtensionBrowserEventRouter::BrowserActionExecuted( | 666 void ExtensionBrowserEventRouter::BrowserActionExecuted( |
590 Profile* profile, const std::string& extension_id, Browser* browser) { | 667 Profile* profile, const std::string& extension_id, Browser* browser) { |
591 TabContentsWrapper* tab_contents = NULL; | 668 TabContentsWrapper* tab_contents = NULL; |
592 int tab_id = 0; | 669 int tab_id = 0; |
593 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) | 670 if (!ExtensionTabUtil::GetDefaultTab(browser, &tab_contents, &tab_id)) |
594 return; | 671 return; |
595 DispatchEventWithTab(profile, extension_id, "browserAction.onClicked", | 672 DispatchEventWithTab(profile, extension_id, "browserAction.onClicked", |
596 tab_contents->tab_contents(), true); | 673 tab_contents->tab_contents(), true); |
597 } | 674 } |
OLD | NEW |