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_extension_loader.h" | 5 #include "chrome/browser/extensions/external_pref_extension_loader.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 "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
11 #include "base/json/json_string_value_serializer.h" | 11 #include "base/json/json_string_value_serializer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/path_service.h" | 14 #include "base/path_service.h" |
15 #include "chrome/browser/extensions/external_extension_util.h" | |
15 #include "chrome/common/chrome_paths.h" | 16 #include "chrome/common/chrome_paths.h" |
16 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
17 | 18 |
18 using content::BrowserThread; | 19 using content::BrowserThread; |
19 | 20 |
20 namespace { | |
21 | |
22 // Caller takes ownership of the returned dictionary. | |
23 DictionaryValue* ExtractPrefs(const FilePath& path, | |
24 base::ValueSerializer* serializer) { | |
25 std::string error_msg; | |
26 Value* extensions = serializer->Deserialize(NULL, &error_msg); | |
27 if (!extensions) { | |
28 LOG(WARNING) << "Unable to deserialize json data: " << error_msg | |
29 << " In file " << path.value() << " ."; | |
30 } else { | |
31 if (!extensions->IsType(Value::TYPE_DICTIONARY)) { | |
32 LOG(WARNING) << "Expected a JSON dictionary in file " | |
33 << path.value() << " ."; | |
34 } else { | |
35 return static_cast<DictionaryValue*>(extensions); | |
36 } | |
37 } | |
38 return new DictionaryValue; | |
39 } | |
40 | |
41 } // namespace | |
42 | |
43 ExternalPrefExtensionLoader::ExternalPrefExtensionLoader(int base_path_key, | 21 ExternalPrefExtensionLoader::ExternalPrefExtensionLoader(int base_path_key, |
44 Options options) | 22 Options options) |
45 : base_path_key_(base_path_key), | 23 : base_path_key_(base_path_key), |
46 options_(options){ | 24 options_(options){ |
47 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 25 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
48 } | 26 } |
49 | 27 |
50 const FilePath ExternalPrefExtensionLoader::GetBaseCrxFilePath() { | 28 const FilePath ExternalPrefExtensionLoader::GetBaseCrxFilePath() { |
51 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 29 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
52 | 30 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 } | 71 } |
94 #else | 72 #else |
95 // The only platform that uses this check is Mac OS. If you add one, | 73 // The only platform that uses this check is Mac OS. If you add one, |
96 // you need to implement file_util::VerifyPathControlledByAdmin() for | 74 // you need to implement file_util::VerifyPathControlledByAdmin() for |
97 // that platform. | 75 // that platform. |
98 NOTREACHED(); | 76 NOTREACHED(); |
99 #endif // defined(OS_MACOSX) | 77 #endif // defined(OS_MACOSX) |
100 } | 78 } |
101 | 79 |
102 JSONFileValueSerializer serializer(json_file); | 80 JSONFileValueSerializer serializer(json_file); |
103 DictionaryValue* parsed_json_prefs = ExtractPrefs(json_file, &serializer); | 81 DictionaryValue* |
104 return parsed_json_prefs; | 82 json_prefs = ExternalExtensionUtil::ExtractPrefs(json_file, &serializer); |
83 return json_prefs; | |
105 } | 84 } |
106 | 85 |
107 void ExternalPrefExtensionLoader::LoadOnFileThread() { | 86 void ExternalPrefExtensionLoader::LoadOnFileThread() { |
108 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 87 CHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
109 | 88 |
110 prefs_.reset(ReadJsonPrefsFile()); | 89 prefs_.reset(ReadJsonPrefsFile()); |
111 if (!prefs_.get()) | 90 if (!prefs_.get()) |
112 prefs_.reset(new DictionaryValue()); | 91 prefs_.reset(new DictionaryValue()); |
113 | 92 |
114 if (base_path_key_ == chrome::DIR_EXTERNAL_EXTENSIONS) { | 93 if (base_path_key_ == chrome::DIR_EXTERNAL_EXTENSIONS) { |
(...skipping 12 matching lines...) Expand all Loading... | |
127 base::Bind(&ExternalPrefExtensionLoader::LoadFinished, this)); | 106 base::Bind(&ExternalPrefExtensionLoader::LoadFinished, this)); |
128 } | 107 } |
129 | 108 |
130 ExternalTestingExtensionLoader::ExternalTestingExtensionLoader( | 109 ExternalTestingExtensionLoader::ExternalTestingExtensionLoader( |
131 const std::string& json_data, | 110 const std::string& json_data, |
132 const FilePath& fake_base_path) | 111 const FilePath& fake_base_path) |
133 : fake_base_path_(fake_base_path) { | 112 : fake_base_path_(fake_base_path) { |
134 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 113 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
135 JSONStringValueSerializer serializer(json_data); | 114 JSONStringValueSerializer serializer(json_data); |
136 FilePath fake_json_path = fake_base_path.AppendASCII("fake.json"); | 115 FilePath fake_json_path = fake_base_path.AppendASCII("fake.json"); |
137 testing_prefs_.reset(ExtractPrefs(fake_json_path, &serializer)); | 116 testing_prefs_.reset(ExternalExtensionUtil::ExtractPrefs(fake_json_path |
Sam Kerner (Chrome)
2012/04/13 19:35:13
Coma should be on previous line.
Alexandre Abreu
2012/04/13 21:52:38
Done.
| |
117 , &serializer)); | |
138 } | 118 } |
139 | 119 |
140 void ExternalTestingExtensionLoader::StartLoading() { | 120 void ExternalTestingExtensionLoader::StartLoading() { |
141 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 121 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
142 prefs_.reset(testing_prefs_->DeepCopy()); | 122 prefs_.reset(testing_prefs_->DeepCopy()); |
143 LoadFinished(); | 123 LoadFinished(); |
144 } | 124 } |
145 | 125 |
146 ExternalTestingExtensionLoader::~ExternalTestingExtensionLoader() {} | 126 ExternalTestingExtensionLoader::~ExternalTestingExtensionLoader() {} |
147 | 127 |
148 const FilePath ExternalTestingExtensionLoader::GetBaseCrxFilePath() { | 128 const FilePath ExternalTestingExtensionLoader::GetBaseCrxFilePath() { |
149 return fake_base_path_; | 129 return fake_base_path_; |
150 } | 130 } |
OLD | NEW |