Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: webkit/fileapi/syncable/syncable_context.cc

Issue 11090019: Add LocalFileSyncContext class which wires up profile-owned service and FileSystemContext(s) (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: addressed comments #2 Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 "webkit/fileapi/syncable/syncable_context.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/stl_util.h"
12 #include "base/task_runner_util.h"
13 #include "webkit/fileapi/file_system_context.h"
14 #include "webkit/fileapi/file_system_task_runners.h"
15 #include "webkit/fileapi/syncable/local_file_change_tracker.h"
16 #include "webkit/fileapi/syncable/syncable_context.h"
17
18 namespace fileapi {
19
20 SyncableContext::SyncableContext(
21 base::SingleThreadTaskRunner* ui_task_runner,
22 base::SingleThreadTaskRunner* io_task_runner)
23 : ui_task_runner_(ui_task_runner),
24 io_task_runner_(io_task_runner),
25 shutdown_(false) {
26 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
27 }
28
29 void SyncableContext::MaybeInitializeFileSystemContext(
30 const GURL& source_url,
31 FileSystemContext* file_system_context,
32 const StatusCallback& callback) {
33 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
34 if (ContainsKey(file_system_contexts_, file_system_context)) {
35 DCHECK(!ContainsKey(origin_to_contexts_, source_url) ||
36 origin_to_contexts_[source_url] == file_system_context);
37 origin_to_contexts_[source_url] = file_system_context;
38 // The context has been already initialized. Just dispatch the callback
39 // with SYNC_STATUS_OK.
40 ui_task_runner_->PostTask(FROM_HERE, base::Bind(callback, SYNC_STATUS_OK));
41 return;
42 }
43
44 StatusCallbackQueue& callback_queue =
45 pending_initialize_callbacks_[file_system_context];
46 callback_queue.push_back(callback);
47 if (callback_queue.size() > 1)
48 return;
49
50 io_task_runner_->PostTask(
51 FROM_HERE,
52 base::Bind(&SyncableContext::InitializeFileSystemContextOnIOThread,
53 this, source_url, make_scoped_refptr(file_system_context)));
54 }
55
56 void SyncableContext::ShutdownOnUIThread() {
57 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
58 shutdown_ = true;
59 io_task_runner_->PostTask(FROM_HERE,
60 base::Bind(&SyncableContext::ShutdownOnIOThread,
61 this));
62 }
63
64 SyncableContext::~SyncableContext() {
65 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
66 DCHECK(shutdown_);
67 }
68
69 void SyncableContext::ShutdownOnIOThread() {
70 DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
71 }
72
73 void SyncableContext::InitializeFileSystemContextOnIOThread(
74 const GURL& source_url,
75 FileSystemContext* file_system_context) {
76 DCHECK(io_task_runner_->RunsTasksOnCurrentThread());
77 DCHECK(file_system_context);
78 if (!file_system_context->change_tracker()) {
79 // Create and initialize LocalFileChangeTracker and call back this method
80 // later again.
81 scoped_ptr<LocalFileChangeTracker>* tracker_ptr(
82 new scoped_ptr<LocalFileChangeTracker>);
83 base::PostTaskAndReplyWithResult(
84 file_system_context->task_runners()->file_task_runner(),
85 FROM_HERE,
86 base::Bind(&SyncableContext::InitializeChangeTrackerOnFileThread, this,
87 tracker_ptr,
88 make_scoped_refptr(file_system_context)),
89 base::Bind(&SyncableContext::DidInitializeChangeTracker, this,
90 base::Owned(tracker_ptr),
91 source_url,
92 make_scoped_refptr(file_system_context)));
93 return;
94 }
95 file_system_context->set_syncable_context(this);
96 DidInitialize(source_url, file_system_context, SYNC_STATUS_OK);
97 }
98
99 SyncStatusCode SyncableContext::InitializeChangeTrackerOnFileThread(
100 scoped_ptr<LocalFileChangeTracker>* tracker_ptr,
101 FileSystemContext* file_system_context) {
102 DCHECK(file_system_context);
103 DCHECK(tracker_ptr);
104 tracker_ptr->reset(new LocalFileChangeTracker(
105 file_system_context->partition_path(),
106 file_system_context->task_runners()->file_task_runner()));
107 // TODO(kinuko,nhiroki): Uncomment this once http://crbug.com/154028 is fixed.
108 // return change_tracker->Initialize(file_system_context);
109 return SYNC_STATUS_OK;
110 }
111
112 void SyncableContext::DidInitializeChangeTracker(
113 scoped_ptr<LocalFileChangeTracker>* tracker_ptr,
114 const GURL& source_url,
115 FileSystemContext* file_system_context,
116 SyncStatusCode status) {
117 DCHECK(file_system_context);
118 if (status != SYNC_STATUS_OK) {
119 DidInitialize(source_url, file_system_context, status);
120 return;
121 }
122 file_system_context->SetLocalFileChangeTracker(tracker_ptr->Pass());
123 InitializeFileSystemContextOnIOThread(source_url, file_system_context);
124 }
125
126 void SyncableContext::DidInitialize(
127 const GURL& source_url,
128 FileSystemContext* file_system_context,
129 SyncStatusCode status) {
130 if (!ui_task_runner_->RunsTasksOnCurrentThread()) {
131 ui_task_runner_->PostTask(
132 FROM_HERE,
133 base::Bind(&SyncableContext::DidInitialize,
134 this, source_url,
135 make_scoped_refptr(file_system_context), status));
136 return;
137 }
138 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
139 DCHECK(!ContainsKey(file_system_contexts_, file_system_context));
140 DCHECK(ContainsKey(pending_initialize_callbacks_, file_system_context));
141 DCHECK(file_system_context->change_tracker());
142
143 file_system_contexts_.insert(file_system_context);
144
145 DCHECK(!ContainsKey(origin_to_contexts_, source_url));
146 origin_to_contexts_[source_url] = file_system_context;
147
148 StatusCallbackQueue& callback_queue =
149 pending_initialize_callbacks_[file_system_context];
150 for (StatusCallbackQueue::iterator iter = callback_queue.begin();
151 iter != callback_queue.end(); ++iter) {
152 ui_task_runner_->PostTask(FROM_HERE, base::Bind(*iter, status));
153 }
154 pending_initialize_callbacks_.erase(file_system_context);
155 }
156
157 } // namespace fileapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698