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