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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/extensions/api/push_messaging/sync_setup_helper.h" 6 7 #include <vector> 8 9 #include "base/file_util.h" 10 #include "base/strings/string_util.h" 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 12 #include "chrome/browser/browser_process.h" 13 #include "chrome/browser/history/history_service_factory.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile_manager.h" 16 #include "chrome/browser/search_engines/template_url_service_factory.h" 17 #include "chrome/browser/sync/profile_sync_service_factory.h" 18 #include "chrome/browser/sync/profile_sync_service_harness.h" 19 #include "chrome/browser/ui/browser.h" 20 #include "chrome/common/chrome_paths.h" 21 #include "chrome/test/base/ui_test_utils.h" 22 #include "testing/gtest/include/gtest/gtest.h" 23 24 namespace extensions { 25 26 SyncSetupHelper::SyncSetupHelper() {} 27 28 SyncSetupHelper::~SyncSetupHelper() {} 29 30 bool SyncSetupHelper::InitializeSync(Profile* profile) { 31 profile_ = profile; 32 client_.reset( 33 ProfileSyncServiceHarness::Create(profile_, username_, password_)); 34 35 if (client_->service()->IsSyncEnabledAndLoggedIn()) 36 return true; 37 38 if (!client_->SetupSync()) 39 return false; 40 41 // Because clients may modify sync data as part of startup (for example local 42 // session-releated data is rewritten), we need to ensure all startup-based 43 // changes have propagated between the clients. 44 // This could take several seconds. 45 AwaitQuiescence(); 46 return true; 47 } 48 49 // Read the sync signin credentials from a file on the machine. 50 bool SyncSetupHelper::ReadPasswordFile(const base::FilePath& password_file) { 51 // TODO(dcheng): Convert format of config file to JSON. 52 std::string file_contents; 53 bool success = file_util::ReadFileToString(password_file, &file_contents); 54 EXPECT_TRUE(success) 55 << "Password file \"" 56 << password_file.value() << "\" does not exist."; 57 if (!success) 58 return false; 59 60 std::vector<std::string> tokens; 61 std::string delimiters = "\r\n"; 62 Tokenize(file_contents, delimiters, &tokens); 63 EXPECT_EQ(5U, tokens.size()) << "Password file \"" 64 << password_file.value() 65 << "\" must contain exactly five lines of text."; 66 if (5U != tokens.size()) 67 return false; 68 username_ = tokens[0]; 69 password_ = tokens[1]; 70 client_id_ = tokens[2]; 71 client_secret_ = tokens[3]; 72 refresh_token_ = tokens[4]; 73 return true; 74 } 75 76 bool SyncSetupHelper::AwaitQuiescence() { 77 std::vector<ProfileSyncServiceHarness*> clients; 78 clients.push_back(client_.get()); 79 return ProfileSyncServiceHarness::AwaitQuiescence(clients); 80 } 81 82 } // namespace extensions 83