Home | History | Annotate | Download | only in browser
      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/shell_integration.h"
      6 
      7 #include "base/mac/mac_util.h"
      8 #include "chrome/browser/platform_util.h"
      9 #import "third_party/mozilla/NSWorkspace+Utils.h"
     10 
     11 // Sets Chromium as default browser (only for current user). Returns false if
     12 // this operation fails.
     13 bool ShellIntegration::SetAsDefaultBrowser() {
     14   if (!platform_util::CanSetAsDefaultBrowser())
     15     return false;
     16 
     17   // We really do want the main bundle here, not base::mac::MainAppBundle(),
     18   // which is the bundle for the framework.
     19   NSString* identifier = [[NSBundle mainBundle] bundleIdentifier];
     20   [[NSWorkspace sharedWorkspace] setDefaultBrowserWithIdentifier:identifier];
     21   return true;
     22 }
     23 
     24 namespace {
     25 
     26 // Returns true if |identifier| is the bundle id of the default browser.
     27 bool IsIdentifierDefaultBrowser(NSString* identifier) {
     28   NSString* defaultBrowser =
     29       [[NSWorkspace sharedWorkspace] defaultBrowserIdentifier];
     30   if (!defaultBrowser)
     31     return false;
     32   // We need to ensure we do the comparison case-insensitive as LS doesn't
     33   // persist the case of our bundle id.
     34   NSComparisonResult result =
     35     [defaultBrowser caseInsensitiveCompare:identifier];
     36   return result == NSOrderedSame;
     37 }
     38 
     39 }  // namespace
     40 
     41 // Attempt to determine if this instance of Chrome is the default browser and
     42 // return the appropriate state. (Defined as being the handler for HTTP/HTTPS
     43 // protocols; we don't want to report "no" here if the user has simply chosen
     44 // to open HTML files in a text editor and FTP links with an FTP client.)
     45 ShellIntegration::DefaultBrowserState ShellIntegration::IsDefaultBrowser() {
     46   // As above, we want to use the real main bundle.
     47   NSString* myIdentifier = [[NSBundle mainBundle] bundleIdentifier];
     48   if (!myIdentifier)
     49     return UNKNOWN_DEFAULT_BROWSER;
     50   return IsIdentifierDefaultBrowser(myIdentifier) ? IS_DEFAULT_BROWSER
     51                                                   : NOT_DEFAULT_BROWSER;
     52 }
     53 
     54 // Returns true if Firefox is the default browser for the current user.
     55 bool ShellIntegration::IsFirefoxDefaultBrowser() {
     56   return IsIdentifierDefaultBrowser(@"org.mozilla.firefox");
     57 }
     58