| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 #if defined(BROWSER_SYNC) | 5 #if defined(BROWSER_SYNC) |
| 6 | 6 |
| 7 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ | 7 #ifndef CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ |
| 8 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ | 8 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ |
| 9 | 9 |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/file_path.h" | 12 #include "base/file_path.h" |
| 13 #include "base/lock.h" | 13 #include "base/lock.h" |
| 14 #include "base/message_loop.h" | 14 #include "base/message_loop.h" |
| 15 #include "base/ref_counted.h" | 15 #include "base/ref_counted.h" |
| 16 #include "base/thread.h" | 16 #include "base/thread.h" |
| 17 #include "base/timer.h" | 17 #include "base/timer.h" |
| 18 #include "chrome/browser/sync/auth_error_state.h" | 18 #include "chrome/browser/sync/auth_error_state.h" |
| 19 #include "chrome/browser/sync/engine/syncapi.h" | 19 #include "chrome/browser/sync/engine/syncapi.h" |
| 20 #include "chrome/browser/sync/glue/bookmark_model_worker.h" | 20 #include "chrome/browser/sync/glue/bookmark_model_worker.h" |
| 21 |
| 21 #include "googleurl/src/gurl.h" | 22 #include "googleurl/src/gurl.h" |
| 22 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
| 23 | 24 |
| 24 namespace browser_sync { | 25 namespace browser_sync { |
| 25 | 26 |
| 26 // SyncFrontend is the interface used by SyncBackendHost to communicate with | 27 // SyncFrontend is the interface used by SyncBackendHost to communicate with |
| 27 // the entity that created it and, presumably, is interested in sync-related | 28 // the entity that created it and, presumably, is interested in sync-related |
| 28 // activity. | 29 // activity. |
| 29 // NOTE: All methods will be invoked by a SyncBackendHost on the same thread | 30 // NOTE: All methods will be invoked by a SyncBackendHost on the same thread |
| 30 // used to create that SyncBackendHost. | 31 // used to create that SyncBackendHost. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 } | 111 } |
| 111 | 112 |
| 112 // Returns the authenticated username of the sync user, or empty if none | 113 // Returns the authenticated username of the sync user, or empty if none |
| 113 // exists. It will only exist if the authentication service provider (e.g | 114 // exists. It will only exist if the authentication service provider (e.g |
| 114 // GAIA) has confirmed the username is authentic. | 115 // GAIA) has confirmed the username is authentic. |
| 115 string16 GetAuthenticatedUsername() const; | 116 string16 GetAuthenticatedUsername() const; |
| 116 | 117 |
| 117 #if defined(UNIT_TEST) | 118 #if defined(UNIT_TEST) |
| 118 // Called from unit test to bypass authentication and initialize the syncapi | 119 // Called from unit test to bypass authentication and initialize the syncapi |
| 119 // to a state suitable for testing but not production. | 120 // to a state suitable for testing but not production. |
| 120 void InitializeForTestMode(const std::wstring& test_user) { | 121 void InitializeForTestMode(const std::wstring& test_user, |
| 122 sync_api::HttpPostProviderFactory* factory, |
| 123 sync_api::HttpPostProviderFactory* auth_factory) { |
| 121 if (!core_thread_.Start()) | 124 if (!core_thread_.Start()) |
| 122 return; | 125 return; |
| 123 bookmark_model_worker_ = new BookmarkModelWorker(frontend_loop_); | 126 bookmark_model_worker_ = new BookmarkModelWorker(frontend_loop_); |
| 127 |
| 124 core_thread_.message_loop()->PostTask(FROM_HERE, | 128 core_thread_.message_loop()->PostTask(FROM_HERE, |
| 125 NewRunnableMethod(core_.get(), | 129 NewRunnableMethod(core_.get(), |
| 126 &SyncBackendHost::Core::DoInitializeForTest, | 130 &SyncBackendHost::Core::DoInitializeForTest, |
| 127 bookmark_model_worker_, | 131 bookmark_model_worker_, |
| 128 test_user)); | 132 test_user, |
| 133 factory, |
| 134 auth_factory)); |
| 129 } | 135 } |
| 130 #endif | 136 #endif |
| 131 | 137 |
| 132 private: | 138 private: |
| 133 // The real guts of SyncBackendHost, to keep the public client API clean. | 139 // The real guts of SyncBackendHost, to keep the public client API clean. |
| 134 class Core : public base::RefCountedThreadSafe<SyncBackendHost::Core>, | 140 class Core : public base::RefCountedThreadSafe<SyncBackendHost::Core>, |
| 135 public sync_api::SyncManager::Observer { | 141 public sync_api::SyncManager::Observer { |
| 136 public: | 142 public: |
| 137 explicit Core(SyncBackendHost* backend); | 143 explicit Core(SyncBackendHost* backend); |
| 138 | 144 |
| 139 // Note: This destructor should *always* be called from the thread that | |
| 140 // created it, and *always* after core_thread_ has exited. The syncapi | |
| 141 // watches thread exit events and keeps pointers to objects this dtor will | |
| 142 // destroy, so this ordering is important. | |
| 143 ~Core(); | |
| 144 | |
| 145 // SyncManager::Observer implementation. The Core just acts like an air | 145 // SyncManager::Observer implementation. The Core just acts like an air |
| 146 // traffic controller here, forwarding incoming messages to appropriate | 146 // traffic controller here, forwarding incoming messages to appropriate |
| 147 // landing threads. | 147 // landing threads. |
| 148 virtual void OnChangesApplied( | 148 virtual void OnChangesApplied( |
| 149 const sync_api::BaseTransaction* trans, | 149 const sync_api::BaseTransaction* trans, |
| 150 const sync_api::SyncManager::ChangeRecord* changes, | 150 const sync_api::SyncManager::ChangeRecord* changes, |
| 151 int change_count); | 151 int change_count); |
| 152 virtual void OnSyncCycleCompleted(); | 152 virtual void OnSyncCycleCompleted(); |
| 153 virtual void OnInitializationComplete(); | 153 virtual void OnInitializationComplete(); |
| 154 virtual void OnAuthProblem( | 154 virtual void OnAuthProblem( |
| 155 sync_api::SyncManager::AuthProblem auth_problem); | 155 sync_api::SyncManager::AuthProblem auth_problem); |
| 156 | 156 |
| 157 // Note: | 157 // Note: |
| 158 // | 158 // |
| 159 // The Do* methods are the various entry points from our SyncBackendHost. | 159 // The Do* methods are the various entry points from our SyncBackendHost. |
| 160 // It calls us on a dedicated thread to actually perform synchronous | 160 // It calls us on a dedicated thread to actually perform synchronous |
| 161 // (and potentially blocking) syncapi operations. | 161 // (and potentially blocking) syncapi operations. |
| 162 // | 162 // |
| 163 // Called on the SyncBackendHost core_thread_ to perform initialization | 163 // Called on the SyncBackendHost core_thread_ to perform initialization |
| 164 // of the syncapi on behalf of SyncBackendHost::Initialize. | 164 // of the syncapi on behalf of SyncBackendHost::Initialize. |
| 165 void DoInitialize(const GURL& service_url, | 165 void DoInitialize(const GURL& service_url, |
| 166 BookmarkModelWorker* bookmark_model_worker, | 166 BookmarkModelWorker* bookmark_model_worker, |
| 167 bool attempt_last_user_authentication); | 167 bool attempt_last_user_authentication, |
| 168 sync_api::HttpPostProviderFactory* http_bridge_factory, |
| 169 sync_api::HttpPostProviderFactory* auth_http_bridge_factory); |
| 168 | 170 |
| 169 // Called on our SyncBackendHost's core_thread_ to perform authentication | 171 // Called on our SyncBackendHost's core_thread_ to perform authentication |
| 170 // on behalf of SyncBackendHost::Authenticate. | 172 // on behalf of SyncBackendHost::Authenticate. |
| 171 void DoAuthenticate(const std::string& username, | 173 void DoAuthenticate(const std::string& username, |
| 172 const std::string& password); | 174 const std::string& password); |
| 173 | 175 |
| 174 // The shutdown order is a bit complicated: | 176 // The shutdown order is a bit complicated: |
| 175 // 1) From |core_thread_|, invoke the syncapi Shutdown call to do a final | 177 // 1) From |core_thread_|, invoke the syncapi Shutdown call to do a final |
| 176 // SaveChanges, close sqlite handles, and halt the syncer thread (which | 178 // SaveChanges, close sqlite handles, and halt the syncer thread (which |
| 177 // could potentially block for 1 minute). | 179 // could potentially block for 1 minute). |
| 178 // 2) Then, from |frontend_loop_|, halt the core_thread_. This causes | 180 // 2) Then, from |frontend_loop_|, halt the core_thread_. This causes |
| 179 // syncapi thread-exit handlers to run and make use of cached pointers to | 181 // syncapi thread-exit handlers to run and make use of cached pointers to |
| 180 // various components owned implicitly by us. | 182 // various components owned implicitly by us. |
| 181 // 3) Destroy this Core. That will delete syncapi components in a safe order | 183 // 3) Destroy this Core. That will delete syncapi components in a safe order |
| 182 // because the thread that was using them has exited (in step 2). | 184 // because the thread that was using them has exited (in step 2). |
| 183 void DoShutdown(bool stopping_sync); | 185 void DoShutdown(bool stopping_sync); |
| 184 | 186 |
| 185 // Set the base request context to use when making HTTP calls. | 187 // Set the base request context to use when making HTTP calls. |
| 186 // This method will add a reference to the context to persist it | 188 // This method will add a reference to the context to persist it |
| 187 // on the IO thread. Must be removed from IO thread. | 189 // on the IO thread. Must be removed from IO thread. |
| 188 // TODO(chron): After HttpNetworkSession reorganization happens, try | |
| 189 // getting HttpBridgeFactory to initialize earlier. | |
| 190 void SetBaseRequestContext(URLRequestContext* request_context); | |
| 191 | 190 |
| 192 sync_api::SyncManager* syncapi() { return syncapi_.get(); } | 191 sync_api::SyncManager* syncapi() { return syncapi_.get(); } |
| 193 | 192 |
| 194 #if defined(UNIT_TEST) | 193 #if defined(UNIT_TEST) |
| 195 // Special form of initialization that does not try and authenticate the | 194 // Special form of initialization that does not try and authenticate the |
| 196 // last known user (since it will fail in test mode) and does some extra | 195 // last known user (since it will fail in test mode) and does some extra |
| 197 // setup to nudge the syncapi into a useable state. | 196 // setup to nudge the syncapi into a useable state. |
| 198 void DoInitializeForTest(BookmarkModelWorker* bookmark_model_worker, | 197 void DoInitializeForTest(BookmarkModelWorker* bookmark_model_worker, |
| 199 const std::wstring& test_user) { | 198 const std::wstring& test_user, |
| 200 DoInitialize(GURL(), bookmark_model_worker, false); | 199 sync_api::HttpPostProviderFactory* factory, |
| 200 sync_api::HttpPostProviderFactory* auth_factory) { |
| 201 DoInitialize(GURL(), bookmark_model_worker, false, factory, |
| 202 auth_factory); |
| 201 syncapi_->SetupForTestMode(WideToUTF16(test_user).c_str()); | 203 syncapi_->SetupForTestMode(WideToUTF16(test_user).c_str()); |
| 202 } | 204 } |
| 203 #endif | 205 #endif |
| 204 | 206 |
| 205 private: | 207 private: |
| 206 // FrontendNotification defines parameters for NotifyFrontend. Each enum | 208 // FrontendNotification defines parameters for NotifyFrontend. Each enum |
| 207 // value corresponds to the one SyncFrontend interface method that | 209 // value corresponds to the one SyncFrontend interface method that |
| 208 // NotifyFrontend should invoke. | 210 // NotifyFrontend should invoke. |
| 209 enum FrontendNotification { | 211 enum FrontendNotification { |
| 210 INITIALIZED, // OnBackendInitialized. | 212 INITIALIZED, // OnBackendInitialized. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 234 // initialization. | 236 // initialization. |
| 235 void SaveChanges(); | 237 void SaveChanges(); |
| 236 | 238 |
| 237 // Dispatched to from HandleAuthErrorEventOnCoreLoop to handle updating | 239 // Dispatched to from HandleAuthErrorEventOnCoreLoop to handle updating |
| 238 // frontend UI components. | 240 // frontend UI components. |
| 239 void HandleAuthErrorEventOnFrontendLoop(AuthErrorState new_auth_error); | 241 void HandleAuthErrorEventOnFrontendLoop(AuthErrorState new_auth_error); |
| 240 | 242 |
| 241 // Our parent SyncBackendHost | 243 // Our parent SyncBackendHost |
| 242 SyncBackendHost* host_; | 244 SyncBackendHost* host_; |
| 243 | 245 |
| 244 // Our request context that we have to keep a ref to. | |
| 245 // Contains session data. | |
| 246 URLRequestContext* base_request_context_; | |
| 247 | |
| 248 // The timer used to periodically call SaveChanges. | 246 // The timer used to periodically call SaveChanges. |
| 249 base::RepeatingTimer<Core> save_changes_timer_; | 247 base::RepeatingTimer<Core> save_changes_timer_; |
| 250 | 248 |
| 251 // The top-level syncapi entry point. | 249 // The top-level syncapi entry point. |
| 252 scoped_ptr<sync_api::SyncManager> syncapi_; | 250 scoped_ptr<sync_api::SyncManager> syncapi_; |
| 253 | 251 |
| 254 DISALLOW_COPY_AND_ASSIGN(Core); | 252 DISALLOW_COPY_AND_ASSIGN(Core); |
| 255 }; | 253 }; |
| 256 | 254 |
| 257 // A thread we dedicate for use by our Core to perform initialization, | 255 // A thread we dedicate for use by our Core to perform initialization, |
| (...skipping 26 matching lines...) Expand all Loading... |
| 284 // UI-thread cache of the last AuthErrorState received from syncapi. | 282 // UI-thread cache of the last AuthErrorState received from syncapi. |
| 285 AuthErrorState last_auth_error_; | 283 AuthErrorState last_auth_error_; |
| 286 | 284 |
| 287 DISALLOW_COPY_AND_ASSIGN(SyncBackendHost); | 285 DISALLOW_COPY_AND_ASSIGN(SyncBackendHost); |
| 288 }; | 286 }; |
| 289 | 287 |
| 290 } // namespace browser_sync | 288 } // namespace browser_sync |
| 291 | 289 |
| 292 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ | 290 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_HOST_H_ |
| 293 #endif // defined(BROWSER_SYNC) | 291 #endif // defined(BROWSER_SYNC) |
| OLD | NEW |