| OLD | NEW |
| 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/external_pref_loader.h" | 5 #include "chrome/browser/extensions/external_pref_loader.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/json/json_file_value_serializer.h" | 11 #include "base/json/json_file_value_serializer.h" |
| 12 #include "base/json/json_string_value_serializer.h" | 12 #include "base/json/json_string_value_serializer.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/path_service.h" | 15 #include "base/path_service.h" |
| 16 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "chrome/browser/prefs/pref_service_syncable.h" |
| 18 #include "chrome/common/chrome_paths.h" | 19 #include "chrome/common/chrome_paths.h" |
| 19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 20 | 21 |
| 21 using content::BrowserThread; | 22 using content::BrowserThread; |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 base::FilePath::CharType kExternalExtensionJson[] = | 26 base::FilePath::CharType kExternalExtensionJson[] = |
| 26 FILE_PATH_LITERAL("external_extensions.json"); | 27 FILE_PATH_LITERAL("external_extensions.json"); |
| 27 | 28 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 | 84 |
| 84 LOG(WARNING) << "Expected a JSON dictionary in file " | 85 LOG(WARNING) << "Expected a JSON dictionary in file " |
| 85 << path.value() << "."; | 86 << path.value() << "."; |
| 86 return new base::DictionaryValue; | 87 return new base::DictionaryValue; |
| 87 } | 88 } |
| 88 | 89 |
| 89 } // namespace | 90 } // namespace |
| 90 | 91 |
| 91 namespace extensions { | 92 namespace extensions { |
| 92 | 93 |
| 93 ExternalPrefLoader::ExternalPrefLoader(int base_path_id, Options options) | 94 ExternalPrefLoader::ExternalPrefLoader(int base_path_id, |
| 94 : base_path_id_(base_path_id), options_(options) { | 95 Options options, |
| 96 Profile* profile) |
| 97 : base_path_id_(base_path_id), |
| 98 options_(options), |
| 99 profile_(profile), |
| 100 pref_observer_added_(false) { |
| 95 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 101 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 96 } | 102 } |
| 97 | 103 |
| 104 ExternalPrefLoader::~ExternalPrefLoader() { |
| 105 if (pref_observer_added_) { |
| 106 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
| 107 DCHECK(prefs); |
| 108 prefs->RemoveObserver(this); |
| 109 } |
| 110 } |
| 111 |
| 98 const base::FilePath ExternalPrefLoader::GetBaseCrxFilePath() { | 112 const base::FilePath ExternalPrefLoader::GetBaseCrxFilePath() { |
| 99 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 113 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 100 | 114 |
| 101 // |base_path_| was set in LoadOnFileThread(). | 115 // |base_path_| was set in LoadOnFileThread(). |
| 102 return base_path_; | 116 return base_path_; |
| 103 } | 117 } |
| 104 | 118 |
| 105 void ExternalPrefLoader::StartLoading() { | 119 void ExternalPrefLoader::StartLoading() { |
| 106 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 120 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 107 BrowserThread::PostTask( | 121 if (options_ & DELAY_LOAD_UNTIL_PRIORITY_SYNC) { |
| 108 BrowserThread::FILE, FROM_HERE, | 122 if (!PostLoadIfPrioritySyncReady()) { |
| 109 base::Bind(&ExternalPrefLoader::LoadOnFileThread, this)); | 123 DCHECK(profile_); |
| 124 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
| 125 DCHECK(prefs); |
| 126 prefs->AddObserver(this); |
| 127 pref_observer_added_ = true; |
| 128 } |
| 129 } else { |
| 130 BrowserThread::PostTask( |
| 131 BrowserThread::FILE, FROM_HERE, |
| 132 base::Bind(&ExternalPrefLoader::LoadOnFileThread, this)); |
| 133 } |
| 134 } |
| 135 |
| 136 void ExternalPrefLoader::OnIsSyncingChanged() { |
| 137 PostLoadIfPrioritySyncReady(); |
| 138 } |
| 139 |
| 140 bool ExternalPrefLoader::PostLoadIfPrioritySyncReady() { |
| 141 DCHECK(options_ & DELAY_LOAD_UNTIL_PRIORITY_SYNC); |
| 142 DCHECK(profile_); |
| 143 |
| 144 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
| 145 DCHECK(prefs); |
| 146 if (prefs->IsPrioritySyncing()) { |
| 147 BrowserThread::PostTask( |
| 148 BrowserThread::FILE, FROM_HERE, |
| 149 base::Bind(&ExternalPrefLoader::LoadOnFileThread, this)); |
| 150 prefs->RemoveObserver(this); |
| 151 pref_observer_added_ = false; |
| 152 return true; |
| 153 } |
| 154 |
| 155 return false; |
| 110 } | 156 } |
| 111 | 157 |
| 112 void ExternalPrefLoader::LoadOnFileThread() { | 158 void ExternalPrefLoader::LoadOnFileThread() { |
| 113 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 159 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 114 | 160 |
| 115 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue); | 161 scoped_ptr<base::DictionaryValue> prefs(new base::DictionaryValue); |
| 116 | 162 |
| 117 // TODO(skerner): Some values of base_path_id_ will cause | 163 // TODO(skerner): Some values of base_path_id_ will cause |
| 118 // PathService::Get() to return false, because the path does | 164 // PathService::Get() to return false, because the path does |
| 119 // not exist. Find and fix the build/install scripts so that | 165 // not exist. Find and fix the build/install scripts so that |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 LoadFinished(); | 291 LoadFinished(); |
| 246 } | 292 } |
| 247 | 293 |
| 248 ExternalTestingLoader::~ExternalTestingLoader() {} | 294 ExternalTestingLoader::~ExternalTestingLoader() {} |
| 249 | 295 |
| 250 const base::FilePath ExternalTestingLoader::GetBaseCrxFilePath() { | 296 const base::FilePath ExternalTestingLoader::GetBaseCrxFilePath() { |
| 251 return fake_base_path_; | 297 return fake_base_path_; |
| 252 } | 298 } |
| 253 | 299 |
| 254 } // namespace extensions | 300 } // namespace extensions |
| OLD | NEW |