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/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 "testing/gtest/include/gtest/gtest.h"
24 namespace extensions {
26 SyncSetupHelper::SyncSetupHelper() {}
28 SyncSetupHelper::~SyncSetupHelper() {}
30 bool SyncSetupHelper::InitializeSync(Profile* profile) {
33 ProfileSyncServiceHarness::Create(profile_, username_, password_));
35 if (client_->service()->IsSyncEnabledAndLoggedIn())
38 if (!client_->SetupSync())
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.
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 = base::ReadFileToString(password_file, &file_contents);
56 << password_file.value() << "\" does not exist.";
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())
68 username_ = tokens[0];
69 password_ = tokens[1];
70 client_id_ = tokens[2];
71 client_secret_ = tokens[3];
72 refresh_token_ = tokens[4];
76 bool SyncSetupHelper::AwaitQuiescence() {
77 std::vector<ProfileSyncServiceHarness*> clients;
78 clients.push_back(client_.get());
79 return ProfileSyncServiceHarness::AwaitQuiescence(clients);
82 } // namespace extensions