Chromium Code Reviews| 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_settings_frontend.h" | 5 #include "chrome/browser/extensions/extension_settings_frontend.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "chrome/browser/extensions/extension_event_names.h" | |
| 10 #include "chrome/browser/extensions/extension_event_router.h" | |
| 11 #include "chrome/browser/extensions/extension_service.h" | |
| 9 #include "chrome/browser/extensions/extension_settings_backend.h" | 12 #include "chrome/browser/extensions/extension_settings_backend.h" |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "content/browser/browser_thread.h" | 14 #include "content/browser/browser_thread.h" |
| 11 | 15 |
| 12 ExtensionSettingsFrontend::ExtensionSettingsFrontend( | 16 ExtensionSettingsFrontend::ExtensionSettingsFrontend(Profile* profile) |
| 13 const FilePath& base_path) | 17 : observers_(new ObserverListThreadSafe<ExtensionSettingsObserver>()), |
| 14 : core_(new ExtensionSettingsFrontend::Core()) { | 18 core_(new ExtensionSettingsFrontend::Core(observers_.get())), |
| 19 profile_(profile) { | |
| 15 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 20 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 21 | |
| 22 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_CREATED, | |
| 23 NotificationService::AllBrowserContextsAndSources()); | |
| 24 registrar_.Add(this, chrome::NOTIFICATION_PROFILE_DESTROYED, | |
| 25 NotificationService::AllBrowserContextsAndSources()); | |
| 26 OnProfileCreated(profile); | |
| 27 | |
| 16 BrowserThread::PostTask( | 28 BrowserThread::PostTask( |
| 17 BrowserThread::FILE, | 29 BrowserThread::FILE, |
| 18 FROM_HERE, | 30 FROM_HERE, |
| 19 base::Bind( | 31 base::Bind( |
| 20 &ExtensionSettingsFrontend::Core::InitOnFileThread, | 32 &ExtensionSettingsFrontend::Core::InitOnFileThread, |
| 21 core_.get(), | 33 core_.get(), |
| 22 base_path)); | 34 profile->GetPath().AppendASCII( |
| 35 ExtensionService::kSettingsDirectoryName))); | |
| 36 } | |
| 37 | |
| 38 ExtensionSettingsFrontend::~ExtensionSettingsFrontend() { | |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 23 } | 40 } |
| 24 | 41 |
| 25 void ExtensionSettingsFrontend::RunWithBackend( | 42 void ExtensionSettingsFrontend::RunWithBackend( |
| 26 const BackendCallback& callback) { | 43 const BackendCallback& callback) { |
| 27 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 28 BrowserThread::PostTask( | 45 BrowserThread::PostTask( |
| 29 BrowserThread::FILE, | 46 BrowserThread::FILE, |
| 30 FROM_HERE, | 47 FROM_HERE, |
| 31 base::Bind( | 48 base::Bind( |
| 32 &ExtensionSettingsFrontend::Core::RunWithBackendOnFileThread, | 49 &ExtensionSettingsFrontend::Core::RunWithBackendOnFileThread, |
| 33 core_.get(), | 50 core_.get(), |
| 34 callback)); | 51 callback)); |
| 35 } | 52 } |
| 36 | 53 |
| 37 ExtensionSettingsFrontend::~ExtensionSettingsFrontend() { | 54 void ExtensionSettingsFrontend::AddObserver( |
| 55 ExtensionSettingsObserver* observer) { | |
| 56 observers_->AddObserver(observer); | |
|
akalin
2011/10/07 21:06:23
DCHECK UI
not at google - send to devlin
2011/10/10 01:00:16
Done.
| |
| 57 } | |
| 58 | |
| 59 void ExtensionSettingsFrontend::RemoveObserver( | |
| 60 ExtensionSettingsObserver* observer) { | |
| 61 observers_->RemoveObserver(observer); | |
|
akalin
2011/10/07 21:06:23
DCHECK UI
not at google - send to devlin
2011/10/10 01:00:16
Done.
| |
| 62 } | |
| 63 | |
| 64 void ExtensionSettingsFrontend::TriggerOnSettingsChanged( | |
| 65 Profile* profile, | |
| 66 const std::string& extension_id, | |
| 67 const ExtensionSettingsObserver::ChangeList& changes) { | |
| 68 observers_->Notify( | |
|
akalin
2011/10/07 21:06:23
DCHECK UI
not at google - send to devlin
2011/10/10 01:00:16
Method deleted.
| |
| 69 &ExtensionSettingsObserver::OnSettingsChanged, | |
| 70 profile, | |
| 71 extension_id, | |
| 72 changes.GetEventJson()); | |
| 73 } | |
| 74 | |
| 75 void ExtensionSettingsFrontend::Observe( | |
| 76 int type, | |
| 77 const NotificationSource& source, | |
| 78 const NotificationDetails& details) { | |
| 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 80 switch (type) { | |
| 81 case chrome::NOTIFICATION_PROFILE_CREATED: | |
| 82 OnProfileCreated(Source<Profile>(source).ptr()); | |
| 83 break; | |
| 84 case chrome::NOTIFICATION_PROFILE_DESTROYED: | |
| 85 OnProfileDestroyed(Source<Profile>(source).ptr()); | |
| 86 break; | |
| 87 default: | |
| 88 NOTREACHED(); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void ExtensionSettingsFrontend::OnProfileCreated(Profile* new_profile) { | |
| 93 DCHECK(profile_observers_.find(new_profile) == profile_observers_.end()); | |
| 94 DefaultObserver* new_observer = new DefaultObserver(new_profile); | |
| 95 profile_observers_[new_profile] = new_observer; | |
| 96 AddObserver(new_observer); | |
| 97 } | |
| 98 | |
| 99 void ExtensionSettingsFrontend::OnProfileDestroyed(Profile* old_profile) { | |
| 100 DefaultObserver* old_observer = profile_observers_[old_profile]; | |
| 101 DCHECK(old_observer); | |
| 102 profile_observers_.erase(old_profile); | |
|
akalin
2011/10/07 21:06:23
this is a leak. Suggest using link_ptrs instead.
not at google - send to devlin
2011/10/10 01:00:16
Ah.
| |
| 103 RemoveObserver(old_observer); | |
| 104 } | |
| 105 | |
| 106 ExtensionSettingsFrontend::Core::Core( | |
| 107 ObserverListThreadSafe<ExtensionSettingsObserver>* observers) | |
| 108 : observers_(observers), backend_(NULL) { | |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 39 } | 110 } |
| 40 | 111 |
| 41 ExtensionSettingsFrontend::Core::Core() : backend_(NULL) { | |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 43 } | |
| 44 | |
| 45 void ExtensionSettingsFrontend::Core::InitOnFileThread( | 112 void ExtensionSettingsFrontend::Core::InitOnFileThread( |
| 46 const FilePath& base_path) { | 113 const FilePath& base_path) { |
| 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 48 DCHECK(!backend_); | 115 DCHECK(!backend_); |
| 49 backend_ = new ExtensionSettingsBackend(base_path); | 116 backend_ = new ExtensionSettingsBackend(base_path, observers_); |
| 50 } | 117 } |
| 51 | 118 |
| 52 void ExtensionSettingsFrontend::Core::RunWithBackendOnFileThread( | 119 void ExtensionSettingsFrontend::Core::RunWithBackendOnFileThread( |
| 53 const BackendCallback& callback) { | 120 const BackendCallback& callback) { |
| 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 55 DCHECK(backend_); | 122 DCHECK(backend_); |
| 56 callback.Run(backend_); | 123 callback.Run(backend_); |
| 57 } | 124 } |
| 58 | 125 |
| 59 ExtensionSettingsFrontend::Core::~Core() { | 126 ExtensionSettingsFrontend::Core::~Core() { |
| 60 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 127 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
| 61 delete backend_; | 128 delete backend_; |
| 62 } else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 129 } else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 63 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, backend_); | 130 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, backend_); |
| 64 } else { | 131 } else { |
| 65 NOTREACHED(); | 132 NOTREACHED(); |
| 66 } | 133 } |
| 67 } | 134 } |
| 135 | |
| 136 ExtensionSettingsFrontend::DefaultObserver::DefaultObserver( | |
| 137 Profile* target_profile) : target_profile_(target_profile) {} | |
| 138 | |
| 139 ExtensionSettingsFrontend::DefaultObserver::~DefaultObserver() {} | |
| 140 | |
| 141 void ExtensionSettingsFrontend::DefaultObserver::OnSettingsChanged( | |
| 142 Profile* origin_profile, | |
| 143 const std::string& extension_id, | |
| 144 const std::string& event_json) { | |
| 145 if (origin_profile != target_profile_) { | |
| 146 target_profile_->GetExtensionEventRouter()->DispatchEventToExtension( | |
| 147 extension_id, | |
| 148 extension_event_names::kOnSettingsChanged, | |
| 149 event_json, | |
| 150 target_profile_, | |
| 151 GURL()); | |
| 152 } | |
| 153 } | |
| OLD | NEW |