• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/browser/ui/webui/sync_internals_ui.h"
6 
7 #include <string>
8 
9 #include "base/logging.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/task.h"
12 #include "base/tracked_objects.h"
13 #include "base/values.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sync/js_arg_list.h"
16 #include "chrome/browser/sync/js_frontend.h"
17 #include "chrome/browser/sync/profile_sync_service.h"
18 #include "chrome/browser/sync/sync_ui_util.h"
19 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
20 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
21 #include "chrome/browser/ui/webui/sync_internals_html_source.h"
22 #include "chrome/common/extensions/extension_messages.h"
23 #include "content/browser/browser_thread.h"
24 
SyncInternalsUI(TabContents * contents)25 SyncInternalsUI::SyncInternalsUI(TabContents* contents)
26     : WebUI(contents) {
27   browser_sync::JsFrontend* backend = GetJsFrontend();
28   if (backend) {
29     backend->AddHandler(this);
30   }
31   // If this PostTask() call fails, it's most likely because this is
32   // being run from a unit test.  The created objects will be cleaned
33   // up, anyway.
34   contents->profile()->GetChromeURLDataManager()->AddDataSource(
35       new SyncInternalsHTMLSource());
36 }
37 
~SyncInternalsUI()38 SyncInternalsUI::~SyncInternalsUI() {
39   browser_sync::JsFrontend* backend = GetJsFrontend();
40   if (backend) {
41     backend->RemoveHandler(this);
42   }
43 }
44 
ProcessWebUIMessage(const ExtensionHostMsg_DomMessage_Params & params)45 void SyncInternalsUI::ProcessWebUIMessage(
46     const ExtensionHostMsg_DomMessage_Params& params) {
47   const std::string& name = params.name;
48   browser_sync::JsArgList args(params.arguments);
49   VLOG(1) << "Received message: " << name << " with args "
50           << args.ToString();
51   // We handle this case directly because it needs to work even if
52   // the sync service doesn't exist.
53   if (name == "getAboutInfo") {
54     ListValue args;
55     DictionaryValue* about_info = new DictionaryValue();
56     args.Append(about_info);
57     ProfileSyncService* service = GetProfile()->GetProfileSyncService();
58     sync_ui_util::ConstructAboutInformation(service, about_info);
59     HandleJsEvent("onGetAboutInfoFinished",
60                   browser_sync::JsArgList(args));
61   } else {
62     browser_sync::JsFrontend* backend = GetJsFrontend();
63     if (backend) {
64       backend->ProcessMessage(name, args, this);
65     } else {
66       LOG(WARNING) << "No sync service; dropping message " << name
67                    << " with args " << args.ToString();
68     }
69   }
70 }
71 
HandleJsEvent(const std::string & name,const browser_sync::JsArgList & args)72 void SyncInternalsUI::HandleJsEvent(const std::string& name,
73                                     const browser_sync::JsArgList& args) {
74   VLOG(1) << "Handling event: " << name << " with args " << args.ToString();
75   std::vector<const Value*> arg_list(args.Get().begin(), args.Get().end());
76   CallJavascriptFunction(name, arg_list);
77 }
78 
GetJsFrontend()79 browser_sync::JsFrontend* SyncInternalsUI::GetJsFrontend() {
80   // If this returns NULL that means that sync is disabled for
81   // whatever reason.
82   ProfileSyncService* sync_service = GetProfile()->GetProfileSyncService();
83   return sync_service ? sync_service->GetJsFrontend() : NULL;
84 }
85