Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/test/chromedriver/chrome_launcher.h" | 5 #include "chrome/test/chromedriver/chrome_launcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 switches.SetSwitch("remote-debugging-port", base::IntToString(port)); | 121 switches.SetSwitch("remote-debugging-port", base::IntToString(port)); |
| 122 switches.SetSwitch("test-type", "webdriver"); | 122 switches.SetSwitch("test-type", "webdriver"); |
| 123 | 123 |
| 124 for (std::set<std::string>::const_iterator iter = | 124 for (std::set<std::string>::const_iterator iter = |
| 125 capabilities.exclude_switches.begin(); | 125 capabilities.exclude_switches.begin(); |
| 126 iter != capabilities.exclude_switches.end(); | 126 iter != capabilities.exclude_switches.end(); |
| 127 ++iter) { | 127 ++iter) { |
| 128 switches.RemoveSwitch(*iter); | 128 switches.RemoveSwitch(*iter); |
| 129 } | 129 } |
| 130 switches.SetFromSwitches(capabilities.switches); | 130 switches.SetFromSwitches(capabilities.switches); |
| 131 | 131 base::FilePath user_data_dir_path; |
| 132 if (!switches.HasSwitch("user-data-dir")) { | 132 if (!switches.HasSwitch("user-data-dir")) { |
| 133 command.AppendArg("data:,"); | 133 command.AppendArg("data:,"); |
| 134 if (!user_data_dir->CreateUniqueTempDir()) | 134 if (!user_data_dir->CreateUniqueTempDir()) |
| 135 return Status(kUnknownError, "cannot create temp dir for user data dir"); | 135 return Status(kUnknownError, "cannot create temp dir for user data dir"); |
| 136 switches.SetSwitch("user-data-dir", user_data_dir->path().value()); | 136 switches.SetSwitch("user-data-dir", user_data_dir->path().value()); |
| 137 Status status = internal::PrepareUserDataDir( | 137 user_data_dir_path = user_data_dir->path(); |
| 138 user_data_dir->path(), capabilities.prefs.get(), | 138 } else { |
| 139 capabilities.local_state.get()); | 139 user_data_dir_path = base::FilePath( |
| 140 if (status.IsError()) | 140 switches.GetSwitchValue("user-data-dir")); |
| 141 } | |
| 142 | |
| 143 Status status = internal::PrepareUserDataDir( | |
| 144 user_data_dir_path , capabilities.prefs.get(), | |
|
samuong
2014/10/16 20:23:40
I've just noticed that if you unconditionally call
| |
| 145 capabilities.local_state.get()); | |
| 146 if (status.IsError()) | |
| 141 return status; | 147 return status; |
|
samuong
2014/10/16 20:23:40
This is indented too far in.
| |
| 142 } | |
| 143 | 148 |
| 144 if (!extension_dir->CreateUniqueTempDir()) { | 149 if (!extension_dir->CreateUniqueTempDir()) { |
| 145 return Status(kUnknownError, | 150 return Status(kUnknownError, |
| 146 "cannot create temp dir for unpacking extensions"); | 151 "cannot create temp dir for unpacking extensions"); |
| 147 } | 152 } |
| 148 Status status = internal::ProcessExtensions(capabilities.extensions, | 153 status = internal::ProcessExtensions(capabilities.extensions, |
| 149 extension_dir->path(), | 154 extension_dir->path(), |
| 150 true, | 155 true, |
| 151 &switches, | 156 &switches, |
| 152 extension_bg_pages); | 157 extension_bg_pages); |
|
samuong
2014/10/16 20:23:40
Reindent lines 154-157 so that they line up with t
| |
| 153 if (status.IsError()) | 158 if (status.IsError()) |
| 154 return status; | 159 return status; |
| 155 switches.AppendToCommandLine(&command); | 160 switches.AppendToCommandLine(&command); |
| 156 *prepared_command = command; | 161 *prepared_command = command; |
| 157 return Status(kOk); | 162 return Status(kOk); |
| 158 } | 163 } |
| 159 | 164 |
| 160 Status WaitForDevToolsAndCheckVersion( | 165 Status WaitForDevToolsAndCheckVersion( |
| 161 const NetAddress& address, | 166 const NetAddress& address, |
| 162 URLRequestContextGetter* context_getter, | 167 URLRequestContextGetter* context_getter, |
| (...skipping 593 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 756 base::JSONWriter::Write(prefs, &prefs_str); | 761 base::JSONWriter::Write(prefs, &prefs_str); |
| 757 VLOG(0) << "Populating " << path.BaseName().value() | 762 VLOG(0) << "Populating " << path.BaseName().value() |
| 758 << " file: " << PrettyPrintValue(*prefs); | 763 << " file: " << PrettyPrintValue(*prefs); |
| 759 if (static_cast<int>(prefs_str.length()) != base::WriteFile( | 764 if (static_cast<int>(prefs_str.length()) != base::WriteFile( |
| 760 path, prefs_str.c_str(), prefs_str.length())) { | 765 path, prefs_str.c_str(), prefs_str.length())) { |
| 761 return Status(kUnknownError, "failed to write prefs file"); | 766 return Status(kUnknownError, "failed to write prefs file"); |
| 762 } | 767 } |
| 763 return Status(kOk); | 768 return Status(kOk); |
| 764 } | 769 } |
| 765 | 770 |
| 771 Status MergeExistPrefFile(const base::DictionaryValue* custom_prefs, | |
| 772 const base::FilePath& path, base::DictionaryValue* merge_prefs) { | |
| 773 | |
| 774 if (base::PathExists(path)) { // if exist, import it | |
| 775 std::string data_read; | |
| 776 base::ReadFileToString(path, &data_read); | |
| 777 // | |
| 778 int code; | |
| 779 std::string error_msg; | |
| 780 scoped_ptr<base::Value> template_value( | |
| 781 base::JSONReader::ReadAndReturnError(data_read, 0, &code, &error_msg)); | |
| 782 | |
| 783 base::DictionaryValue* prefs = NULL; | |
| 784 if (!template_value || !template_value->GetAsDictionary(&prefs)) { | |
| 785 return Status(kUnknownError, | |
| 786 "cannot parse default Preference: " + error_msg); | |
| 787 } | |
| 788 merge_prefs->MergeDictionary(prefs); | |
| 789 // overwrite if any custom prefs | |
| 790 if (custom_prefs) { | |
| 791 for (base::DictionaryValue::Iterator it(*custom_prefs); !it.IsAtEnd(); | |
| 792 it.Advance()) { | |
| 793 merge_prefs->Set(it.key(), it.value().DeepCopy()); | |
| 794 } | |
| 795 } | |
| 796 | |
| 797 } else { // nothing to read - copy user define data directly | |
| 798 if (custom_prefs) | |
| 799 merge_prefs->MergeDictionary(custom_prefs); | |
| 800 } | |
| 801 return Status(kOk); | |
| 802 } | |
| 803 | |
| 766 Status PrepareUserDataDir( | 804 Status PrepareUserDataDir( |
| 767 const base::FilePath& user_data_dir, | 805 const base::FilePath& user_data_dir, |
| 768 const base::DictionaryValue* custom_prefs, | 806 const base::DictionaryValue* custom_prefs, |
| 769 const base::DictionaryValue* custom_local_state) { | 807 const base::DictionaryValue* custom_local_state) { |
| 770 base::FilePath default_dir = | 808 base::FilePath default_dir = |
| 771 user_data_dir.AppendASCII(chrome::kInitialProfile); | 809 user_data_dir.AppendASCII(chrome::kInitialProfile); |
| 772 if (!base::CreateDirectory(default_dir)) | 810 if (!base::CreateDirectory(default_dir)) |
| 773 return Status(kUnknownError, "cannot create default profile directory"); | 811 return Status(kUnknownError, "cannot create default profile directory"); |
| 774 | 812 |
| 775 Status status = | 813 base::DictionaryValue merge_prefs; |
| 776 WritePrefsFile(kPreferences, | 814 // merge to existed Preferences |
| 777 custom_prefs, | 815 Status status = MergeExistPrefFile(custom_prefs, |
| 778 default_dir.Append(chrome::kPreferencesFilename)); | 816 default_dir.Append(chrome::kPreferencesFilename), |
| 817 &merge_prefs); | |
| 818 if (status.IsError()) | |
| 819 return status; | |
| 820 // merge to template | |
| 821 status = WritePrefsFile(kPreferences, | |
| 822 &merge_prefs, | |
| 823 default_dir.Append(chrome::kPreferencesFilename)); | |
| 779 if (status.IsError()) | 824 if (status.IsError()) |
| 780 return status; | 825 return status; |
| 781 | 826 |
| 782 status = WritePrefsFile(kLocalState, | 827 status = WritePrefsFile(kLocalState, |
| 783 custom_local_state, | 828 custom_local_state, |
| 784 user_data_dir.Append(chrome::kLocalStateFilename)); | 829 user_data_dir.Append(chrome::kLocalStateFilename)); |
| 785 if (status.IsError()) | 830 if (status.IsError()) |
| 786 return status; | 831 return status; |
| 787 | 832 |
| 788 // Write empty "First Run" file, otherwise Chrome will wipe the default | 833 // Write empty "First Run" file, otherwise Chrome will wipe the default |
| 789 // profile that was written. | 834 // profile that was written. |
| 790 if (base::WriteFile( | 835 if (base::WriteFile( |
| 791 user_data_dir.Append(chrome::kFirstRunSentinel), "", 0) != 0) { | 836 user_data_dir.Append(chrome::kFirstRunSentinel), "", 0) != 0) { |
| 792 return Status(kUnknownError, "failed to write first run file"); | 837 return Status(kUnknownError, "failed to write first run file"); |
| 793 } | 838 } |
| 794 return Status(kOk); | 839 return Status(kOk); |
| 795 } | 840 } |
| 796 | 841 |
| 797 } // namespace internal | 842 } // namespace internal |
| OLD | NEW |