Home | History | Annotate | Download | only in webui
      1 // Copyright (c) 2013 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/welcome_handler_android.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/values.h"
      9 #include "chrome/browser/android/tab_android.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/browser/sync/profile_sync_service_factory.h"
     12 #include "content/public/browser/web_ui.h"
     13 
     14 WelcomeHandler::WelcomeHandler()
     15     : sync_service_(NULL),
     16       observer_manager_(this),
     17       is_sync_footer_visible_(false) {}
     18 
     19 WelcomeHandler::~WelcomeHandler() {}
     20 
     21 void WelcomeHandler::RegisterMessages() {
     22   web_ui()->RegisterMessageCallback(
     23       "updateSyncFooterVisibility",
     24       base::Bind(&WelcomeHandler::HandleUpdateSyncFooterVisibility,
     25       base::Unretained(this)));
     26 
     27   web_ui()->RegisterMessageCallback(
     28       "showSyncSettings",
     29       base::Bind(&WelcomeHandler::HandleShowSyncSettings,
     30       base::Unretained(this)));
     31 
     32   web_ui()->RegisterMessageCallback(
     33       "showTermsOfService",
     34       base::Bind(&WelcomeHandler::HandleShowTermsOfService,
     35       base::Unretained(this)));
     36 
     37   // Register for callbacks
     38   sync_service_ = ProfileSyncServiceFactory::GetInstance()->GetForProfile(
     39       Profile::FromWebUI(web_ui()));
     40   if (sync_service_)
     41     observer_manager_.Add(sync_service_);
     42 }
     43 
     44 void WelcomeHandler::HandleUpdateSyncFooterVisibility(const ListValue* args) {
     45   UpdateSyncFooterVisibility(true);
     46 }
     47 
     48 void WelcomeHandler::HandleShowSyncSettings(const ListValue* args) {
     49   TabAndroid::FromWebContents(web_ui()->GetWebContents())->ShowSyncSettings();
     50 }
     51 
     52 void WelcomeHandler::HandleShowTermsOfService(const ListValue* args) {
     53   TabAndroid::FromWebContents(web_ui()->GetWebContents())->ShowTermsOfService();
     54 }
     55 
     56 void WelcomeHandler::OnStateChanged() {
     57   UpdateSyncFooterVisibility(false);
     58 }
     59 
     60 void WelcomeHandler::UpdateSyncFooterVisibility(bool forced) {
     61   bool is_sync_enabled =
     62       sync_service_ && sync_service_->IsSyncEnabledAndLoggedIn();
     63 
     64   if (forced || is_sync_footer_visible_ != is_sync_enabled) {
     65     is_sync_footer_visible_ = is_sync_enabled;
     66     web_ui()->CallJavascriptFunction("welcome.setSyncFooterVisible",
     67                                      base::FundamentalValue(is_sync_enabled));
     68   }
     69 }
     70