Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1300)

Side by Side Diff: chrome/test/chromedriver/chrome_launcher.cc

Issue 613163004: [chromedriver] setting browser default download directory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add test testDownloadDirectoryOverridesExistingPreferences Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 userDataDirPath;
samuong 2014/10/15 22:08:17 For local variable names in C++, we use underscore
132 if (!switches.HasSwitch("user-data-dir")) { 132 if (!switches.HasSwitch("user-data-dir")) {
133 command.AppendArg("data:,");
134 if (!user_data_dir->CreateUniqueTempDir()) 133 if (!user_data_dir->CreateUniqueTempDir())
135 return Status(kUnknownError, "cannot create temp dir for user data dir"); 134 return Status(kUnknownError, "cannot create temp dir for user data dir");
136 switches.SetSwitch("user-data-dir", user_data_dir->path().value()); 135 switches.SetSwitch("user-data-dir", user_data_dir->path().value());
137 Status status = internal::PrepareUserDataDir( 136 userDataDirPath = user_data_dir->path();
138 user_data_dir->path(), capabilities.prefs.get(), 137 } else {
139 capabilities.local_state.get()); 138 userDataDirPath = base::FilePath(switches.GetSwitchValue("user-data-dir"));
140 if (status.IsError()) 139 }
140 //
samuong 2014/10/15 22:08:17 More style nitpicking: don't use empty comments li
141 command.AppendArg("data:,");
samuong 2014/10/15 22:08:17 Existing user data directories might have a home p
142 Status status = internal::PrepareUserDataDir(
143 userDataDirPath , capabilities.prefs.get(),
144 capabilities.local_state.get());
145 if (status.IsError())
141 return status; 146 return status;
142 } 147 //
143
144 if (!extension_dir->CreateUniqueTempDir()) { 148 if (!extension_dir->CreateUniqueTempDir()) {
145 return Status(kUnknownError, 149 return Status(kUnknownError,
146 "cannot create temp dir for unpacking extensions"); 150 "cannot create temp dir for unpacking extensions");
147 } 151 }
148 Status status = internal::ProcessExtensions(capabilities.extensions, 152 status = internal::ProcessExtensions(capabilities.extensions,
149 extension_dir->path(), 153 extension_dir->path(),
150 true, 154 true,
151 &switches, 155 &switches,
152 extension_bg_pages); 156 extension_bg_pages);
153 if (status.IsError()) 157 if (status.IsError())
154 return status; 158 return status;
155 switches.AppendToCommandLine(&command); 159 switches.AppendToCommandLine(&command);
156 *prepared_command = command; 160 *prepared_command = command;
157 return Status(kOk); 161 return Status(kOk);
158 } 162 }
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
755 std::string prefs_str; 759 std::string prefs_str;
756 base::JSONWriter::Write(prefs, &prefs_str); 760 base::JSONWriter::Write(prefs, &prefs_str);
757 VLOG(0) << "Populating " << path.BaseName().value() 761 VLOG(0) << "Populating " << path.BaseName().value()
758 << " file: " << PrettyPrintValue(*prefs); 762 << " file: " << PrettyPrintValue(*prefs);
759 if (static_cast<int>(prefs_str.length()) != base::WriteFile( 763 if (static_cast<int>(prefs_str.length()) != base::WriteFile(
760 path, prefs_str.c_str(), prefs_str.length())) { 764 path, prefs_str.c_str(), prefs_str.length())) {
761 return Status(kUnknownError, "failed to write prefs file"); 765 return Status(kUnknownError, "failed to write prefs file");
762 } 766 }
763 return Status(kOk); 767 return Status(kOk);
764 } 768 }
769 //
770 Status mergeExistPrefFile(const base::DictionaryValue* custom_prefs,
samuong 2014/10/15 22:08:17 For function names in C++ like this, we use CamelC
771 const base::FilePath& path, base::DictionaryValue* mergePrefs) {
765 772
773 if (base::PathExists(path)) { // if exist, import it
774 std::string data_read;
775 base::ReadFileToString(path, &data_read);
776 //
777 int code;
778 std::string error_msg;
779 scoped_ptr<base::Value> template_value(
780 base::JSONReader::ReadAndReturnError(data_read, 0, &code,
781 &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 mergePrefs->MergeDictionary(prefs);
789 // overwrite if any custom prefs
790 if (custom_prefs) {
791 for (base::DictionaryValue::Iterator it(*custom_prefs);
792 !it.IsAtEnd(); it.Advance()) {
793 mergePrefs->Set(it.key(), it.value().DeepCopy());
794 }
795 }
796
797 } else { // nothing to read - copy user define data directly
798 if (custom_prefs)
799 mergePrefs->MergeDictionary(custom_prefs);
800 }
801
802 return Status(kOk);
803 }
804
805 //
766 Status PrepareUserDataDir( 806 Status PrepareUserDataDir(
767 const base::FilePath& user_data_dir, 807 const base::FilePath& user_data_dir,
768 const base::DictionaryValue* custom_prefs, 808 const base::DictionaryValue* custom_prefs,
769 const base::DictionaryValue* custom_local_state) { 809 const base::DictionaryValue* custom_local_state) {
770 base::FilePath default_dir = 810 base::FilePath default_dir =
771 user_data_dir.AppendASCII(chrome::kInitialProfile); 811 user_data_dir.AppendASCII(chrome::kInitialProfile);
772 if (!base::CreateDirectory(default_dir)) 812 if (!base::CreateDirectory(default_dir))
773 return Status(kUnknownError, "cannot create default profile directory"); 813 return Status(kUnknownError, "cannot create default profile directory");
774 814 //
775 Status status = 815 base::DictionaryValue mergePrefs;
816 // merge to existed Preferences
817 Status status = mergeExistPrefFile(custom_prefs,
818 default_dir.Append(chrome::kPreferencesFilename),
819 &mergePrefs);
820 if (status.IsError())
821 return status;
822 // merge to template
823 status =
776 WritePrefsFile(kPreferences, 824 WritePrefsFile(kPreferences,
777 custom_prefs, 825 &mergePrefs,
778 default_dir.Append(chrome::kPreferencesFilename)); 826 default_dir.Append(chrome::kPreferencesFilename));
779 if (status.IsError()) 827 if (status.IsError())
780 return status; 828 return status;
samuong 2014/10/15 22:08:17 C++ code should be indented with two spaces, not f
781 829
782 status = WritePrefsFile(kLocalState, 830 status = WritePrefsFile(kLocalState,
783 custom_local_state, 831 custom_local_state,
784 user_data_dir.Append(chrome::kLocalStateFilename)); 832 user_data_dir.Append(chrome::kLocalStateFilename));
785 if (status.IsError()) 833 if (status.IsError())
786 return status; 834 return status;
787 835
788 // Write empty "First Run" file, otherwise Chrome will wipe the default 836 // Write empty "First Run" file, otherwise Chrome will wipe the default
789 // profile that was written. 837 // profile that was written.
790 if (base::WriteFile( 838 if (base::WriteFile(
791 user_data_dir.Append(chrome::kFirstRunSentinel), "", 0) != 0) { 839 user_data_dir.Append(chrome::kFirstRunSentinel), "", 0) != 0) {
792 return Status(kUnknownError, "failed to write first run file"); 840 return Status(kUnknownError, "failed to write first run file");
793 } 841 }
794 return Status(kOk); 842 return Status(kOk);
795 } 843 }
796 844
797 } // namespace internal 845 } // namespace internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698