Home | History | Annotate | Download | only in content
      1 /*
      2  * Copyright (C) 2011 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  *
     14  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     16  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     17  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     18  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     19  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     20  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     21  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "content/content_detector.h"
     27 #include "PlatformString.h"
     28 
     29 #define NAVIGATION_MAX_PHONE_LENGTH 14
     30 
     31 struct FindState {
     32     int mStartResult;
     33     int mEndResult;
     34     char* mPattern;
     35     UChar mStore[NAVIGATION_MAX_PHONE_LENGTH + 1];
     36     UChar* mStorePtr;
     37     UChar mBackOne;
     38     UChar mBackTwo;
     39     UChar mCurrent;
     40     bool mOpenParen;
     41     bool mInitialized;
     42     bool mContinuationNode;
     43 };
     44 
     45 enum FoundState {
     46     FOUND_NONE,
     47     FOUND_PARTIAL,
     48     FOUND_COMPLETE
     49 };
     50 
     51 // Searches for phone numbers (US only) or email addresses based off of the navcache code
     52 class PhoneEmailDetector : public ContentDetector {
     53 public:
     54     PhoneEmailDetector();
     55     virtual ~PhoneEmailDetector() {}
     56 
     57 private:
     58     // Implementation of ContentDetector.
     59     virtual bool FindContent(const string16::const_iterator& begin,
     60                              const string16::const_iterator& end,
     61                              size_t* start_pos,
     62                              size_t* end_pos);
     63 
     64     virtual std::string GetContentText(const WebKit::WebRange& range);
     65     virtual GURL GetIntentURL(const std::string& content_text);
     66     virtual size_t GetMaximumContentLength() {
     67         return NAVIGATION_MAX_PHONE_LENGTH * 4;
     68     }
     69     virtual bool IsEnabled(const WebKit::WebHitTestInfo& hit_test) OVERRIDE;
     70 
     71     DISALLOW_COPY_AND_ASSIGN(PhoneEmailDetector);
     72 
     73     FindState m_findState;
     74     FoundState m_foundResult;
     75     const char* m_prefix;
     76     // TODO: This shouldn't be done like this. PhoneEmailDetector should be
     77     // refactored into two pieces and follow the IsEnabled style. This will
     78     // only work because we always call IsEnabled before FindContent
     79     bool m_isPhoneDetectionEnabled;
     80     bool m_isEmailDetectionEnabled;
     81 };
     82