Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / components / suggestions / suggestions_service.cc
index db1e3fc..b12aae1 100644 (file)
@@ -156,8 +156,25 @@ bool SuggestionsService::IsControlGroup() {
 }
 
 void SuggestionsService::FetchSuggestionsData(
+    SyncState sync_state,
     SuggestionsService::ResponseCallback callback) {
   DCHECK(thread_checker_.CalledOnValidThread());
+  if (sync_state == NOT_INITIALIZED_ENABLED) {
+    // Sync is not initialized yet, but enabled. Serve previously cached
+    // suggestions if available.
+    waiting_requestors_.push_back(callback);
+    ServeFromCache();
+    return;
+  } else if (sync_state == SYNC_OR_HISTORY_SYNC_DISABLED) {
+    // Cancel any ongoing request (and the timeout closure). We must no longer
+    // interact with the server.
+    pending_request_.reset(NULL);
+    pending_timeout_closure_.reset(NULL);
+    suggestions_store_->ClearSuggestions();
+    callback.Run(SuggestionsProfile());
+    DispatchRequestsAndClear(SuggestionsProfile(), &waiting_requestors_);
+    return;
+  }
 
   FetchSuggestionsDataNoTimeout(callback);
 
@@ -170,21 +187,6 @@ void SuggestionsService::FetchSuggestionsData(
       base::TimeDelta::FromMilliseconds(request_timeout_ms_));
 }
 
-void SuggestionsService::FetchSuggestionsDataNoTimeout(
-    SuggestionsService::ResponseCallback callback) {
-  DCHECK(thread_checker_.CalledOnValidThread());
-  if (pending_request_.get()) {
-    // Request already exists, so just add requestor to queue.
-    waiting_requestors_.push_back(callback);
-    return;
-  }
-
-  // Form new request.
-  DCHECK(waiting_requestors_.empty());
-  waiting_requestors_.push_back(callback);
-  IssueRequest(suggestions_url_);
-}
-
 void SuggestionsService::GetPageThumbnail(
     const GURL& url,
     base::Callback<void(const GURL&, const SkBitmap*)> callback) {
@@ -235,6 +237,33 @@ void SuggestionsService::RegisterProfilePrefs(
   BlacklistStore::RegisterProfilePrefs(registry);
 }
 
+void SuggestionsService::SetDefaultExpiryTimestamp(
+    SuggestionsProfile* suggestions, int64 default_timestamp_usec) {
+  for (int i = 0; i < suggestions->suggestions_size(); ++i) {
+    ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i);
+    // Do not set expiry if the server has already provided a more specific
+    // expiry time for this suggestion.
+    if (!suggestion->has_expiry_ts()) {
+      suggestion->set_expiry_ts(default_timestamp_usec);
+    }
+  }
+}
+
+void SuggestionsService::FetchSuggestionsDataNoTimeout(
+    SuggestionsService::ResponseCallback callback) {
+  DCHECK(thread_checker_.CalledOnValidThread());
+  if (pending_request_.get()) {
+    // Request already exists, so just add requestor to queue.
+    waiting_requestors_.push_back(callback);
+    return;
+  }
+
+  // Form new request.
+  DCHECK(waiting_requestors_.empty());
+  waiting_requestors_.push_back(callback);
+  IssueRequest(suggestions_url_);
+}
+
 void SuggestionsService::IssueRequest(const GURL& url) {
   pending_request_.reset(CreateSuggestionsRequest(url));
   pending_request_->Start();
@@ -330,18 +359,6 @@ void SuggestionsService::OnURLFetchComplete(const net::URLFetcher* source) {
   ScheduleBlacklistUpload(true);
 }
 
-void SuggestionsService::SetDefaultExpiryTimestamp(
-    SuggestionsProfile* suggestions, int64 default_timestamp_usec) {
-  for (int i = 0; i < suggestions->suggestions_size(); ++i) {
-    ChromeSuggestion* suggestion = suggestions->mutable_suggestions(i);
-    // Do not set expiry if the server has already provided a more specific
-    // expiry time for this suggestion.
-    if (!suggestion->has_expiry_ts()) {
-      suggestion->set_expiry_ts(default_timestamp_usec);
-    }
-  }
-}
-
 void SuggestionsService::Shutdown() {
   // Cancel pending request and timeout closure, then serve existing requestors
   // from cache.