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.
5 #include "chrome/browser/extensions/api/push_messaging/sync_setup_helper.h"
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.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"
25 namespace extensions {
27 SyncSetupHelper::SyncSetupHelper() {}
29 SyncSetupHelper::~SyncSetupHelper() {}
31 bool SyncSetupHelper::InitializeSync(Profile* profile) {
34 ProfileSyncServiceHarness::Create(profile_, username_, password_));
36 if (client_->service()->IsSyncEnabledAndLoggedIn())
39 if (!client_->SetupSync())
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.
50 // Read the sync signin credentials from a file on the machine.
51 bool 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);
57 << password_file.value() << "\" does not exist.";
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())
69 username_ = tokens[0];
70 password_ = tokens[1];
71 client_id_ = tokens[2];
72 client_secret_ = tokens[3];
73 refresh_token_ = tokens[4];
77 bool SyncSetupHelper::AwaitQuiescence() {
78 std::vector<ProfileSyncServiceHarness*> clients;
79 clients.push_back(client_.get());
80 return ProfileSyncServiceHarness::AwaitQuiescence(clients);
83 } // namespace extensions