Home | History | Annotate | Download | only in internal_api
      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 "sync/internal_api/js_sync_manager_observer.h"
      6 
      7 #include <cstddef>
      8 
      9 #include "base/location.h"
     10 #include "base/logging.h"
     11 #include "base/strings/string_number_conversions.h"
     12 #include "base/values.h"
     13 #include "sync/internal_api/public/base/model_type.h"
     14 #include "sync/internal_api/public/change_record.h"
     15 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
     16 #include "sync/internal_api/public/util/sync_string_conversions.h"
     17 #include "sync/js/js_arg_list.h"
     18 #include "sync/js/js_event_details.h"
     19 #include "sync/js/js_event_handler.h"
     20 
     21 namespace syncer {
     22 
     23 JsSyncManagerObserver::JsSyncManagerObserver() {}
     24 
     25 JsSyncManagerObserver::~JsSyncManagerObserver() {}
     26 
     27 void JsSyncManagerObserver::SetJsEventHandler(
     28     const WeakHandle<JsEventHandler>& event_handler) {
     29   event_handler_ = event_handler;
     30 }
     31 
     32 void JsSyncManagerObserver::OnSyncCycleCompleted(
     33     const sessions::SyncSessionSnapshot& snapshot) {
     34   if (!event_handler_.IsInitialized()) {
     35     return;
     36   }
     37   base::DictionaryValue details;
     38   details.Set("snapshot", snapshot.ToValue());
     39   HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details));
     40 }
     41 
     42 void JsSyncManagerObserver::OnConnectionStatusChange(ConnectionStatus status) {
     43   if (!event_handler_.IsInitialized()) {
     44     return;
     45   }
     46   base::DictionaryValue details;
     47   details.SetString("status", ConnectionStatusToString(status));
     48   HandleJsEvent(FROM_HERE,
     49                 "onConnectionStatusChange", JsEventDetails(&details));
     50 }
     51 
     52 void JsSyncManagerObserver::OnActionableError(
     53     const SyncProtocolError& sync_error) {
     54   if (!event_handler_.IsInitialized()) {
     55     return;
     56   }
     57   base::DictionaryValue details;
     58   details.Set("syncError",  sync_error.ToValue());
     59   HandleJsEvent(FROM_HERE, "onActionableError",
     60                 JsEventDetails(&details));
     61 }
     62 
     63 void JsSyncManagerObserver::OnInitializationComplete(
     64     const WeakHandle<JsBackend>& js_backend,
     65     const WeakHandle<DataTypeDebugInfoListener>& debug_info_listener,
     66     bool success, syncer::ModelTypeSet restored_types) {
     67   if (!event_handler_.IsInitialized()) {
     68     return;
     69   }
     70   // Ignore the |js_backend| argument; it's not really convertible to
     71   // JSON anyway.
     72 
     73   base::DictionaryValue details;
     74   details.Set("restoredTypes", ModelTypeSetToValue(restored_types));
     75 
     76   HandleJsEvent(FROM_HERE,
     77                 "onInitializationComplete",
     78                 JsEventDetails(&details));
     79 }
     80 
     81 void JsSyncManagerObserver::OnStopSyncingPermanently() {
     82   if (!event_handler_.IsInitialized()) {
     83     return;
     84   }
     85   HandleJsEvent(FROM_HERE, "onStopSyncingPermanently", JsEventDetails());
     86 }
     87 
     88 void JsSyncManagerObserver::HandleJsEvent(
     89     const tracked_objects::Location& from_here,
     90     const std::string& name, const JsEventDetails& details) {
     91   if (!event_handler_.IsInitialized()) {
     92     NOTREACHED();
     93     return;
     94   }
     95   event_handler_.Call(from_here,
     96                       &JsEventHandler::HandleJsEvent, name, details);
     97 }
     98 
     99 }  // namespace syncer
    100