From 649840c77637f62d7a6a5decc079dcff46f8f342 Mon Sep 17 00:00:00 2001 From: "tfarina@chromium.org" Date: Wed, 10 Aug 2011 12:58:00 +0000 Subject: sync: Move all the js_* files into a js/ subdirectory. Note: This was a TODO for akalin. BUG=None TEST=None R=akalin@chromium.org Review URL: http://codereview.chromium.org/7497051 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96169 0039d316-1c4b-4281-b951-d872f2087c98 --- chrome/browser/sync/README.js | 2 - chrome/browser/sync/engine/syncapi.cc | 20 +- chrome/browser/sync/engine/syncapi_unittest.cc | 10 +- chrome/browser/sync/glue/sync_backend_host.cc | 6 +- chrome/browser/sync/glue/sync_backend_host.h | 4 +- chrome/browser/sync/js/js_arg_list.cc | 28 ++ chrome/browser/sync/js/js_arg_list.h | 43 +++ chrome/browser/sync/js/js_arg_list_unittest.cc | 40 +++ chrome/browser/sync/js/js_backend.h | 41 +++ chrome/browser/sync/js/js_controller.h | 50 +++ chrome/browser/sync/js/js_event_details.cc | 29 ++ chrome/browser/sync/js/js_event_details.h | 43 +++ .../browser/sync/js/js_event_details_unittest.cc | 36 +++ chrome/browser/sync/js/js_event_handler.h | 30 ++ chrome/browser/sync/js/js_reply_handler.h | 30 ++ chrome/browser/sync/js/js_sync_manager_observer.cc | 171 +++++++++++ chrome/browser/sync/js/js_sync_manager_observer.h | 65 ++++ .../sync/js/js_sync_manager_observer_unittest.cc | 334 +++++++++++++++++++++ chrome/browser/sync/js/js_test_util.cc | 137 +++++++++ chrome/browser/sync/js/js_test_util.h | 109 +++++++ chrome/browser/sync/js/js_transaction_observer.cc | 95 ++++++ chrome/browser/sync/js/js_transaction_observer.h | 61 ++++ chrome/browser/sync/js_arg_list.cc | 28 -- chrome/browser/sync/js_arg_list.h | 43 --- chrome/browser/sync/js_arg_list_unittest.cc | 40 --- chrome/browser/sync/js_backend.h | 41 --- chrome/browser/sync/js_controller.h | 50 --- chrome/browser/sync/js_event_details.cc | 29 -- chrome/browser/sync/js_event_details.h | 43 --- chrome/browser/sync/js_event_details_unittest.cc | 36 --- chrome/browser/sync/js_event_handler.h | 30 -- chrome/browser/sync/js_reply_handler.h | 30 -- chrome/browser/sync/js_sync_manager_observer.cc | 171 ----------- chrome/browser/sync/js_sync_manager_observer.h | 65 ---- .../sync/js_sync_manager_observer_unittest.cc | 334 --------------------- chrome/browser/sync/js_test_util.cc | 137 --------- chrome/browser/sync/js_test_util.h | 109 ------- chrome/browser/sync/js_transaction_observer.cc | 95 ------ chrome/browser/sync/js_transaction_observer.h | 61 ---- chrome/browser/sync/profile_sync_service.cc | 4 +- .../browser/sync/profile_sync_service_unittest.cc | 6 +- chrome/browser/sync/sync_js_controller.cc | 4 +- chrome/browser/sync/sync_js_controller.h | 6 +- chrome/browser/sync/sync_js_controller_unittest.cc | 6 +- chrome/browser/sync/test_profile_sync_service.cc | 4 +- chrome/browser/ui/webui/sync_internals_ui.cc | 6 +- chrome/browser/ui/webui/sync_internals_ui.h | 6 +- .../browser/ui/webui/sync_internals_ui_unittest.cc | 6 +- chrome/chrome.gyp | 24 +- chrome/chrome_tests.gypi | 10 +- 50 files changed, 1403 insertions(+), 1405 deletions(-) create mode 100644 chrome/browser/sync/js/js_arg_list.cc create mode 100644 chrome/browser/sync/js/js_arg_list.h create mode 100644 chrome/browser/sync/js/js_arg_list_unittest.cc create mode 100644 chrome/browser/sync/js/js_backend.h create mode 100644 chrome/browser/sync/js/js_controller.h create mode 100644 chrome/browser/sync/js/js_event_details.cc create mode 100644 chrome/browser/sync/js/js_event_details.h create mode 100644 chrome/browser/sync/js/js_event_details_unittest.cc create mode 100644 chrome/browser/sync/js/js_event_handler.h create mode 100644 chrome/browser/sync/js/js_reply_handler.h create mode 100644 chrome/browser/sync/js/js_sync_manager_observer.cc create mode 100644 chrome/browser/sync/js/js_sync_manager_observer.h create mode 100644 chrome/browser/sync/js/js_sync_manager_observer_unittest.cc create mode 100644 chrome/browser/sync/js/js_test_util.cc create mode 100644 chrome/browser/sync/js/js_test_util.h create mode 100644 chrome/browser/sync/js/js_transaction_observer.cc create mode 100644 chrome/browser/sync/js/js_transaction_observer.h delete mode 100644 chrome/browser/sync/js_arg_list.cc delete mode 100644 chrome/browser/sync/js_arg_list.h delete mode 100644 chrome/browser/sync/js_arg_list_unittest.cc delete mode 100644 chrome/browser/sync/js_backend.h delete mode 100644 chrome/browser/sync/js_controller.h delete mode 100644 chrome/browser/sync/js_event_details.cc delete mode 100644 chrome/browser/sync/js_event_details.h delete mode 100644 chrome/browser/sync/js_event_details_unittest.cc delete mode 100644 chrome/browser/sync/js_event_handler.h delete mode 100644 chrome/browser/sync/js_reply_handler.h delete mode 100644 chrome/browser/sync/js_sync_manager_observer.cc delete mode 100644 chrome/browser/sync/js_sync_manager_observer.h delete mode 100644 chrome/browser/sync/js_sync_manager_observer_unittest.cc delete mode 100644 chrome/browser/sync/js_test_util.cc delete mode 100644 chrome/browser/sync/js_test_util.h delete mode 100644 chrome/browser/sync/js_transaction_observer.cc delete mode 100644 chrome/browser/sync/js_transaction_observer.h diff --git a/chrome/browser/sync/README.js b/chrome/browser/sync/README.js index 492c5c3..0fbfa66 100644 --- a/chrome/browser/sync/README.js +++ b/chrome/browser/sync/README.js @@ -16,8 +16,6 @@ An event has a name and a details object, which is represented by a JsEventDetails (js_event_details.h) object, which is basically a wrapper around an immutable DictionaryValue. -TODO(akalin): Move all the js_* files into a js/ subdirectory. - Message/event flow ------------------ diff --git a/chrome/browser/sync/engine/syncapi.cc b/chrome/browser/sync/engine/syncapi.cc index e75a6b0..ff446fe 100644 --- a/chrome/browser/sync/engine/syncapi.cc +++ b/chrome/browser/sync/engine/syncapi.cc @@ -34,20 +34,20 @@ #include "base/values.h" #include "chrome/browser/sync/engine/all_status.h" #include "chrome/browser/sync/engine/change_reorder_buffer.h" +#include "chrome/browser/sync/engine/http_post_provider_factory.h" #include "chrome/browser/sync/engine/model_safe_worker.h" -#include "chrome/browser/sync/engine/nudge_source.h" #include "chrome/browser/sync/engine/net/server_connection_manager.h" #include "chrome/browser/sync/engine/net/syncapi_server_connection_manager.h" +#include "chrome/browser/sync/engine/nudge_source.h" #include "chrome/browser/sync/engine/sync_scheduler.h" #include "chrome/browser/sync/engine/syncer.h" -#include "chrome/browser/sync/engine/http_post_provider_factory.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_backend.h" -#include "chrome/browser/sync/js_event_details.h" -#include "chrome/browser/sync/js_event_handler.h" -#include "chrome/browser/sync/js_reply_handler.h" -#include "chrome/browser/sync/js_sync_manager_observer.h" -#include "chrome/browser/sync/js_transaction_observer.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_backend.h" +#include "chrome/browser/sync/js/js_event_details.h" +#include "chrome/browser/sync/js/js_event_handler.h" +#include "chrome/browser/sync/js/js_reply_handler.h" +#include "chrome/browser/sync/js/js_sync_manager_observer.h" +#include "chrome/browser/sync/js/js_transaction_observer.h" #include "chrome/browser/sync/notifier/sync_notifier.h" #include "chrome/browser/sync/notifier/sync_notifier_observer.h" #include "chrome/browser/sync/protocol/app_specifics.pb.h" @@ -66,8 +66,8 @@ #include "chrome/browser/sync/sessions/sync_session_context.h" #include "chrome/browser/sync/syncable/directory_change_delegate.h" #include "chrome/browser/sync/syncable/directory_manager.h" -#include "chrome/browser/sync/syncable/model_type_payload_map.h" #include "chrome/browser/sync/syncable/model_type.h" +#include "chrome/browser/sync/syncable/model_type_payload_map.h" #include "chrome/browser/sync/syncable/nigori_util.h" #include "chrome/browser/sync/syncable/syncable.h" #include "chrome/browser/sync/weak_handle.h" diff --git a/chrome/browser/sync/engine/syncapi_unittest.cc b/chrome/browser/sync/engine/syncapi_unittest.cc index 2f05bab..0b1288f 100644 --- a/chrome/browser/sync/engine/syncapi_unittest.cc +++ b/chrome/browser/sync/engine/syncapi_unittest.cc @@ -25,11 +25,11 @@ #include "chrome/browser/sync/engine/http_post_provider_interface.h" #include "chrome/browser/sync/engine/model_safe_worker.h" #include "chrome/browser/sync/engine/syncapi.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_backend.h" -#include "chrome/browser/sync/js_event_handler.h" -#include "chrome/browser/sync/js_reply_handler.h" -#include "chrome/browser/sync/js_test_util.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_backend.h" +#include "chrome/browser/sync/js/js_event_handler.h" +#include "chrome/browser/sync/js/js_reply_handler.h" +#include "chrome/browser/sync/js/js_test_util.h" #include "chrome/browser/sync/notifier/sync_notifier.h" #include "chrome/browser/sync/notifier/sync_notifier_observer.h" #include "chrome/browser/sync/protocol/bookmark_specifics.pb.h" diff --git a/chrome/browser/sync/glue/sync_backend_host.cc b/chrome/browser/sync/glue/sync_backend_host.cc index 732d1af..86f4478 100644 --- a/chrome/browser/sync/glue/sync_backend_host.cc +++ b/chrome/browser/sync/glue/sync_backend_host.cc @@ -26,9 +26,9 @@ #include "chrome/browser/sync/glue/http_bridge.h" #include "chrome/browser/sync/glue/password_model_worker.h" #include "chrome/browser/sync/glue/sync_backend_host.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_event_details.h" -#include "chrome/browser/sync/js_event_handler.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_event_details.h" +#include "chrome/browser/sync/js/js_event_handler.h" #include "chrome/browser/sync/notifier/sync_notifier.h" #include "chrome/browser/sync/sessions/session_state.h" // TODO(tim): Remove this! We should have a syncapi pass-thru instead. diff --git a/chrome/browser/sync/glue/sync_backend_host.h b/chrome/browser/sync/glue/sync_backend_host.h index 55d25cf..1f7d8c1 100644 --- a/chrome/browser/sync/glue/sync_backend_host.h +++ b/chrome/browser/sync/glue/sync_backend_host.h @@ -19,12 +19,12 @@ #include "base/synchronization/lock.h" #include "base/threading/thread.h" #include "base/timer.h" -#include "chrome/browser/sync/engine/syncapi.h" #include "chrome/browser/sync/engine/configure_reason.h" #include "chrome/browser/sync/engine/model_safe_worker.h" -#include "chrome/browser/sync/js_backend.h" +#include "chrome/browser/sync/engine/syncapi.h" #include "chrome/browser/sync/glue/data_type_controller.h" #include "chrome/browser/sync/glue/ui_model_worker.h" +#include "chrome/browser/sync/js/js_backend.h" #include "chrome/browser/sync/notifier/sync_notifier_factory.h" #include "chrome/browser/sync/syncable/model_type.h" #include "chrome/browser/sync/weak_handle.h" diff --git a/chrome/browser/sync/js/js_arg_list.cc b/chrome/browser/sync/js/js_arg_list.cc new file mode 100644 index 0000000..baded00 --- /dev/null +++ b/chrome/browser/sync/js/js_arg_list.cc @@ -0,0 +1,28 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/sync/js/js_arg_list.h" + +#include "base/json/json_writer.h" + +namespace browser_sync { + +JsArgList::JsArgList() : args_(new SharedValue()) {} + +JsArgList::JsArgList(ListValue* args) + : args_(new SharedValue(args)) {} + +JsArgList::~JsArgList() {} + +const ListValue& JsArgList::Get() const { + return args_->Get(); +} + +std::string JsArgList::ToString() const { + std::string str; + base::JSONWriter::Write(&Get(), false, &str); + return str; +} + +} // namespace browser_sync diff --git a/chrome/browser/sync/js/js_arg_list.h b/chrome/browser/sync/js/js_arg_list.h new file mode 100644 index 0000000..2e5965b --- /dev/null +++ b/chrome/browser/sync/js/js_arg_list.h @@ -0,0 +1,43 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SYNC_JS_JS_ARG_LIST_H_ +#define CHROME_BROWSER_SYNC_JS_JS_ARG_LIST_H_ +#pragma once + +// See README.js for design comments. + +#include + +#include "base/memory/ref_counted.h" +#include "base/values.h" +#include "chrome/browser/sync/shared_value.h" + +namespace browser_sync { + +// A thread-safe wrapper around an immutable ListValue. Used for +// passing around argument lists to different threads. +class JsArgList { + public: + // Uses an empty argument list. + JsArgList(); + + // Takes over the data in |args|, leaving |args| empty. + explicit JsArgList(ListValue* args); + + ~JsArgList(); + + const ListValue& Get() const; + + std::string ToString() const; + + // Copy constructor and assignment operator welcome. + + private: + scoped_refptr > args_; +}; + +} // namespace browser_sync + +#endif // CHROME_BROWSER_SYNC_JS_JS_ARG_LIST_H_ diff --git a/chrome/browser/sync/js/js_arg_list_unittest.cc b/chrome/browser/sync/js/js_arg_list_unittest.cc new file mode 100644 index 0000000..65469f0 --- /dev/null +++ b/chrome/browser/sync/js/js_arg_list_unittest.cc @@ -0,0 +1,40 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/sync/js/js_arg_list.h" + +#include "base/memory/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace browser_sync { +namespace { + +class JsArgListTest : public testing::Test {}; + +TEST_F(JsArgListTest, EmptyList) { + JsArgList arg_list; + EXPECT_TRUE(arg_list.Get().empty()); + EXPECT_EQ("[]", arg_list.ToString()); +} + +TEST_F(JsArgListTest, FromList) { + scoped_ptr list(new ListValue()); + list->Append(Value::CreateBooleanValue(false)); + list->Append(Value::CreateIntegerValue(5)); + DictionaryValue* dict = new DictionaryValue(); + list->Append(dict); + dict->SetString("foo", "bar"); + dict->Set("baz", new ListValue()); + + scoped_ptr list_copy(list->DeepCopy()); + + JsArgList arg_list(list.get()); + + // |arg_list| should take over |list|'s data. + EXPECT_TRUE(list->empty()); + EXPECT_TRUE(arg_list.Get().Equals(list_copy.get())); +} + +} // namespace +} // namespace browser_sync diff --git a/chrome/browser/sync/js/js_backend.h b/chrome/browser/sync/js/js_backend.h new file mode 100644 index 0000000..a141972 --- /dev/null +++ b/chrome/browser/sync/js/js_backend.h @@ -0,0 +1,41 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SYNC_JS_JS_BACKEND_H_ +#define CHROME_BROWSER_SYNC_JS_JS_BACKEND_H_ +#pragma once + +// See README.js for design comments. + +#include + +namespace browser_sync { + +class JsArgList; +class JsEventHandler; +class JsReplyHandler; +template class WeakHandle; + +// Interface representing the backend of chrome://sync-internals. A +// JsBackend can handle messages and can emit events to a +// JsEventHandler. +class JsBackend { + public: + // Starts emitting events to the given handler, if initialized. + virtual void SetJsEventHandler( + const WeakHandle& event_handler) = 0; + + // Processes the given message and replies via the given handler, if + // initialized. + virtual void ProcessJsMessage( + const std::string& name, const JsArgList& args, + const WeakHandle& reply_handler) = 0; + + protected: + virtual ~JsBackend() {} +}; + +} // namespace browser_sync + +#endif // CHROME_BROWSER_SYNC_JS_JS_BACKEND_H_ diff --git a/chrome/browser/sync/js/js_controller.h b/chrome/browser/sync/js/js_controller.h new file mode 100644 index 0000000..edacdd8 --- /dev/null +++ b/chrome/browser/sync/js/js_controller.h @@ -0,0 +1,50 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SYNC_JS_JS_CONTROLLER_H_ +#define CHROME_BROWSER_SYNC_JS_JS_CONTROLLER_H_ +#pragma once + +// See README.js for design comments. + +#include + +namespace browser_sync { + +class JsArgList; +class JsEventHandler; +class JsReplyHandler; +template class WeakHandle; + +// An interface for objects that JsEventHandlers directly interact +// with. JsEventHandlers can add themselves to receive events and +// also send messages which will eventually reach the backend. +class JsController { + public: + // Adds an event handler which will start receiving JS events (not + // immediately, so this can be called in the handler's constructor). + // Multiple event handlers are supported, but each event handler + // must be added at most once. + // + // Ideally, we'd take WeakPtrs, but we need the raw pointer values + // to be able to look them up for removal. + virtual void AddJsEventHandler(JsEventHandler* event_handler) = 0; + + // Removes the given event handler if it has been added. It will + // immediately stop receiving any JS events. + virtual void RemoveJsEventHandler(JsEventHandler* event_handler) = 0; + + // Processes a JS message. The reply (if any) will be sent to + // |reply_handler| if it is initialized. + virtual void ProcessJsMessage( + const std::string& name, const JsArgList& args, + const WeakHandle& reply_handler) = 0; + + protected: + virtual ~JsController() {} +}; + +} // namespace browser_sync + +#endif // CHROME_BROWSER_SYNC_JS_JS_CONTROLLER_H_ diff --git a/chrome/browser/sync/js/js_event_details.cc b/chrome/browser/sync/js/js_event_details.cc new file mode 100644 index 0000000..7fd880e --- /dev/null +++ b/chrome/browser/sync/js/js_event_details.cc @@ -0,0 +1,29 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/sync/js/js_event_details.h" + +#include "base/json/json_writer.h" + +namespace browser_sync { + +JsEventDetails::JsEventDetails() + : details_(new SharedValue()) {} + +JsEventDetails::JsEventDetails(DictionaryValue* details) + : details_(new SharedValue(details)) {} + +JsEventDetails::~JsEventDetails() {} + +const DictionaryValue& JsEventDetails::Get() const { + return details_->Get(); +} + +std::string JsEventDetails::ToString() const { + std::string str; + base::JSONWriter::Write(&Get(), false, &str); + return str; +} + +} // namespace browser_sync diff --git a/chrome/browser/sync/js/js_event_details.h b/chrome/browser/sync/js/js_event_details.h new file mode 100644 index 0000000..066eda1 --- /dev/null +++ b/chrome/browser/sync/js/js_event_details.h @@ -0,0 +1,43 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SYNC_JS_JS_EVENT_DETAILS_H_ +#define CHROME_BROWSER_SYNC_JS_JS_EVENT_DETAILS_H_ +#pragma once + +// See README.js for design comments. + +#include + +#include "base/memory/ref_counted.h" +#include "base/values.h" +#include "chrome/browser/sync/shared_value.h" + +namespace browser_sync { + +// A thread-safe wrapper around an immutable DictionaryValue. Used +// for passing around event details to different threads. +class JsEventDetails { + public: + // Uses an empty dictionary. + JsEventDetails(); + + // Takes over the data in |details|, leaving |details| empty. + explicit JsEventDetails(DictionaryValue* details); + + ~JsEventDetails(); + + const DictionaryValue& Get() const; + + std::string ToString() const; + + // Copy constructor and assignment operator welcome. + + private: + scoped_refptr > details_; +}; + +} // namespace browser_sync + +#endif // CHROME_BROWSER_SYNC_JS_JS_EVENT_DETAILS_H_ diff --git a/chrome/browser/sync/js/js_event_details_unittest.cc b/chrome/browser/sync/js/js_event_details_unittest.cc new file mode 100644 index 0000000..111eb21 --- /dev/null +++ b/chrome/browser/sync/js/js_event_details_unittest.cc @@ -0,0 +1,36 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/sync/js/js_event_details.h" + +#include "base/memory/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace browser_sync { +namespace { + +class JsEventDetailsTest : public testing::Test {}; + +TEST_F(JsEventDetailsTest, EmptyList) { + JsEventDetails details; + EXPECT_TRUE(details.Get().empty()); + EXPECT_EQ("{}", details.ToString()); +} + +TEST_F(JsEventDetailsTest, FromDictionary) { + DictionaryValue dict; + dict.SetString("foo", "bar"); + dict.Set("baz", new ListValue()); + + scoped_ptr dict_copy(dict.DeepCopy()); + + JsEventDetails details(&dict); + + // |details| should take over |dict|'s data. + EXPECT_TRUE(dict.empty()); + EXPECT_TRUE(details.Get().Equals(dict_copy.get())); +} + +} // namespace +} // namespace browser_sync diff --git a/chrome/browser/sync/js/js_event_handler.h b/chrome/browser/sync/js/js_event_handler.h new file mode 100644 index 0000000..bf8ca32 --- /dev/null +++ b/chrome/browser/sync/js/js_event_handler.h @@ -0,0 +1,30 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SYNC_JS_JS_EVENT_HANDLER_H_ +#define CHROME_BROWSER_SYNC_JS_JS_EVENT_HANDLER_H_ +#pragma once + +// See README.js for design comments. + +#include + +namespace browser_sync { + +class JsEventDetails; + +// An interface for objects that handle Javascript events (e.g., +// WebUIs). +class JsEventHandler { + public: + virtual void HandleJsEvent( + const std::string& name, const JsEventDetails& details) = 0; + + protected: + virtual ~JsEventHandler() {} +}; + +} // namespace browser_sync + +#endif // CHROME_BROWSER_SYNC_JS_JS_EVENT_HANDLER_H_ diff --git a/chrome/browser/sync/js/js_reply_handler.h b/chrome/browser/sync/js/js_reply_handler.h new file mode 100644 index 0000000..4118cd9 --- /dev/null +++ b/chrome/browser/sync/js/js_reply_handler.h @@ -0,0 +1,30 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SYNC_JS_JS_REPLY_HANDLER_H_ +#define CHROME_BROWSER_SYNC_JS_JS_REPLY_HANDLER_H_ +#pragma once + +// See README.js for design comments. + +#include + +namespace browser_sync { + +class JsArgList; + +// An interface for objects that handle Javascript message replies +// (e.g., WebUIs). +class JsReplyHandler { + public: + virtual void HandleJsReply( + const std::string& name, const JsArgList& args) = 0; + + protected: + virtual ~JsReplyHandler() {} +}; + +} // namespace browser_sync + +#endif // CHROME_BROWSER_SYNC_JS_JS_REPLY_HANDLER_H_ diff --git a/chrome/browser/sync/js/js_sync_manager_observer.cc b/chrome/browser/sync/js/js_sync_manager_observer.cc new file mode 100644 index 0000000..e966ad3 --- /dev/null +++ b/chrome/browser/sync/js/js_sync_manager_observer.cc @@ -0,0 +1,171 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/sync/js/js_sync_manager_observer.h" + +#include + +#include "base/logging.h" +#include "base/tracked.h" +#include "base/values.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_event_details.h" +#include "chrome/browser/sync/js/js_event_handler.h" +#include "chrome/browser/sync/sessions/session_state.h" +#include "chrome/browser/sync/syncable/model_type.h" + +namespace browser_sync { + +JsSyncManagerObserver::JsSyncManagerObserver() {} + +JsSyncManagerObserver::~JsSyncManagerObserver() {} + +void JsSyncManagerObserver::SetJsEventHandler( + const WeakHandle& event_handler) { + event_handler_ = event_handler; +} + +void JsSyncManagerObserver::OnChangesApplied( + syncable::ModelType model_type, + const sync_api::BaseTransaction* trans, + const sync_api::SyncManager::ChangeRecord* changes, + int change_count) { + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.SetString("modelType", syncable::ModelTypeToString(model_type)); + ListValue* change_values = new ListValue(); + details.Set("changes", change_values); + for (int i = 0; i < change_count; ++i) { + change_values->Append(changes[i].ToValue(trans)); + } + HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); +} + +void JsSyncManagerObserver::OnChangesComplete( + syncable::ModelType model_type) { + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.SetString("modelType", syncable::ModelTypeToString(model_type)); + HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details)); +} + +void JsSyncManagerObserver::OnSyncCycleCompleted( + const sessions::SyncSessionSnapshot* snapshot) { + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.Set("snapshot", snapshot->ToValue()); + HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details)); +} + +void JsSyncManagerObserver::OnAuthError( + const GoogleServiceAuthError& auth_error) { + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.Set("authError", auth_error.ToValue()); + HandleJsEvent(FROM_HERE, "onAuthError", JsEventDetails(&details)); +} + +void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) { + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.SetString("token", ""); + HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details)); +} + +void JsSyncManagerObserver::OnPassphraseRequired( + sync_api::PassphraseRequiredReason reason) { + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.SetString("reason", + sync_api::PassphraseRequiredReasonToString(reason)); + HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details)); +} + +void JsSyncManagerObserver::OnPassphraseAccepted( + const std::string& bootstrap_token) { + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.SetString("bootstrapToken", ""); + HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details)); +} + +void JsSyncManagerObserver::OnEncryptionComplete( + const syncable::ModelTypeSet& encrypted_types) { + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.Set("encryptedTypes", + syncable::ModelTypeSetToValue(encrypted_types)); + HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails(&details)); +} + +void JsSyncManagerObserver::OnMigrationNeededForTypes( + const syncable::ModelTypeSet& types) { + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.Set("types", syncable::ModelTypeSetToValue(types)); + HandleJsEvent(FROM_HERE, "onMigrationNeededForTypes", + JsEventDetails(&details)); +} + +void JsSyncManagerObserver::OnInitializationComplete( + const WeakHandle& js_backend) { + if (!event_handler_.IsInitialized()) { + return; + } + // Ignore the |js_backend| argument; it's not really convertible to + // JSON anyway. + HandleJsEvent(FROM_HERE, "onInitializationComplete", JsEventDetails()); +} + +void JsSyncManagerObserver::OnStopSyncingPermanently() { + if (!event_handler_.IsInitialized()) { + return; + } + HandleJsEvent(FROM_HERE, "onStopSyncingPermanently", JsEventDetails()); +} + +void JsSyncManagerObserver::OnClearServerDataSucceeded() { + if (!event_handler_.IsInitialized()) { + return; + } + HandleJsEvent(FROM_HERE, "onClearServerDataSucceeded", JsEventDetails()); +} + +void JsSyncManagerObserver::OnClearServerDataFailed() { + if (!event_handler_.IsInitialized()) { + return; + } + HandleJsEvent(FROM_HERE, "onClearServerDataFailed", JsEventDetails()); +} + +void JsSyncManagerObserver::HandleJsEvent( + const tracked_objects::Location& from_here, + const std::string& name, const JsEventDetails& details) { + if (!event_handler_.IsInitialized()) { + NOTREACHED(); + return; + } + event_handler_.Call(from_here, + &JsEventHandler::HandleJsEvent, name, details); +} + +} // namespace browser_sync diff --git a/chrome/browser/sync/js/js_sync_manager_observer.h b/chrome/browser/sync/js/js_sync_manager_observer.h new file mode 100644 index 0000000..cc7c8c0 --- /dev/null +++ b/chrome/browser/sync/js/js_sync_manager_observer.h @@ -0,0 +1,65 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SYNC_JS_JS_SYNC_MANAGER_OBSERVER_H_ +#define CHROME_BROWSER_SYNC_JS_JS_SYNC_MANAGER_OBSERVER_H_ +#pragma once + +#include + +#include "base/basictypes.h" +#include "chrome/browser/sync/engine/syncapi.h" +#include "chrome/browser/sync/weak_handle.h" + +namespace tracked_objects { +class Location; +} // namespace tracked_objects + +namespace browser_sync { + +class JsEventDetails; +class JsEventHandler; + +// Routes SyncManager events to a JsEventHandler. +class JsSyncManagerObserver : public sync_api::SyncManager::Observer { + public: + JsSyncManagerObserver(); + virtual ~JsSyncManagerObserver(); + + void SetJsEventHandler(const WeakHandle& event_handler); + + // sync_api::SyncManager::Observer implementation. + virtual void OnChangesApplied( + syncable::ModelType model_type, + const sync_api::BaseTransaction* trans, + const sync_api::SyncManager::ChangeRecord* changes, + int change_count); + virtual void OnChangesComplete(syncable::ModelType model_type); + virtual void OnSyncCycleCompleted( + const sessions::SyncSessionSnapshot* snapshot); + virtual void OnAuthError(const GoogleServiceAuthError& auth_error); + virtual void OnUpdatedToken(const std::string& token); + virtual void OnPassphraseRequired(sync_api::PassphraseRequiredReason reason); + virtual void OnPassphraseAccepted(const std::string& bootstrap_token); + virtual void OnEncryptionComplete( + const syncable::ModelTypeSet& encrypted_types); + virtual void OnInitializationComplete( + const WeakHandle& js_backend); + virtual void OnStopSyncingPermanently(); + virtual void OnClearServerDataSucceeded(); + virtual void OnClearServerDataFailed(); + virtual void OnMigrationNeededForTypes(const syncable::ModelTypeSet& types); + + private: + void HandleJsEvent(const tracked_objects::Location& from_here, + const std::string& name, const JsEventDetails& details); + + WeakHandle event_handler_; + + DISALLOW_COPY_AND_ASSIGN(JsSyncManagerObserver); +}; + +} // namespace browser_sync + +#endif // CHROME_BROWSER_SYNC_JS_JS_SYNC_MANAGER_OBSERVER_H_ diff --git a/chrome/browser/sync/js/js_sync_manager_observer_unittest.cc b/chrome/browser/sync/js/js_sync_manager_observer_unittest.cc new file mode 100644 index 0000000..2949104 --- /dev/null +++ b/chrome/browser/sync/js/js_sync_manager_observer_unittest.cc @@ -0,0 +1,334 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/sync/js/js_sync_manager_observer.h" + +#include + +#include "base/basictypes.h" +#include "base/message_loop.h" +#include "base/tracked.h" +#include "base/values.h" +#include "chrome/browser/sync/engine/syncapi.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_event_details.h" +#include "chrome/browser/sync/js/js_test_util.h" +#include "chrome/browser/sync/sessions/session_state.h" +#include "chrome/browser/sync/syncable/model_type.h" +#include "chrome/browser/sync/weak_handle.h" +#include "chrome/test/sync/engine/test_user_share.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace browser_sync { +namespace { + +using ::testing::InSequence; +using ::testing::StrictMock; + +class JsSyncManagerObserverTest : public testing::Test { + protected: + JsSyncManagerObserverTest() { + js_sync_manager_observer_.SetJsEventHandler( + mock_js_event_handler_.AsWeakHandle()); + } + + private: + // This must be destroyed after the member variables below in order + // for WeakHandles to be destroyed properly. + MessageLoop message_loop_; + + protected: + StrictMock mock_js_event_handler_; + JsSyncManagerObserver js_sync_manager_observer_; + + void PumpLoop() { + message_loop_.RunAllPending(); + } +}; + +TEST_F(JsSyncManagerObserverTest, NoArgNotifiations) { + InSequence dummy; + + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onInitializationComplete", + HasDetails(JsEventDetails()))); + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onStopSyncingPermanently", + HasDetails(JsEventDetails()))); + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onClearServerDataSucceeded", + HasDetails(JsEventDetails()))); + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onClearServerDataFailed", + HasDetails(JsEventDetails()))); + + js_sync_manager_observer_.OnInitializationComplete(WeakHandle()); + js_sync_manager_observer_.OnStopSyncingPermanently(); + js_sync_manager_observer_.OnClearServerDataSucceeded(); + js_sync_manager_observer_.OnClearServerDataFailed(); + PumpLoop(); +} + +TEST_F(JsSyncManagerObserverTest, OnChangesComplete) { + InSequence dummy; + + for (int i = syncable::FIRST_REAL_MODEL_TYPE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + DictionaryValue expected_details; + expected_details.SetString( + "modelType", + syncable::ModelTypeToString(syncable::ModelTypeFromInt(i))); + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onChangesComplete", + HasDetailsAsDictionary(expected_details))); + } + + for (int i = syncable::FIRST_REAL_MODEL_TYPE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + js_sync_manager_observer_.OnChangesComplete(syncable::ModelTypeFromInt(i)); + } + PumpLoop(); +} + +TEST_F(JsSyncManagerObserverTest, OnSyncCycleCompleted) { + std::string download_progress_markers[syncable::MODEL_TYPE_COUNT]; + sessions::SyncSessionSnapshot snapshot(sessions::SyncerStatus(), + sessions::ErrorCounters(), + 100, + false, + syncable::ModelTypeBitSet(), + download_progress_markers, + false, + true, + 100, + 8, + 5, + false, + sessions::SyncSourceInfo(), + 0); + DictionaryValue expected_details; + expected_details.Set("snapshot", snapshot.ToValue()); + + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onSyncCycleCompleted", + HasDetailsAsDictionary(expected_details))); + + js_sync_manager_observer_.OnSyncCycleCompleted(&snapshot); + PumpLoop(); +} + +TEST_F(JsSyncManagerObserverTest, OnAuthError) { + GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); + DictionaryValue expected_details; + expected_details.Set("authError", error.ToValue()); + + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onAuthError", + HasDetailsAsDictionary(expected_details))); + + js_sync_manager_observer_.OnAuthError(error); + PumpLoop(); +} + +TEST_F(JsSyncManagerObserverTest, OnPassphraseRequired) { + InSequence dummy; + + DictionaryValue reason_passphrase_not_required_details; + DictionaryValue reason_encryption_details; + DictionaryValue reason_decryption_details; + DictionaryValue reason_set_passphrase_failed_details; + + reason_passphrase_not_required_details.SetString( + "reason", + sync_api::PassphraseRequiredReasonToString( + sync_api::REASON_PASSPHRASE_NOT_REQUIRED)); + reason_encryption_details.SetString( + "reason", + sync_api::PassphraseRequiredReasonToString(sync_api::REASON_ENCRYPTION)); + reason_decryption_details.SetString( + "reason", + sync_api::PassphraseRequiredReasonToString(sync_api::REASON_DECRYPTION)); + reason_set_passphrase_failed_details.SetString( + "reason", + sync_api::PassphraseRequiredReasonToString( + sync_api::REASON_SET_PASSPHRASE_FAILED)); + + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onPassphraseRequired", + HasDetailsAsDictionary( + reason_passphrase_not_required_details))); + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onPassphraseRequired", + HasDetailsAsDictionary(reason_encryption_details))); + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onPassphraseRequired", + HasDetailsAsDictionary(reason_decryption_details))); + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onPassphraseRequired", + HasDetailsAsDictionary( + reason_set_passphrase_failed_details))); + + js_sync_manager_observer_.OnPassphraseRequired( + sync_api::REASON_PASSPHRASE_NOT_REQUIRED); + js_sync_manager_observer_.OnPassphraseRequired(sync_api::REASON_ENCRYPTION); + js_sync_manager_observer_.OnPassphraseRequired(sync_api::REASON_DECRYPTION); + js_sync_manager_observer_.OnPassphraseRequired( + sync_api::REASON_SET_PASSPHRASE_FAILED); + PumpLoop(); +} + +TEST_F(JsSyncManagerObserverTest, SensitiveNotifiations) { + DictionaryValue redacted_token_details; + redacted_token_details.SetString("token", ""); + DictionaryValue redacted_bootstrap_token_details; + redacted_bootstrap_token_details.SetString("bootstrapToken", ""); + + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onUpdatedToken", + HasDetailsAsDictionary(redacted_token_details))); + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent( + "onPassphraseAccepted", + HasDetailsAsDictionary(redacted_bootstrap_token_details))); + + js_sync_manager_observer_.OnUpdatedToken("sensitive_token"); + js_sync_manager_observer_.OnPassphraseAccepted("sensitive_token"); + PumpLoop(); +} + +TEST_F(JsSyncManagerObserverTest, OnEncryptionComplete) { + DictionaryValue expected_details; + ListValue* encrypted_type_values = new ListValue(); + expected_details.Set("encryptedTypes", encrypted_type_values); + syncable::ModelTypeSet encrypted_types; + + for (int i = syncable::FIRST_REAL_MODEL_TYPE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + syncable::ModelType type = syncable::ModelTypeFromInt(i); + encrypted_types.insert(type); + encrypted_type_values->Append(Value::CreateStringValue( + syncable::ModelTypeToString(type))); + } + + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onEncryptionComplete", + HasDetailsAsDictionary(expected_details))); + + js_sync_manager_observer_.OnEncryptionComplete(encrypted_types); + PumpLoop(); +} + +TEST_F(JsSyncManagerObserverTest, OnMigrationNeededForTypes) { + DictionaryValue expected_details; + ListValue* type_values = new ListValue(); + expected_details.Set("types", type_values); + syncable::ModelTypeSet types; + + for (int i = syncable::FIRST_REAL_MODEL_TYPE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + syncable::ModelType type = syncable::ModelTypeFromInt(i); + types.insert(type); + type_values->Append(Value::CreateStringValue( + syncable::ModelTypeToString(type))); + } + + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onMigrationNeededForTypes", + HasDetailsAsDictionary(expected_details))); + + js_sync_manager_observer_.OnMigrationNeededForTypes(types); + PumpLoop(); +} + +namespace { + +// Makes a node of the given model type. Returns the id of the +// newly-created node. +int64 MakeNode(sync_api::UserShare* share, syncable::ModelType model_type) { + sync_api::WriteTransaction trans(FROM_HERE, share); + sync_api::ReadNode root_node(&trans); + root_node.InitByRootLookup(); + sync_api::WriteNode node(&trans); + EXPECT_TRUE(node.InitUniqueByCreation( + model_type, root_node, + syncable::ModelTypeToString(model_type))); + node.SetIsFolder(false); + return node.GetId(); +} + +} // namespace + +TEST_F(JsSyncManagerObserverTest, OnChangesApplied) { + InSequence dummy; + + TestUserShare test_user_share; + test_user_share.SetUp(); + + // We don't test with passwords as that requires additional setup. + + // Build a list of example ChangeRecords. + sync_api::SyncManager::ChangeRecord changes[syncable::MODEL_TYPE_COUNT]; + for (int i = syncable::AUTOFILL_PROFILE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + changes[i].id = + MakeNode(test_user_share.user_share(), syncable::ModelTypeFromInt(i)); + switch (i % 3) { + case 0: + changes[i].action = + sync_api::SyncManager::ChangeRecord::ACTION_ADD; + break; + case 1: + changes[i].action = + sync_api::SyncManager::ChangeRecord::ACTION_UPDATE; + break; + default: + changes[i].action = + sync_api::SyncManager::ChangeRecord::ACTION_DELETE; + break; + } + { + sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); + sync_api::ReadNode node(&trans); + EXPECT_TRUE(node.InitByIdLookup(changes[i].id)); + changes[i].specifics = node.GetEntry()->Get(syncable::SPECIFICS); + } + } + + // For each i, we call OnChangesApplied() with the first arg equal + // to i cast to ModelType and the second argument with the changes + // starting from changes[i]. + + // Set expectations for each data type. + for (int i = syncable::AUTOFILL_PROFILE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + const std::string& model_type_str = + syncable::ModelTypeToString(syncable::ModelTypeFromInt(i)); + DictionaryValue expected_details; + expected_details.SetString("modelType", model_type_str); + ListValue* expected_changes = new ListValue(); + expected_details.Set("changes", expected_changes); + for (int j = i; j < syncable::MODEL_TYPE_COUNT; ++j) { + sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); + expected_changes->Append(changes[j].ToValue(&trans)); + } + EXPECT_CALL(mock_js_event_handler_, + HandleJsEvent("onChangesApplied", + HasDetailsAsDictionary(expected_details))); + } + + // Fire OnChangesApplied() for each data type. + for (int i = syncable::AUTOFILL_PROFILE; + i < syncable::MODEL_TYPE_COUNT; ++i) { + sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); + js_sync_manager_observer_.OnChangesApplied(syncable::ModelTypeFromInt(i), + &trans, &changes[i], + syncable::MODEL_TYPE_COUNT - i); + } + + test_user_share.TearDown(); + PumpLoop(); +} + +} // namespace +} // namespace browser_sync diff --git a/chrome/browser/sync/js/js_test_util.cc b/chrome/browser/sync/js/js_test_util.cc new file mode 100644 index 0000000..94a02f5 --- /dev/null +++ b/chrome/browser/sync/js/js_test_util.cc @@ -0,0 +1,137 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/sync/js/js_test_util.h" + +#include "base/basictypes.h" +#include "base/memory/scoped_ptr.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_event_details.h" + +namespace browser_sync { + +void PrintTo(const JsArgList& args, ::std::ostream* os) { + *os << args.ToString(); +} + +void PrintTo(const JsEventDetails& details, ::std::ostream* os) { + *os << details.ToString(); +} + +namespace { + +// Matcher implementation for HasArgs(). +class HasArgsMatcher + : public ::testing::MatcherInterface { + public: + explicit HasArgsMatcher(const JsArgList& expected_args) + : expected_args_(expected_args) {} + + virtual ~HasArgsMatcher() {} + + virtual bool MatchAndExplain( + const JsArgList& args, + ::testing::MatchResultListener* listener) const { + // No need to annotate listener since we already define PrintTo(). + return args.Get().Equals(&expected_args_.Get()); + } + + virtual void DescribeTo(::std::ostream* os) const { + *os << "has args " << expected_args_.ToString(); + } + + virtual void DescribeNegationTo(::std::ostream* os) const { + *os << "doesn't have args " << expected_args_.ToString(); + } + + private: + const JsArgList expected_args_; + + DISALLOW_COPY_AND_ASSIGN(HasArgsMatcher); +}; + +// Matcher implementation for HasDetails(). +class HasDetailsMatcher + : public ::testing::MatcherInterface { + public: + explicit HasDetailsMatcher(const JsEventDetails& expected_details) + : expected_details_(expected_details) {} + + virtual ~HasDetailsMatcher() {} + + virtual bool MatchAndExplain( + const JsEventDetails& details, + ::testing::MatchResultListener* listener) const { + // No need to annotate listener since we already define PrintTo(). + return details.Get().Equals(&expected_details_.Get()); + } + + virtual void DescribeTo(::std::ostream* os) const { + *os << "has details " << expected_details_.ToString(); + } + + virtual void DescribeNegationTo(::std::ostream* os) const { + *os << "doesn't have details " << expected_details_.ToString(); + } + + private: + const JsEventDetails expected_details_; + + DISALLOW_COPY_AND_ASSIGN(HasDetailsMatcher); +}; + +} // namespace + +::testing::Matcher HasArgs(const JsArgList& expected_args) { + return ::testing::MakeMatcher(new HasArgsMatcher(expected_args)); +} + +::testing::Matcher HasArgsAsList( + const ListValue& expected_args) { + scoped_ptr expected_args_copy(expected_args.DeepCopy()); + return HasArgs(JsArgList(expected_args_copy.get())); +} + +::testing::Matcher HasDetails( + const JsEventDetails& expected_details) { + return ::testing::MakeMatcher(new HasDetailsMatcher(expected_details)); +} + +::testing::Matcher HasDetailsAsDictionary( + const DictionaryValue& expected_details) { + scoped_ptr expected_details_copy( + expected_details.DeepCopy()); + return HasDetails(JsEventDetails(expected_details_copy.get())); +} + +MockJsBackend::MockJsBackend() {} + +MockJsBackend::~MockJsBackend() {} + +WeakHandle MockJsBackend::AsWeakHandle() { + return WeakHandle(AsWeakPtr()); +} + +MockJsController::MockJsController() {} + +MockJsController::~MockJsController() {} + +MockJsEventHandler::MockJsEventHandler() {} + +WeakHandle MockJsEventHandler::AsWeakHandle() { + return WeakHandle(AsWeakPtr()); +} + +MockJsEventHandler::~MockJsEventHandler() {} + +MockJsReplyHandler::MockJsReplyHandler() {} + +MockJsReplyHandler::~MockJsReplyHandler() {} + +WeakHandle MockJsReplyHandler::AsWeakHandle() { + return WeakHandle(AsWeakPtr()); +} + +} // namespace browser_sync + diff --git a/chrome/browser/sync/js/js_test_util.h b/chrome/browser/sync/js/js_test_util.h new file mode 100644 index 0000000..eae0704 --- /dev/null +++ b/chrome/browser/sync/js/js_test_util.h @@ -0,0 +1,109 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SYNC_JS_JS_TEST_UTIL_H_ +#define CHROME_BROWSER_SYNC_JS_JS_TEST_UTIL_H_ +#pragma once + +#include +#include + +#include "base/memory/weak_ptr.h" +#include "chrome/browser/sync/js/js_backend.h" +#include "chrome/browser/sync/js/js_controller.h" +#include "chrome/browser/sync/js/js_event_handler.h" +#include "chrome/browser/sync/js/js_reply_handler.h" +#include "chrome/browser/sync/weak_handle.h" +#include "testing/gmock/include/gmock/gmock.h" + +namespace base { +class DictionaryValue; +class ListValue; +} + +namespace browser_sync { + +class JsArgList; +class JsEventDetails; + +// Defined for googletest. Equivalent to "*os << args.ToString()". +void PrintTo(const JsArgList& args, ::std::ostream* os); +void PrintTo(const JsEventDetails& details, ::std::ostream* os); + +// A gmock matcher for JsArgList. Use like: +// +// EXPECT_CALL(mock, HandleJsReply("foo", HasArgs(expected_args))); +::testing::Matcher HasArgs(const JsArgList& expected_args); + +// Like HasArgs() but takes a ListValue instead. +::testing::Matcher HasArgsAsList( + const base::ListValue& expected_args); + +// A gmock matcher for JsEventDetails. Use like: +// +// EXPECT_CALL(mock, HandleJsEvent("foo", HasArgs(expected_details))); +::testing::Matcher HasDetails( + const JsEventDetails& expected_details); + +// Like HasDetails() but takes a DictionaryValue instead. +::testing::Matcher HasDetailsAsDictionary( + const base::DictionaryValue& expected_details); + +// Mocks. + +class MockJsBackend : public JsBackend, + public base::SupportsWeakPtr { + public: + MockJsBackend(); + virtual ~MockJsBackend(); + + WeakHandle AsWeakHandle(); + + MOCK_METHOD1(SetJsEventHandler, void(const WeakHandle&)); + MOCK_METHOD3(ProcessJsMessage, void(const ::std::string&, const JsArgList&, + const WeakHandle&)); +}; + +class MockJsController : public JsController, + public base::SupportsWeakPtr { + public: + MockJsController(); + virtual ~MockJsController(); + + MOCK_METHOD1(AddJsEventHandler, void(JsEventHandler*)); + MOCK_METHOD1(RemoveJsEventHandler, void(JsEventHandler*)); + MOCK_METHOD3(ProcessJsMessage, + void(const ::std::string&, const JsArgList&, + const WeakHandle&)); +}; + +class MockJsEventHandler + : public JsEventHandler, + public base::SupportsWeakPtr { + public: + MockJsEventHandler(); + virtual ~MockJsEventHandler(); + + WeakHandle AsWeakHandle(); + + MOCK_METHOD2(HandleJsEvent, + void(const ::std::string&, const JsEventDetails&)); +}; + +class MockJsReplyHandler + : public JsReplyHandler, + public base::SupportsWeakPtr { + public: + MockJsReplyHandler(); + virtual ~MockJsReplyHandler(); + + WeakHandle AsWeakHandle(); + + MOCK_METHOD2(HandleJsReply, + void(const ::std::string&, const JsArgList&)); +}; + +} // namespace browser_sync + +#endif // CHROME_BROWSER_SYNC_JS_JS_TEST_UTIL_H_ diff --git a/chrome/browser/sync/js/js_transaction_observer.cc b/chrome/browser/sync/js/js_transaction_observer.cc new file mode 100644 index 0000000..1de2925 --- /dev/null +++ b/chrome/browser/sync/js/js_transaction_observer.cc @@ -0,0 +1,95 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/sync/js/js_transaction_observer.h" + +#include +#include + +#include "base/logging.h" +#include "base/tracked.h" +#include "base/values.h" +#include "chrome/browser/sync/js/js_event_details.h" +#include "chrome/browser/sync/js/js_event_handler.h" + +namespace browser_sync { + +JsTransactionObserver::JsTransactionObserver() {} + +JsTransactionObserver::~JsTransactionObserver() { + DCHECK(non_thread_safe_.CalledOnValidThread()); +} + +void JsTransactionObserver::SetJsEventHandler( + const WeakHandle& event_handler) { + event_handler_ = event_handler; +} + +namespace { + +std::string GetLocationString(const tracked_objects::Location& location) { + std::ostringstream oss; + oss << location.function_name() << "@" + << location.file_name() << ":" << location.line_number(); + return oss.str(); +} + +} // namespace + +void JsTransactionObserver::OnTransactionStart( + const tracked_objects::Location& location, + const syncable::WriterTag& writer) { + DCHECK(non_thread_safe_.CalledOnValidThread()); + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.SetString("location", GetLocationString(location)); + details.SetString("writer", syncable::WriterTagToString(writer)); + HandleJsEvent(FROM_HERE, "onTransactionStart", JsEventDetails(&details)); +} + +void JsTransactionObserver::OnTransactionMutate( + const tracked_objects::Location& location, + const syncable::WriterTag& writer, + const syncable::EntryKernelMutationSet& mutations, + const syncable::ModelTypeBitSet& models_with_changes) { + DCHECK(non_thread_safe_.CalledOnValidThread()); + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.SetString("location", GetLocationString(location)); + details.SetString("writer", syncable::WriterTagToString(writer)); + details.Set("mutations", syncable::EntryKernelMutationSetToValue(mutations)); + details.Set("modelsWithChanges", + syncable::ModelTypeBitSetToValue(models_with_changes)); + HandleJsEvent(FROM_HERE, "onTransactionMutate", JsEventDetails(&details)); +} + +void JsTransactionObserver::OnTransactionEnd( + const tracked_objects::Location& location, + const syncable::WriterTag& writer) { + DCHECK(non_thread_safe_.CalledOnValidThread()); + if (!event_handler_.IsInitialized()) { + return; + } + DictionaryValue details; + details.SetString("location", GetLocationString(location)); + details.SetString("writer", syncable::WriterTagToString(writer)); + HandleJsEvent(FROM_HERE, "onTransactionEnd", JsEventDetails(&details)); +} + +void JsTransactionObserver::HandleJsEvent( + const tracked_objects::Location& from_here, + const std::string& name, const JsEventDetails& details) { + if (!event_handler_.IsInitialized()) { + NOTREACHED(); + return; + } + event_handler_.Call(from_here, + &JsEventHandler::HandleJsEvent, name, details); +} + +} // namespace browser_sync diff --git a/chrome/browser/sync/js/js_transaction_observer.h b/chrome/browser/sync/js/js_transaction_observer.h new file mode 100644 index 0000000..aa618ce --- /dev/null +++ b/chrome/browser/sync/js/js_transaction_observer.h @@ -0,0 +1,61 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_SYNC_JS_JS_TRANSACTION_OBSERVER_H_ +#define CHROME_BROWSER_SYNC_JS_JS_TRANSACTION_OBSERVER_H_ +#pragma once + +#include + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/threading/non_thread_safe.h" +#include "chrome/browser/sync/syncable/transaction_observer.h" +#include "chrome/browser/sync/weak_handle.h" + +namespace tracked_objects { +class Location; +} // namespace tracked_objects + +namespace browser_sync { + +class JsEventDetails; +class JsEventHandler; + +// Routes SyncManager events to a JsEventHandler. +class JsTransactionObserver : public syncable::TransactionObserver { + public: + JsTransactionObserver(); + + virtual ~JsTransactionObserver(); + + void SetJsEventHandler(const WeakHandle& event_handler); + + // syncable::TransactionObserver implementation. + virtual void OnTransactionStart( + const tracked_objects::Location& location, + const syncable::WriterTag& writer) OVERRIDE; + virtual void OnTransactionMutate( + const tracked_objects::Location& location, + const syncable::WriterTag& writer, + const syncable::EntryKernelMutationSet& mutations, + const syncable::ModelTypeBitSet& models_with_changes) OVERRIDE; + virtual void OnTransactionEnd( + const tracked_objects::Location& location, + const syncable::WriterTag& writer) OVERRIDE; + + private: + base::NonThreadSafe non_thread_safe_; + WeakHandle event_handler_; + + void HandleJsEvent( + const tracked_objects::Location& from_here, + const std::string& name, const JsEventDetails& details); + + DISALLOW_COPY_AND_ASSIGN(JsTransactionObserver); +}; + +} // namespace browser_sync + +#endif // CHROME_BROWSER_SYNC_JS_JS_TRANSACTION_OBSERVER_H_ diff --git a/chrome/browser/sync/js_arg_list.cc b/chrome/browser/sync/js_arg_list.cc deleted file mode 100644 index 73daea0..0000000 --- a/chrome/browser/sync/js_arg_list.cc +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/js_arg_list.h" - -#include "base/json/json_writer.h" - -namespace browser_sync { - -JsArgList::JsArgList() : args_(new SharedValue()) {} - -JsArgList::JsArgList(ListValue* args) - : args_(new SharedValue(args)) {} - -JsArgList::~JsArgList() {} - -const ListValue& JsArgList::Get() const { - return args_->Get(); -} - -std::string JsArgList::ToString() const { - std::string str; - base::JSONWriter::Write(&Get(), false, &str); - return str; -} - -} // namespace browser_sync diff --git a/chrome/browser/sync/js_arg_list.h b/chrome/browser/sync/js_arg_list.h deleted file mode 100644 index 3dafa71..0000000 --- a/chrome/browser/sync/js_arg_list.h +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_JS_ARG_LIST_H_ -#define CHROME_BROWSER_SYNC_JS_ARG_LIST_H_ -#pragma once - -// See README.js for design comments. - -#include - -#include "base/memory/ref_counted.h" -#include "base/values.h" -#include "chrome/browser/sync/shared_value.h" - -namespace browser_sync { - -// A thread-safe wrapper around an immutable ListValue. Used for -// passing around argument lists to different threads. -class JsArgList { - public: - // Uses an empty argument list. - JsArgList(); - - // Takes over the data in |args|, leaving |args| empty. - explicit JsArgList(ListValue* args); - - ~JsArgList(); - - const ListValue& Get() const; - - std::string ToString() const; - - // Copy constructor and assignment operator welcome. - - private: - scoped_refptr > args_; -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_JS_ARG_LIST_H_ diff --git a/chrome/browser/sync/js_arg_list_unittest.cc b/chrome/browser/sync/js_arg_list_unittest.cc deleted file mode 100644 index ed5c952..0000000 --- a/chrome/browser/sync/js_arg_list_unittest.cc +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/js_arg_list.h" - -#include "base/memory/scoped_ptr.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace browser_sync { -namespace { - -class JsArgListTest : public testing::Test {}; - -TEST_F(JsArgListTest, EmptyList) { - JsArgList arg_list; - EXPECT_TRUE(arg_list.Get().empty()); - EXPECT_EQ("[]", arg_list.ToString()); -} - -TEST_F(JsArgListTest, FromList) { - scoped_ptr list(new ListValue()); - list->Append(Value::CreateBooleanValue(false)); - list->Append(Value::CreateIntegerValue(5)); - DictionaryValue* dict = new DictionaryValue(); - list->Append(dict); - dict->SetString("foo", "bar"); - dict->Set("baz", new ListValue()); - - scoped_ptr list_copy(list->DeepCopy()); - - JsArgList arg_list(list.get()); - - // |arg_list| should take over |list|'s data. - EXPECT_TRUE(list->empty()); - EXPECT_TRUE(arg_list.Get().Equals(list_copy.get())); -} - -} // namespace -} // namespace browser_sync diff --git a/chrome/browser/sync/js_backend.h b/chrome/browser/sync/js_backend.h deleted file mode 100644 index bd5db52..0000000 --- a/chrome/browser/sync/js_backend.h +++ /dev/null @@ -1,41 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_JS_BACKEND_H_ -#define CHROME_BROWSER_SYNC_JS_BACKEND_H_ -#pragma once - -// See README.js for design comments. - -#include - -namespace browser_sync { - -class JsArgList; -class JsEventHandler; -class JsReplyHandler; -template class WeakHandle; - -// Interface representing the backend of chrome://sync-internals. A -// JsBackend can handle messages and can emit events to a -// JsEventHandler. -class JsBackend { - public: - // Starts emitting events to the given handler, if initialized. - virtual void SetJsEventHandler( - const WeakHandle& event_handler) = 0; - - // Processes the given message and replies via the given handler, if - // initialized. - virtual void ProcessJsMessage( - const std::string& name, const JsArgList& args, - const WeakHandle& reply_handler) = 0; - - protected: - virtual ~JsBackend() {} -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_JS_BACKEND_H_ diff --git a/chrome/browser/sync/js_controller.h b/chrome/browser/sync/js_controller.h deleted file mode 100644 index 65a43da..0000000 --- a/chrome/browser/sync/js_controller.h +++ /dev/null @@ -1,50 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_JS_CONTROLLER_H_ -#define CHROME_BROWSER_SYNC_JS_CONTROLLER_H_ -#pragma once - -// See README.js for design comments. - -#include - -namespace browser_sync { - -class JsArgList; -class JsEventHandler; -class JsReplyHandler; -template class WeakHandle; - -// An interface for objects that JsEventHandlers directly interact -// with. JsEventHandlers can add themselves to receive events and -// also send messages which will eventually reach the backend. -class JsController { - public: - // Adds an event handler which will start receiving JS events (not - // immediately, so this can be called in the handler's constructor). - // Multiple event handlers are supported, but each event handler - // must be added at most once. - // - // Ideally, we'd take WeakPtrs, but we need the raw pointer values - // to be able to look them up for removal. - virtual void AddJsEventHandler(JsEventHandler* event_handler) = 0; - - // Removes the given event handler if it has been added. It will - // immediately stop receiving any JS events. - virtual void RemoveJsEventHandler(JsEventHandler* event_handler) = 0; - - // Processes a JS message. The reply (if any) will be sent to - // |reply_handler| if it is initialized. - virtual void ProcessJsMessage( - const std::string& name, const JsArgList& args, - const WeakHandle& reply_handler) = 0; - - protected: - virtual ~JsController() {} -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_JS_CONTROLLER_H_ diff --git a/chrome/browser/sync/js_event_details.cc b/chrome/browser/sync/js_event_details.cc deleted file mode 100644 index 371716d..0000000 --- a/chrome/browser/sync/js_event_details.cc +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/js_event_details.h" - -#include "base/json/json_writer.h" - -namespace browser_sync { - -JsEventDetails::JsEventDetails() - : details_(new SharedValue()) {} - -JsEventDetails::JsEventDetails(DictionaryValue* details) - : details_(new SharedValue(details)) {} - -JsEventDetails::~JsEventDetails() {} - -const DictionaryValue& JsEventDetails::Get() const { - return details_->Get(); -} - -std::string JsEventDetails::ToString() const { - std::string str; - base::JSONWriter::Write(&Get(), false, &str); - return str; -} - -} // namespace browser_sync diff --git a/chrome/browser/sync/js_event_details.h b/chrome/browser/sync/js_event_details.h deleted file mode 100644 index 56129ca..0000000 --- a/chrome/browser/sync/js_event_details.h +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_JS_EVENT_DETAILS_H_ -#define CHROME_BROWSER_SYNC_JS_EVENT_DETAILS_H_ -#pragma once - -// See README.js for design comments. - -#include - -#include "base/memory/ref_counted.h" -#include "base/values.h" -#include "chrome/browser/sync/shared_value.h" - -namespace browser_sync { - -// A thread-safe wrapper around an immutable DictionaryValue. Used -// for passing around event details to different threads. -class JsEventDetails { - public: - // Uses an empty dictionary. - JsEventDetails(); - - // Takes over the data in |details|, leaving |details| empty. - explicit JsEventDetails(DictionaryValue* details); - - ~JsEventDetails(); - - const DictionaryValue& Get() const; - - std::string ToString() const; - - // Copy constructor and assignment operator welcome. - - private: - scoped_refptr > details_; -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_JS_EVENT_DETAILS_H_ diff --git a/chrome/browser/sync/js_event_details_unittest.cc b/chrome/browser/sync/js_event_details_unittest.cc deleted file mode 100644 index 60ac2e4..0000000 --- a/chrome/browser/sync/js_event_details_unittest.cc +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/js_event_details.h" - -#include "base/memory/scoped_ptr.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace browser_sync { -namespace { - -class JsEventDetailsTest : public testing::Test {}; - -TEST_F(JsEventDetailsTest, EmptyList) { - JsEventDetails details; - EXPECT_TRUE(details.Get().empty()); - EXPECT_EQ("{}", details.ToString()); -} - -TEST_F(JsEventDetailsTest, FromDictionary) { - DictionaryValue dict; - dict.SetString("foo", "bar"); - dict.Set("baz", new ListValue()); - - scoped_ptr dict_copy(dict.DeepCopy()); - - JsEventDetails details(&dict); - - // |details| should take over |dict|'s data. - EXPECT_TRUE(dict.empty()); - EXPECT_TRUE(details.Get().Equals(dict_copy.get())); -} - -} // namespace -} // namespace browser_sync diff --git a/chrome/browser/sync/js_event_handler.h b/chrome/browser/sync/js_event_handler.h deleted file mode 100644 index 5c3f504..0000000 --- a/chrome/browser/sync/js_event_handler.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_JS_EVENT_HANDLER_H_ -#define CHROME_BROWSER_SYNC_JS_EVENT_HANDLER_H_ -#pragma once - -// See README.js for design comments. - -#include - -namespace browser_sync { - -class JsEventDetails; - -// An interface for objects that handle Javascript events (e.g., -// WebUIs). -class JsEventHandler { - public: - virtual void HandleJsEvent( - const std::string& name, const JsEventDetails& details) = 0; - - protected: - virtual ~JsEventHandler() {} -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_JS_EVENT_HANDLER_H_ diff --git a/chrome/browser/sync/js_reply_handler.h b/chrome/browser/sync/js_reply_handler.h deleted file mode 100644 index 5d50fc8..0000000 --- a/chrome/browser/sync/js_reply_handler.h +++ /dev/null @@ -1,30 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_JS_REPLY_HANDLER_H_ -#define CHROME_BROWSER_SYNC_JS_REPLY_HANDLER_H_ -#pragma once - -// See README.js for design comments. - -#include - -namespace browser_sync { - -class JsArgList; - -// An interface for objects that handle Javascript message replies -// (e.g., WebUIs). -class JsReplyHandler { - public: - virtual void HandleJsReply( - const std::string& name, const JsArgList& args) = 0; - - protected: - virtual ~JsReplyHandler() {} -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_JS_REPLY_HANDLER_H_ diff --git a/chrome/browser/sync/js_sync_manager_observer.cc b/chrome/browser/sync/js_sync_manager_observer.cc deleted file mode 100644 index 4c2ff6a..0000000 --- a/chrome/browser/sync/js_sync_manager_observer.cc +++ /dev/null @@ -1,171 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/js_sync_manager_observer.h" - -#include - -#include "base/logging.h" -#include "base/tracked.h" -#include "base/values.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_event_details.h" -#include "chrome/browser/sync/js_event_handler.h" -#include "chrome/browser/sync/sessions/session_state.h" -#include "chrome/browser/sync/syncable/model_type.h" - -namespace browser_sync { - -JsSyncManagerObserver::JsSyncManagerObserver() {} - -JsSyncManagerObserver::~JsSyncManagerObserver() {} - -void JsSyncManagerObserver::SetJsEventHandler( - const WeakHandle& event_handler) { - event_handler_ = event_handler; -} - -void JsSyncManagerObserver::OnChangesApplied( - syncable::ModelType model_type, - const sync_api::BaseTransaction* trans, - const sync_api::SyncManager::ChangeRecord* changes, - int change_count) { - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.SetString("modelType", syncable::ModelTypeToString(model_type)); - ListValue* change_values = new ListValue(); - details.Set("changes", change_values); - for (int i = 0; i < change_count; ++i) { - change_values->Append(changes[i].ToValue(trans)); - } - HandleJsEvent(FROM_HERE, "onChangesApplied", JsEventDetails(&details)); -} - -void JsSyncManagerObserver::OnChangesComplete( - syncable::ModelType model_type) { - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.SetString("modelType", syncable::ModelTypeToString(model_type)); - HandleJsEvent(FROM_HERE, "onChangesComplete", JsEventDetails(&details)); -} - -void JsSyncManagerObserver::OnSyncCycleCompleted( - const sessions::SyncSessionSnapshot* snapshot) { - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.Set("snapshot", snapshot->ToValue()); - HandleJsEvent(FROM_HERE, "onSyncCycleCompleted", JsEventDetails(&details)); -} - -void JsSyncManagerObserver::OnAuthError( - const GoogleServiceAuthError& auth_error) { - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.Set("authError", auth_error.ToValue()); - HandleJsEvent(FROM_HERE, "onAuthError", JsEventDetails(&details)); -} - -void JsSyncManagerObserver::OnUpdatedToken(const std::string& token) { - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.SetString("token", ""); - HandleJsEvent(FROM_HERE, "onUpdatedToken", JsEventDetails(&details)); -} - -void JsSyncManagerObserver::OnPassphraseRequired( - sync_api::PassphraseRequiredReason reason) { - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.SetString("reason", - sync_api::PassphraseRequiredReasonToString(reason)); - HandleJsEvent(FROM_HERE, "onPassphraseRequired", JsEventDetails(&details)); -} - -void JsSyncManagerObserver::OnPassphraseAccepted( - const std::string& bootstrap_token) { - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.SetString("bootstrapToken", ""); - HandleJsEvent(FROM_HERE, "onPassphraseAccepted", JsEventDetails(&details)); -} - -void JsSyncManagerObserver::OnEncryptionComplete( - const syncable::ModelTypeSet& encrypted_types) { - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.Set("encryptedTypes", - syncable::ModelTypeSetToValue(encrypted_types)); - HandleJsEvent(FROM_HERE, "onEncryptionComplete", JsEventDetails(&details)); -} - -void JsSyncManagerObserver::OnMigrationNeededForTypes( - const syncable::ModelTypeSet& types) { - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.Set("types", syncable::ModelTypeSetToValue(types)); - HandleJsEvent(FROM_HERE, "onMigrationNeededForTypes", - JsEventDetails(&details)); -} - -void JsSyncManagerObserver::OnInitializationComplete( - const WeakHandle& js_backend) { - if (!event_handler_.IsInitialized()) { - return; - } - // Ignore the |js_backend| argument; it's not really convertible to - // JSON anyway. - HandleJsEvent(FROM_HERE, "onInitializationComplete", JsEventDetails()); -} - -void JsSyncManagerObserver::OnStopSyncingPermanently() { - if (!event_handler_.IsInitialized()) { - return; - } - HandleJsEvent(FROM_HERE, "onStopSyncingPermanently", JsEventDetails()); -} - -void JsSyncManagerObserver::OnClearServerDataSucceeded() { - if (!event_handler_.IsInitialized()) { - return; - } - HandleJsEvent(FROM_HERE, "onClearServerDataSucceeded", JsEventDetails()); -} - -void JsSyncManagerObserver::OnClearServerDataFailed() { - if (!event_handler_.IsInitialized()) { - return; - } - HandleJsEvent(FROM_HERE, "onClearServerDataFailed", JsEventDetails()); -} - -void JsSyncManagerObserver::HandleJsEvent( - const tracked_objects::Location& from_here, - const std::string& name, const JsEventDetails& details) { - if (!event_handler_.IsInitialized()) { - NOTREACHED(); - return; - } - event_handler_.Call(from_here, - &JsEventHandler::HandleJsEvent, name, details); -} - -} // namespace browser_sync diff --git a/chrome/browser/sync/js_sync_manager_observer.h b/chrome/browser/sync/js_sync_manager_observer.h deleted file mode 100644 index 29422e8..0000000 --- a/chrome/browser/sync/js_sync_manager_observer.h +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_JS_SYNC_MANAGER_OBSERVER_H_ -#define CHROME_BROWSER_SYNC_JS_SYNC_MANAGER_OBSERVER_H_ -#pragma once - -#include - -#include "base/basictypes.h" -#include "chrome/browser/sync/engine/syncapi.h" -#include "chrome/browser/sync/weak_handle.h" - -namespace tracked_objects { -class Location; -} // namespace tracked_objects - -namespace browser_sync { - -class JsEventDetails; -class JsEventHandler; - -// Routes SyncManager events to a JsEventHandler. -class JsSyncManagerObserver : public sync_api::SyncManager::Observer { - public: - JsSyncManagerObserver(); - virtual ~JsSyncManagerObserver(); - - void SetJsEventHandler(const WeakHandle& event_handler); - - // sync_api::SyncManager::Observer implementation. - virtual void OnChangesApplied( - syncable::ModelType model_type, - const sync_api::BaseTransaction* trans, - const sync_api::SyncManager::ChangeRecord* changes, - int change_count); - virtual void OnChangesComplete(syncable::ModelType model_type); - virtual void OnSyncCycleCompleted( - const sessions::SyncSessionSnapshot* snapshot); - virtual void OnAuthError(const GoogleServiceAuthError& auth_error); - virtual void OnUpdatedToken(const std::string& token); - virtual void OnPassphraseRequired(sync_api::PassphraseRequiredReason reason); - virtual void OnPassphraseAccepted(const std::string& bootstrap_token); - virtual void OnEncryptionComplete( - const syncable::ModelTypeSet& encrypted_types); - virtual void OnInitializationComplete( - const WeakHandle& js_backend); - virtual void OnStopSyncingPermanently(); - virtual void OnClearServerDataSucceeded(); - virtual void OnClearServerDataFailed(); - virtual void OnMigrationNeededForTypes(const syncable::ModelTypeSet& types); - - private: - void HandleJsEvent(const tracked_objects::Location& from_here, - const std::string& name, const JsEventDetails& details); - - WeakHandle event_handler_; - - DISALLOW_COPY_AND_ASSIGN(JsSyncManagerObserver); -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_JS_SYNC_MANAGER_OBSERVER_H_ diff --git a/chrome/browser/sync/js_sync_manager_observer_unittest.cc b/chrome/browser/sync/js_sync_manager_observer_unittest.cc deleted file mode 100644 index 2057dc7..0000000 --- a/chrome/browser/sync/js_sync_manager_observer_unittest.cc +++ /dev/null @@ -1,334 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/js_sync_manager_observer.h" - -#include - -#include "base/basictypes.h" -#include "base/message_loop.h" -#include "base/tracked.h" -#include "base/values.h" -#include "chrome/browser/sync/engine/syncapi.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_event_details.h" -#include "chrome/browser/sync/js_test_util.h" -#include "chrome/browser/sync/sessions/session_state.h" -#include "chrome/browser/sync/syncable/model_type.h" -#include "chrome/browser/sync/weak_handle.h" -#include "chrome/test/sync/engine/test_user_share.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace browser_sync { -namespace { - -using ::testing::InSequence; -using ::testing::StrictMock; - -class JsSyncManagerObserverTest : public testing::Test { - protected: - JsSyncManagerObserverTest() { - js_sync_manager_observer_.SetJsEventHandler( - mock_js_event_handler_.AsWeakHandle()); - } - - private: - // This must be destroyed after the member variables below in order - // for WeakHandles to be destroyed properly. - MessageLoop message_loop_; - - protected: - StrictMock mock_js_event_handler_; - JsSyncManagerObserver js_sync_manager_observer_; - - void PumpLoop() { - message_loop_.RunAllPending(); - } -}; - -TEST_F(JsSyncManagerObserverTest, NoArgNotifiations) { - InSequence dummy; - - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onInitializationComplete", - HasDetails(JsEventDetails()))); - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onStopSyncingPermanently", - HasDetails(JsEventDetails()))); - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onClearServerDataSucceeded", - HasDetails(JsEventDetails()))); - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onClearServerDataFailed", - HasDetails(JsEventDetails()))); - - js_sync_manager_observer_.OnInitializationComplete(WeakHandle()); - js_sync_manager_observer_.OnStopSyncingPermanently(); - js_sync_manager_observer_.OnClearServerDataSucceeded(); - js_sync_manager_observer_.OnClearServerDataFailed(); - PumpLoop(); -} - -TEST_F(JsSyncManagerObserverTest, OnChangesComplete) { - InSequence dummy; - - for (int i = syncable::FIRST_REAL_MODEL_TYPE; - i < syncable::MODEL_TYPE_COUNT; ++i) { - DictionaryValue expected_details; - expected_details.SetString( - "modelType", - syncable::ModelTypeToString(syncable::ModelTypeFromInt(i))); - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onChangesComplete", - HasDetailsAsDictionary(expected_details))); - } - - for (int i = syncable::FIRST_REAL_MODEL_TYPE; - i < syncable::MODEL_TYPE_COUNT; ++i) { - js_sync_manager_observer_.OnChangesComplete(syncable::ModelTypeFromInt(i)); - } - PumpLoop(); -} - -TEST_F(JsSyncManagerObserverTest, OnSyncCycleCompleted) { - std::string download_progress_markers[syncable::MODEL_TYPE_COUNT]; - sessions::SyncSessionSnapshot snapshot(sessions::SyncerStatus(), - sessions::ErrorCounters(), - 100, - false, - syncable::ModelTypeBitSet(), - download_progress_markers, - false, - true, - 100, - 8, - 5, - false, - sessions::SyncSourceInfo(), - 0); - DictionaryValue expected_details; - expected_details.Set("snapshot", snapshot.ToValue()); - - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onSyncCycleCompleted", - HasDetailsAsDictionary(expected_details))); - - js_sync_manager_observer_.OnSyncCycleCompleted(&snapshot); - PumpLoop(); -} - -TEST_F(JsSyncManagerObserverTest, OnAuthError) { - GoogleServiceAuthError error(GoogleServiceAuthError::TWO_FACTOR); - DictionaryValue expected_details; - expected_details.Set("authError", error.ToValue()); - - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onAuthError", - HasDetailsAsDictionary(expected_details))); - - js_sync_manager_observer_.OnAuthError(error); - PumpLoop(); -} - -TEST_F(JsSyncManagerObserverTest, OnPassphraseRequired) { - InSequence dummy; - - DictionaryValue reason_passphrase_not_required_details; - DictionaryValue reason_encryption_details; - DictionaryValue reason_decryption_details; - DictionaryValue reason_set_passphrase_failed_details; - - reason_passphrase_not_required_details.SetString( - "reason", - sync_api::PassphraseRequiredReasonToString( - sync_api::REASON_PASSPHRASE_NOT_REQUIRED)); - reason_encryption_details.SetString( - "reason", - sync_api::PassphraseRequiredReasonToString(sync_api::REASON_ENCRYPTION)); - reason_decryption_details.SetString( - "reason", - sync_api::PassphraseRequiredReasonToString(sync_api::REASON_DECRYPTION)); - reason_set_passphrase_failed_details.SetString( - "reason", - sync_api::PassphraseRequiredReasonToString( - sync_api::REASON_SET_PASSPHRASE_FAILED)); - - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onPassphraseRequired", - HasDetailsAsDictionary( - reason_passphrase_not_required_details))); - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onPassphraseRequired", - HasDetailsAsDictionary(reason_encryption_details))); - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onPassphraseRequired", - HasDetailsAsDictionary(reason_decryption_details))); - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onPassphraseRequired", - HasDetailsAsDictionary( - reason_set_passphrase_failed_details))); - - js_sync_manager_observer_.OnPassphraseRequired( - sync_api::REASON_PASSPHRASE_NOT_REQUIRED); - js_sync_manager_observer_.OnPassphraseRequired(sync_api::REASON_ENCRYPTION); - js_sync_manager_observer_.OnPassphraseRequired(sync_api::REASON_DECRYPTION); - js_sync_manager_observer_.OnPassphraseRequired( - sync_api::REASON_SET_PASSPHRASE_FAILED); - PumpLoop(); -} - -TEST_F(JsSyncManagerObserverTest, SensitiveNotifiations) { - DictionaryValue redacted_token_details; - redacted_token_details.SetString("token", ""); - DictionaryValue redacted_bootstrap_token_details; - redacted_bootstrap_token_details.SetString("bootstrapToken", ""); - - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onUpdatedToken", - HasDetailsAsDictionary(redacted_token_details))); - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent( - "onPassphraseAccepted", - HasDetailsAsDictionary(redacted_bootstrap_token_details))); - - js_sync_manager_observer_.OnUpdatedToken("sensitive_token"); - js_sync_manager_observer_.OnPassphraseAccepted("sensitive_token"); - PumpLoop(); -} - -TEST_F(JsSyncManagerObserverTest, OnEncryptionComplete) { - DictionaryValue expected_details; - ListValue* encrypted_type_values = new ListValue(); - expected_details.Set("encryptedTypes", encrypted_type_values); - syncable::ModelTypeSet encrypted_types; - - for (int i = syncable::FIRST_REAL_MODEL_TYPE; - i < syncable::MODEL_TYPE_COUNT; ++i) { - syncable::ModelType type = syncable::ModelTypeFromInt(i); - encrypted_types.insert(type); - encrypted_type_values->Append(Value::CreateStringValue( - syncable::ModelTypeToString(type))); - } - - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onEncryptionComplete", - HasDetailsAsDictionary(expected_details))); - - js_sync_manager_observer_.OnEncryptionComplete(encrypted_types); - PumpLoop(); -} - -TEST_F(JsSyncManagerObserverTest, OnMigrationNeededForTypes) { - DictionaryValue expected_details; - ListValue* type_values = new ListValue(); - expected_details.Set("types", type_values); - syncable::ModelTypeSet types; - - for (int i = syncable::FIRST_REAL_MODEL_TYPE; - i < syncable::MODEL_TYPE_COUNT; ++i) { - syncable::ModelType type = syncable::ModelTypeFromInt(i); - types.insert(type); - type_values->Append(Value::CreateStringValue( - syncable::ModelTypeToString(type))); - } - - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onMigrationNeededForTypes", - HasDetailsAsDictionary(expected_details))); - - js_sync_manager_observer_.OnMigrationNeededForTypes(types); - PumpLoop(); -} - -namespace { - -// Makes a node of the given model type. Returns the id of the -// newly-created node. -int64 MakeNode(sync_api::UserShare* share, syncable::ModelType model_type) { - sync_api::WriteTransaction trans(FROM_HERE, share); - sync_api::ReadNode root_node(&trans); - root_node.InitByRootLookup(); - sync_api::WriteNode node(&trans); - EXPECT_TRUE(node.InitUniqueByCreation( - model_type, root_node, - syncable::ModelTypeToString(model_type))); - node.SetIsFolder(false); - return node.GetId(); -} - -} // namespace - -TEST_F(JsSyncManagerObserverTest, OnChangesApplied) { - InSequence dummy; - - TestUserShare test_user_share; - test_user_share.SetUp(); - - // We don't test with passwords as that requires additional setup. - - // Build a list of example ChangeRecords. - sync_api::SyncManager::ChangeRecord changes[syncable::MODEL_TYPE_COUNT]; - for (int i = syncable::AUTOFILL_PROFILE; - i < syncable::MODEL_TYPE_COUNT; ++i) { - changes[i].id = - MakeNode(test_user_share.user_share(), syncable::ModelTypeFromInt(i)); - switch (i % 3) { - case 0: - changes[i].action = - sync_api::SyncManager::ChangeRecord::ACTION_ADD; - break; - case 1: - changes[i].action = - sync_api::SyncManager::ChangeRecord::ACTION_UPDATE; - break; - default: - changes[i].action = - sync_api::SyncManager::ChangeRecord::ACTION_DELETE; - break; - } - { - sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); - sync_api::ReadNode node(&trans); - EXPECT_TRUE(node.InitByIdLookup(changes[i].id)); - changes[i].specifics = node.GetEntry()->Get(syncable::SPECIFICS); - } - } - - // For each i, we call OnChangesApplied() with the first arg equal - // to i cast to ModelType and the second argument with the changes - // starting from changes[i]. - - // Set expectations for each data type. - for (int i = syncable::AUTOFILL_PROFILE; - i < syncable::MODEL_TYPE_COUNT; ++i) { - const std::string& model_type_str = - syncable::ModelTypeToString(syncable::ModelTypeFromInt(i)); - DictionaryValue expected_details; - expected_details.SetString("modelType", model_type_str); - ListValue* expected_changes = new ListValue(); - expected_details.Set("changes", expected_changes); - for (int j = i; j < syncable::MODEL_TYPE_COUNT; ++j) { - sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); - expected_changes->Append(changes[j].ToValue(&trans)); - } - EXPECT_CALL(mock_js_event_handler_, - HandleJsEvent("onChangesApplied", - HasDetailsAsDictionary(expected_details))); - } - - // Fire OnChangesApplied() for each data type. - for (int i = syncable::AUTOFILL_PROFILE; - i < syncable::MODEL_TYPE_COUNT; ++i) { - sync_api::ReadTransaction trans(FROM_HERE, test_user_share.user_share()); - js_sync_manager_observer_.OnChangesApplied(syncable::ModelTypeFromInt(i), - &trans, &changes[i], - syncable::MODEL_TYPE_COUNT - i); - } - - test_user_share.TearDown(); - PumpLoop(); -} - -} // namespace -} // namespace browser_sync diff --git a/chrome/browser/sync/js_test_util.cc b/chrome/browser/sync/js_test_util.cc deleted file mode 100644 index eea7576..0000000 --- a/chrome/browser/sync/js_test_util.cc +++ /dev/null @@ -1,137 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/js_test_util.h" - -#include "base/basictypes.h" -#include "base/memory/scoped_ptr.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_event_details.h" - -namespace browser_sync { - -void PrintTo(const JsArgList& args, ::std::ostream* os) { - *os << args.ToString(); -} - -void PrintTo(const JsEventDetails& details, ::std::ostream* os) { - *os << details.ToString(); -} - -namespace { - -// Matcher implementation for HasArgs(). -class HasArgsMatcher - : public ::testing::MatcherInterface { - public: - explicit HasArgsMatcher(const JsArgList& expected_args) - : expected_args_(expected_args) {} - - virtual ~HasArgsMatcher() {} - - virtual bool MatchAndExplain( - const JsArgList& args, - ::testing::MatchResultListener* listener) const { - // No need to annotate listener since we already define PrintTo(). - return args.Get().Equals(&expected_args_.Get()); - } - - virtual void DescribeTo(::std::ostream* os) const { - *os << "has args " << expected_args_.ToString(); - } - - virtual void DescribeNegationTo(::std::ostream* os) const { - *os << "doesn't have args " << expected_args_.ToString(); - } - - private: - const JsArgList expected_args_; - - DISALLOW_COPY_AND_ASSIGN(HasArgsMatcher); -}; - -// Matcher implementation for HasDetails(). -class HasDetailsMatcher - : public ::testing::MatcherInterface { - public: - explicit HasDetailsMatcher(const JsEventDetails& expected_details) - : expected_details_(expected_details) {} - - virtual ~HasDetailsMatcher() {} - - virtual bool MatchAndExplain( - const JsEventDetails& details, - ::testing::MatchResultListener* listener) const { - // No need to annotate listener since we already define PrintTo(). - return details.Get().Equals(&expected_details_.Get()); - } - - virtual void DescribeTo(::std::ostream* os) const { - *os << "has details " << expected_details_.ToString(); - } - - virtual void DescribeNegationTo(::std::ostream* os) const { - *os << "doesn't have details " << expected_details_.ToString(); - } - - private: - const JsEventDetails expected_details_; - - DISALLOW_COPY_AND_ASSIGN(HasDetailsMatcher); -}; - -} // namespace - -::testing::Matcher HasArgs(const JsArgList& expected_args) { - return ::testing::MakeMatcher(new HasArgsMatcher(expected_args)); -} - -::testing::Matcher HasArgsAsList( - const ListValue& expected_args) { - scoped_ptr expected_args_copy(expected_args.DeepCopy()); - return HasArgs(JsArgList(expected_args_copy.get())); -} - -::testing::Matcher HasDetails( - const JsEventDetails& expected_details) { - return ::testing::MakeMatcher(new HasDetailsMatcher(expected_details)); -} - -::testing::Matcher HasDetailsAsDictionary( - const DictionaryValue& expected_details) { - scoped_ptr expected_details_copy( - expected_details.DeepCopy()); - return HasDetails(JsEventDetails(expected_details_copy.get())); -} - -MockJsBackend::MockJsBackend() {} - -MockJsBackend::~MockJsBackend() {} - -WeakHandle MockJsBackend::AsWeakHandle() { - return WeakHandle(AsWeakPtr()); -} - -MockJsController::MockJsController() {} - -MockJsController::~MockJsController() {} - -MockJsEventHandler::MockJsEventHandler() {} - -WeakHandle MockJsEventHandler::AsWeakHandle() { - return WeakHandle(AsWeakPtr()); -} - -MockJsEventHandler::~MockJsEventHandler() {} - -MockJsReplyHandler::MockJsReplyHandler() {} - -MockJsReplyHandler::~MockJsReplyHandler() {} - -WeakHandle MockJsReplyHandler::AsWeakHandle() { - return WeakHandle(AsWeakPtr()); -} - -} // namespace browser_sync - diff --git a/chrome/browser/sync/js_test_util.h b/chrome/browser/sync/js_test_util.h deleted file mode 100644 index 6ac1a99..0000000 --- a/chrome/browser/sync/js_test_util.h +++ /dev/null @@ -1,109 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_JS_TEST_UTIL_H_ -#define CHROME_BROWSER_SYNC_JS_TEST_UTIL_H_ -#pragma once - -#include -#include - -#include "base/memory/weak_ptr.h" -#include "chrome/browser/sync/js_backend.h" -#include "chrome/browser/sync/js_controller.h" -#include "chrome/browser/sync/js_event_handler.h" -#include "chrome/browser/sync/js_reply_handler.h" -#include "chrome/browser/sync/weak_handle.h" -#include "testing/gmock/include/gmock/gmock.h" - -namespace base { -class DictionaryValue; -class ListValue; -} - -namespace browser_sync { - -class JsArgList; -class JsEventDetails; - -// Defined for googletest. Equivalent to "*os << args.ToString()". -void PrintTo(const JsArgList& args, ::std::ostream* os); -void PrintTo(const JsEventDetails& details, ::std::ostream* os); - -// A gmock matcher for JsArgList. Use like: -// -// EXPECT_CALL(mock, HandleJsReply("foo", HasArgs(expected_args))); -::testing::Matcher HasArgs(const JsArgList& expected_args); - -// Like HasArgs() but takes a ListValue instead. -::testing::Matcher HasArgsAsList( - const base::ListValue& expected_args); - -// A gmock matcher for JsEventDetails. Use like: -// -// EXPECT_CALL(mock, HandleJsEvent("foo", HasArgs(expected_details))); -::testing::Matcher HasDetails( - const JsEventDetails& expected_details); - -// Like HasDetails() but takes a DictionaryValue instead. -::testing::Matcher HasDetailsAsDictionary( - const base::DictionaryValue& expected_details); - -// Mocks. - -class MockJsBackend : public JsBackend, - public base::SupportsWeakPtr { - public: - MockJsBackend(); - virtual ~MockJsBackend(); - - WeakHandle AsWeakHandle(); - - MOCK_METHOD1(SetJsEventHandler, void(const WeakHandle&)); - MOCK_METHOD3(ProcessJsMessage, void(const ::std::string&, const JsArgList&, - const WeakHandle&)); -}; - -class MockJsController : public JsController, - public base::SupportsWeakPtr { - public: - MockJsController(); - virtual ~MockJsController(); - - MOCK_METHOD1(AddJsEventHandler, void(JsEventHandler*)); - MOCK_METHOD1(RemoveJsEventHandler, void(JsEventHandler*)); - MOCK_METHOD3(ProcessJsMessage, - void(const ::std::string&, const JsArgList&, - const WeakHandle&)); -}; - -class MockJsEventHandler - : public JsEventHandler, - public base::SupportsWeakPtr { - public: - MockJsEventHandler(); - virtual ~MockJsEventHandler(); - - WeakHandle AsWeakHandle(); - - MOCK_METHOD2(HandleJsEvent, - void(const ::std::string&, const JsEventDetails&)); -}; - -class MockJsReplyHandler - : public JsReplyHandler, - public base::SupportsWeakPtr { - public: - MockJsReplyHandler(); - virtual ~MockJsReplyHandler(); - - WeakHandle AsWeakHandle(); - - MOCK_METHOD2(HandleJsReply, - void(const ::std::string&, const JsArgList&)); -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_JS_TEST_UTIL_H_ diff --git a/chrome/browser/sync/js_transaction_observer.cc b/chrome/browser/sync/js_transaction_observer.cc deleted file mode 100644 index e0700fc..0000000 --- a/chrome/browser/sync/js_transaction_observer.cc +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "chrome/browser/sync/js_transaction_observer.h" - -#include -#include - -#include "base/logging.h" -#include "base/tracked.h" -#include "base/values.h" -#include "chrome/browser/sync/js_event_details.h" -#include "chrome/browser/sync/js_event_handler.h" - -namespace browser_sync { - -JsTransactionObserver::JsTransactionObserver() {} - -JsTransactionObserver::~JsTransactionObserver() { - DCHECK(non_thread_safe_.CalledOnValidThread()); -} - -void JsTransactionObserver::SetJsEventHandler( - const WeakHandle& event_handler) { - event_handler_ = event_handler; -} - -namespace { - -std::string GetLocationString(const tracked_objects::Location& location) { - std::ostringstream oss; - oss << location.function_name() << "@" - << location.file_name() << ":" << location.line_number(); - return oss.str(); -} - -} // namespace - -void JsTransactionObserver::OnTransactionStart( - const tracked_objects::Location& location, - const syncable::WriterTag& writer) { - DCHECK(non_thread_safe_.CalledOnValidThread()); - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.SetString("location", GetLocationString(location)); - details.SetString("writer", syncable::WriterTagToString(writer)); - HandleJsEvent(FROM_HERE, "onTransactionStart", JsEventDetails(&details)); -} - -void JsTransactionObserver::OnTransactionMutate( - const tracked_objects::Location& location, - const syncable::WriterTag& writer, - const syncable::EntryKernelMutationSet& mutations, - const syncable::ModelTypeBitSet& models_with_changes) { - DCHECK(non_thread_safe_.CalledOnValidThread()); - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.SetString("location", GetLocationString(location)); - details.SetString("writer", syncable::WriterTagToString(writer)); - details.Set("mutations", syncable::EntryKernelMutationSetToValue(mutations)); - details.Set("modelsWithChanges", - syncable::ModelTypeBitSetToValue(models_with_changes)); - HandleJsEvent(FROM_HERE, "onTransactionMutate", JsEventDetails(&details)); -} - -void JsTransactionObserver::OnTransactionEnd( - const tracked_objects::Location& location, - const syncable::WriterTag& writer) { - DCHECK(non_thread_safe_.CalledOnValidThread()); - if (!event_handler_.IsInitialized()) { - return; - } - DictionaryValue details; - details.SetString("location", GetLocationString(location)); - details.SetString("writer", syncable::WriterTagToString(writer)); - HandleJsEvent(FROM_HERE, "onTransactionEnd", JsEventDetails(&details)); -} - -void JsTransactionObserver::HandleJsEvent( - const tracked_objects::Location& from_here, - const std::string& name, const JsEventDetails& details) { - if (!event_handler_.IsInitialized()) { - NOTREACHED(); - return; - } - event_handler_.Call(from_here, - &JsEventHandler::HandleJsEvent, name, details); -} - -} // namespace browser_sync diff --git a/chrome/browser/sync/js_transaction_observer.h b/chrome/browser/sync/js_transaction_observer.h deleted file mode 100644 index bf59a6d..0000000 --- a/chrome/browser/sync/js_transaction_observer.h +++ /dev/null @@ -1,61 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef CHROME_BROWSER_SYNC_JS_TRANSACTION_OBSERVER_H_ -#define CHROME_BROWSER_SYNC_JS_TRANSACTION_OBSERVER_H_ -#pragma once - -#include - -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "base/threading/non_thread_safe.h" -#include "chrome/browser/sync/syncable/transaction_observer.h" -#include "chrome/browser/sync/weak_handle.h" - -namespace tracked_objects { -class Location; -} // namespace tracked_objects - -namespace browser_sync { - -class JsEventDetails; -class JsEventHandler; - -// Routes SyncManager events to a JsEventHandler. -class JsTransactionObserver : public syncable::TransactionObserver { - public: - JsTransactionObserver(); - - virtual ~JsTransactionObserver(); - - void SetJsEventHandler(const WeakHandle& event_handler); - - // syncable::TransactionObserver implementation. - virtual void OnTransactionStart( - const tracked_objects::Location& location, - const syncable::WriterTag& writer) OVERRIDE; - virtual void OnTransactionMutate( - const tracked_objects::Location& location, - const syncable::WriterTag& writer, - const syncable::EntryKernelMutationSet& mutations, - const syncable::ModelTypeBitSet& models_with_changes) OVERRIDE; - virtual void OnTransactionEnd( - const tracked_objects::Location& location, - const syncable::WriterTag& writer) OVERRIDE; - - private: - base::NonThreadSafe non_thread_safe_; - WeakHandle event_handler_; - - void HandleJsEvent( - const tracked_objects::Location& from_here, - const std::string& name, const JsEventDetails& details); - - DISALLOW_COPY_AND_ASSIGN(JsTransactionObserver); -}; - -} // namespace browser_sync - -#endif // CHROME_BROWSER_SYNC_JS_TRANSACTION_OBSERVER_H_ diff --git a/chrome/browser/sync/profile_sync_service.cc b/chrome/browser/sync/profile_sync_service.cc index c77c510..0578300 100644 --- a/chrome/browser/sync/profile_sync_service.cc +++ b/chrome/browser/sync/profile_sync_service.cc @@ -32,8 +32,8 @@ #include "chrome/browser/sync/glue/data_type_controller.h" #include "chrome/browser/sync/glue/data_type_manager.h" #include "chrome/browser/sync/glue/session_data_type_controller.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_event_details.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_event_details.h" #include "chrome/browser/sync/profile_sync_factory.h" #include "chrome/browser/sync/signin_manager.h" #include "chrome/browser/ui/browser.h" diff --git a/chrome/browser/sync/profile_sync_service_unittest.cc b/chrome/browser/sync/profile_sync_service_unittest.cc index 971f96d..49587ad 100644 --- a/chrome/browser/sync/profile_sync_service_unittest.cc +++ b/chrome/browser/sync/profile_sync_service_unittest.cc @@ -9,9 +9,9 @@ #include "chrome/browser/net/gaia/token_service.h" #include "chrome/browser/sync/glue/bookmark_data_type_controller.h" #include "chrome/browser/sync/glue/data_type_controller.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_event_details.h" -#include "chrome/browser/sync/js_test_util.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_event_details.h" +#include "chrome/browser/sync/js/js_test_util.h" #include "chrome/browser/sync/profile_sync_factory_mock.h" #include "chrome/browser/sync/test_profile_sync_service.h" #include "chrome/common/net/gaia/gaia_constants.h" diff --git a/chrome/browser/sync/sync_js_controller.cc b/chrome/browser/sync/sync_js_controller.cc index 166611c..da2c67b7 100644 --- a/chrome/browser/sync/sync_js_controller.cc +++ b/chrome/browser/sync/sync_js_controller.cc @@ -4,8 +4,8 @@ #include "chrome/browser/sync/sync_js_controller.h" -#include "chrome/browser/sync/js_backend.h" -#include "chrome/browser/sync/js_event_details.h" +#include "chrome/browser/sync/js/js_backend.h" +#include "chrome/browser/sync/js/js_event_details.h" namespace browser_sync { diff --git a/chrome/browser/sync/sync_js_controller.h b/chrome/browser/sync/sync_js_controller.h index 0b31f11..5221543 100644 --- a/chrome/browser/sync/sync_js_controller.h +++ b/chrome/browser/sync/sync_js_controller.h @@ -13,9 +13,9 @@ #include "base/compiler_specific.h" #include "base/memory/weak_ptr.h" #include "base/observer_list.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_controller.h" -#include "chrome/browser/sync/js_event_handler.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_controller.h" +#include "chrome/browser/sync/js/js_event_handler.h" #include "chrome/browser/sync/weak_handle.h" namespace browser_sync { diff --git a/chrome/browser/sync/sync_js_controller_unittest.cc b/chrome/browser/sync/sync_js_controller_unittest.cc index 1dd5ca2..96bc6b3d 100644 --- a/chrome/browser/sync/sync_js_controller_unittest.cc +++ b/chrome/browser/sync/sync_js_controller_unittest.cc @@ -6,9 +6,9 @@ #include "base/message_loop.h" #include "base/values.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_event_details.h" -#include "chrome/browser/sync/js_test_util.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_event_details.h" +#include "chrome/browser/sync/js/js_test_util.h" #include "testing/gmock/include/gmock/gmock.h" #include "testing/gtest/include/gtest/gtest.h" diff --git a/chrome/browser/sync/test_profile_sync_service.cc b/chrome/browser/sync/test_profile_sync_service.cc index cf4af6f..8173f01 100644 --- a/chrome/browser/sync/test_profile_sync_service.cc +++ b/chrome/browser/sync/test_profile_sync_service.cc @@ -8,10 +8,10 @@ #include "chrome/browser/sync/engine/syncapi.h" #include "chrome/browser/sync/glue/data_type_controller.h" #include "chrome/browser/sync/glue/sync_backend_host.h" -#include "chrome/browser/sync/js_reply_handler.h" +#include "chrome/browser/sync/js/js_reply_handler.h" #include "chrome/browser/sync/profile_sync_factory.h" -#include "chrome/browser/sync/signin_manager.h" #include "chrome/browser/sync/sessions/session_state.h" +#include "chrome/browser/sync/signin_manager.h" #include "chrome/browser/sync/syncable/directory_manager.h" #include "chrome/browser/sync/syncable/syncable.h" #include "chrome/common/chrome_notification_types.h" diff --git a/chrome/browser/ui/webui/sync_internals_ui.cc b/chrome/browser/ui/webui/sync_internals_ui.cc index 25adb33..d9029e9 100644 --- a/chrome/browser/ui/webui/sync_internals_ui.cc +++ b/chrome/browser/ui/webui/sync_internals_ui.cc @@ -12,9 +12,9 @@ #include "base/tracked_objects.h" #include "base/values.h" #include "chrome/browser/profiles/profile.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_controller.h" -#include "chrome/browser/sync/js_event_details.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_controller.h" +#include "chrome/browser/sync/js/js_event_details.h" #include "chrome/browser/sync/profile_sync_service.h" #include "chrome/browser/sync/sync_ui_util.h" #include "chrome/browser/sync/weak_handle.h" diff --git a/chrome/browser/ui/webui/sync_internals_ui.h b/chrome/browser/ui/webui/sync_internals_ui.h index e114592..374e6c6 100644 --- a/chrome/browser/ui/webui/sync_internals_ui.h +++ b/chrome/browser/ui/webui/sync_internals_ui.h @@ -8,11 +8,11 @@ #include -#include "base/compiler_specific.h" #include "base/basictypes.h" +#include "base/compiler_specific.h" #include "base/memory/weak_ptr.h" -#include "chrome/browser/sync/js_event_handler.h" -#include "chrome/browser/sync/js_reply_handler.h" +#include "chrome/browser/sync/js/js_event_handler.h" +#include "chrome/browser/sync/js/js_reply_handler.h" #include "chrome/browser/ui/webui/chrome_web_ui.h" class ProfileSyncService; diff --git a/chrome/browser/ui/webui/sync_internals_ui_unittest.cc b/chrome/browser/ui/webui/sync_internals_ui_unittest.cc index 1728f33..eefe4fb 100644 --- a/chrome/browser/ui/webui/sync_internals_ui_unittest.cc +++ b/chrome/browser/ui/webui/sync_internals_ui_unittest.cc @@ -10,9 +10,9 @@ #include "base/message_loop.h" #include "base/utf_string_conversions.h" #include "base/values.h" -#include "chrome/browser/sync/js_arg_list.h" -#include "chrome/browser/sync/js_event_details.h" -#include "chrome/browser/sync/js_test_util.h" +#include "chrome/browser/sync/js/js_arg_list.h" +#include "chrome/browser/sync/js/js_event_details.h" +#include "chrome/browser/sync/js/js_test_util.h" #include "chrome/browser/sync/profile_sync_service_mock.h" #include "chrome/test/base/profile_mock.h" #include "content/browser/browser_thread.h" diff --git a/chrome/chrome.gyp b/chrome/chrome.gyp index 684feba..6f244f6 100644 --- a/chrome/chrome.gyp +++ b/chrome/chrome.gyp @@ -641,18 +641,18 @@ 'browser/sync/engine/update_applicator.h', 'browser/sync/engine/verify_updates_command.cc', 'browser/sync/engine/verify_updates_command.h', - 'browser/sync/js_arg_list.cc', - 'browser/sync/js_arg_list.h', - 'browser/sync/js_backend.h', - 'browser/sync/js_controller.h', - 'browser/sync/js_event_details.cc', - 'browser/sync/js_event_details.h', - 'browser/sync/js_event_handler.h', - 'browser/sync/js_reply_handler.h', - 'browser/sync/js_sync_manager_observer.cc', - 'browser/sync/js_sync_manager_observer.h', - 'browser/sync/js_transaction_observer.cc', - 'browser/sync/js_transaction_observer.h', + 'browser/sync/js/js_arg_list.cc', + 'browser/sync/js/js_arg_list.h', + 'browser/sync/js/js_backend.h', + 'browser/sync/js/js_controller.h', + 'browser/sync/js/js_event_details.cc', + 'browser/sync/js/js_event_details.h', + 'browser/sync/js/js_event_handler.h', + 'browser/sync/js/js_reply_handler.h', + 'browser/sync/js/js_sync_manager_observer.cc', + 'browser/sync/js/js_sync_manager_observer.h', + 'browser/sync/js/js_transaction_observer.cc', + 'browser/sync/js/js_transaction_observer.h', 'browser/sync/protocol/proto_enum_conversions.cc', 'browser/sync/protocol/proto_enum_conversions.h', 'browser/sync/protocol/proto_value_conversions.cc', diff --git a/chrome/chrome_tests.gypi b/chrome/chrome_tests.gypi index f3c2c68..8673879 100644 --- a/chrome/chrome_tests.gypi +++ b/chrome/chrome_tests.gypi @@ -315,8 +315,8 @@ '..', ], 'sources': [ - 'browser/sync/js_test_util.cc', - 'browser/sync/js_test_util.h', + 'browser/sync/js/js_test_util.cc', + 'browser/sync/js/js_test_util.h', 'test/sync/null_directory_change_delegate.cc', 'test/sync/null_directory_change_delegate.h', 'test/sync/engine/test_directory_setter_upper.cc', @@ -3113,9 +3113,9 @@ 'browser/sync/engine/syncproto_unittest.cc', 'browser/sync/engine/syncapi_mock.h', 'browser/sync/engine/verify_updates_command_unittest.cc', - 'browser/sync/js_arg_list_unittest.cc', - 'browser/sync/js_event_details_unittest.cc', - 'browser/sync/js_sync_manager_observer_unittest.cc', + 'browser/sync/js/js_arg_list_unittest.cc', + 'browser/sync/js/js_event_details_unittest.cc', + 'browser/sync/js/js_sync_manager_observer_unittest.cc', 'browser/sync/notifier/cache_invalidation_packet_handler_unittest.cc', 'browser/sync/notifier/chrome_invalidation_client_unittest.cc', 'browser/sync/notifier/chrome_system_resources_unittest.cc', -- cgit v1.1