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/external_pref_extension_loader.h" | 5 #include "chrome/browser/extensions/external_pref_extension_loader.h" |
| 6 | 6 |
| 7 #include "app/app_paths.h" | 7 #include "app/app_paths.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
| 12 #include "chrome/browser/browser_thread.h" | 12 #include "chrome/browser/browser_thread.h" |
| 13 #include "chrome/common/json_value_serializer.h" | 13 #include "chrome/common/json_value_serializer.h" |
| 14 | 14 |
| 15 namespace { | 15 namespace { |
| 16 | 16 |
| 17 // Caller takes ownership of the returned dictionary | 17 // Caller takes ownership of the returned dictionary. |
| 18 DictionaryValue* ExtractPrefs(ValueSerializer* serializer) { | 18 DictionaryValue* ExtractPrefs(const FilePath& path, |
| 19 ValueSerializer* serializer) { | |
| 19 std::string error_msg; | 20 std::string error_msg; |
| 20 Value* extensions = serializer->Deserialize(NULL, &error_msg); | 21 Value* extensions = serializer->Deserialize(NULL, &error_msg); |
| 21 if (!extensions) { | 22 if (!extensions) { |
| 22 LOG(WARNING) << "Unable to deserialize json data: " << error_msg; | 23 LOG(WARNING) << "Unable to deserialize json data: " << error_msg |
| 24 << " In file " << path.value() << " ."; | |
| 23 } else { | 25 } else { |
| 24 if (!extensions->IsType(Value::TYPE_DICTIONARY)) { | 26 if (!extensions->IsType(Value::TYPE_DICTIONARY)) { |
| 25 NOTREACHED() << "Invalid json data"; | 27 LOG(WARNING) << "Expected a JSON dictionary in file " |
| 28 << path.value() << " ."; | |
| 26 } else { | 29 } else { |
| 27 return static_cast<DictionaryValue*>(extensions); | 30 return static_cast<DictionaryValue*>(extensions); |
| 28 } | 31 } |
| 29 } | 32 } |
| 30 return new DictionaryValue; | 33 return new DictionaryValue; |
| 31 } | 34 } |
| 32 | 35 |
| 33 } // namespace | 36 } // namespace |
| 34 | 37 |
| 35 ExternalPrefExtensionLoader::ExternalPrefExtensionLoader(int base_path_key) | 38 ExternalPrefExtensionLoader::ExternalPrefExtensionLoader(int base_path_key) |
| 36 : base_path_key_(base_path_key) { | 39 : base_path_key_(base_path_key) { |
| 37 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 40 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 38 } | 41 } |
| 39 | 42 |
| 40 const FilePath ExternalPrefExtensionLoader::GetBaseCrxFilePath() { | 43 const FilePath ExternalPrefExtensionLoader::GetBaseCrxFilePath() { |
| 41 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 42 | 45 |
| 43 // LoadOnFileThread() should set |external_file_path_| to a non-empty | 46 // |base_path_| was set in LoadOnFileThread(). |
| 44 // path. This function should not be called until after LoadOnFileThread() | |
| 45 // is complete. | |
| 46 CHECK(!base_path_.empty()); | |
|
Sam Kerner (Chrome)
2011/01/21 15:47:01
Yesterday we talked about changing this to:
CHEC
| |
| 47 | |
| 48 return base_path_; | 47 return base_path_; |
| 49 } | 48 } |
| 50 | 49 |
| 51 void ExternalPrefExtensionLoader::StartLoading() { | 50 void ExternalPrefExtensionLoader::StartLoading() { |
| 52 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 51 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 53 BrowserThread::PostTask( | 52 BrowserThread::PostTask( |
| 54 BrowserThread::FILE, FROM_HERE, | 53 BrowserThread::FILE, FROM_HERE, |
| 55 NewRunnableMethod( | 54 NewRunnableMethod( |
| 56 this, | 55 this, |
| 57 &ExternalPrefExtensionLoader::LoadOnFileThread)); | 56 &ExternalPrefExtensionLoader::LoadOnFileThread)); |
| 58 } | 57 } |
| 59 | 58 |
| 60 void ExternalPrefExtensionLoader::LoadOnFileThread() { | 59 void ExternalPrefExtensionLoader::LoadOnFileThread() { |
| 61 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 60 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 62 | 61 |
| 62 // TODO(skerner): Some values of base_path_key_ will cause | |
| 63 // PathServcie::Get() to return false, because the path does | |
|
Erik does not do reviews
2011/01/21 17:05:25
PathService
Sam Kerner (Chrome)
2011/01/21 17:59:26
Done.
| |
| 64 // not exist. Find and fix the build/install scripts so that | |
| 65 // this can become a CHECK(). Known exmples include chrome | |
| 66 // OS develoer builds and linux install packages. | |
|
Erik does not do reviews
2011/01/21 17:05:25
developer
Sam Kerner (Chrome)
2011/01/21 17:59:26
Done.
| |
| 67 // Tracked as crbug.com/70402 . | |
| 68 | |
| 63 scoped_ptr<DictionaryValue> prefs; | 69 scoped_ptr<DictionaryValue> prefs; |
| 64 | |
| 65 if (PathService::Get(base_path_key_, &base_path_)) { | 70 if (PathService::Get(base_path_key_, &base_path_)) { |
| 66 FilePath json_file; | 71 FilePath json_file; |
| 67 json_file = | 72 json_file = |
| 68 base_path_.Append(FILE_PATH_LITERAL("external_extensions.json")); | 73 base_path_.Append(FILE_PATH_LITERAL("external_extensions.json")); |
| 69 | 74 |
| 70 if (file_util::PathExists(json_file)) { | 75 if (file_util::PathExists(json_file)) { |
| 71 JSONFileValueSerializer serializer(json_file); | 76 JSONFileValueSerializer serializer(json_file); |
| 72 prefs.reset(ExtractPrefs(&serializer)); | 77 prefs.reset(ExtractPrefs(json_file, &serializer)); |
| 73 } | 78 } |
| 74 } | 79 } |
| 75 | 80 |
| 76 if (!prefs.get()) | 81 if (!prefs.get()) |
| 77 prefs.reset(new DictionaryValue()); | 82 prefs.reset(new DictionaryValue()); |
| 78 | 83 |
| 79 prefs_.reset(prefs.release()); | 84 prefs_.reset(prefs.release()); |
| 85 | |
| 86 // If we have any records to process, then we must have | |
| 87 // read the .json file. If we read the .json file, then | |
| 88 // we were able to set |base_path_|. | |
| 89 CHECK(!prefs_->empty() || !base_path_.empty()); | |
|
Sam Kerner (Chrome)
2011/01/21 15:47:01
New CHECK here.
| |
| 90 | |
| 80 BrowserThread::PostTask( | 91 BrowserThread::PostTask( |
| 81 BrowserThread::UI, FROM_HERE, | 92 BrowserThread::UI, FROM_HERE, |
| 82 NewRunnableMethod( | 93 NewRunnableMethod( |
| 83 this, | 94 this, |
| 84 &ExternalPrefExtensionLoader::LoadFinished)); | 95 &ExternalPrefExtensionLoader::LoadFinished)); |
| 85 } | 96 } |
| 86 | 97 |
| 87 ExternalTestingExtensionLoader::ExternalTestingExtensionLoader( | 98 ExternalTestingExtensionLoader::ExternalTestingExtensionLoader( |
| 88 const std::string& json_data, | 99 const std::string& json_data, |
| 89 const FilePath& fake_base_path) | 100 const FilePath& fake_base_path) |
| 90 : fake_base_path_(fake_base_path) { | 101 : fake_base_path_(fake_base_path) { |
| 91 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 102 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 92 JSONStringValueSerializer serializer(json_data); | 103 JSONStringValueSerializer serializer(json_data); |
| 93 testing_prefs_.reset(ExtractPrefs(&serializer)); | 104 FilePath fake_json_path = fake_base_path.AppendASCII("fake.json"); |
| 105 testing_prefs_.reset(ExtractPrefs(fake_json_path, &serializer)); | |
| 94 } | 106 } |
| 95 | 107 |
| 96 void ExternalTestingExtensionLoader::StartLoading() { | 108 void ExternalTestingExtensionLoader::StartLoading() { |
| 97 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 109 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 98 prefs_.reset(testing_prefs_->DeepCopy()); | 110 prefs_.reset(testing_prefs_->DeepCopy()); |
| 99 LoadFinished(); | 111 LoadFinished(); |
| 100 } | 112 } |
| 101 | 113 |
| 102 const FilePath ExternalTestingExtensionLoader::GetBaseCrxFilePath() { | 114 const FilePath ExternalTestingExtensionLoader::GetBaseCrxFilePath() { |
| 103 return fake_base_path_; | 115 return fake_base_path_; |
| 104 } | 116 } |
| OLD | NEW |